diff --git a/src/services/ffmpeg.ts b/src/services/ffmpeg.ts index 104a47dcb..dfd24737a 100644 --- a/src/services/ffmpeg.ts +++ b/src/services/ffmpeg.ts @@ -5,6 +5,7 @@ import log from 'electron-log'; import { readFile, rmSync, writeFile } from 'promise-fs'; import { logErrorSentry } from './sentry'; import { generateTempFilePath, getTempDirPath } from '../utils/temp'; +import { existsSync } from 'fs'; const execAsync = util.promisify(require('child_process').exec); @@ -39,13 +40,17 @@ export async function runFFmpegCmd( cmd = shellescape(cmd); log.info('cmd', cmd); await execAsync(cmd); - return new Uint8Array(await readFile(tempOutputFilePath)); + if (!existsSync(tempOutputFilePath)) { + throw new Error('ffmpeg output file not found'); + } + const outputFile = await readFile(tempOutputFilePath); + return new Uint8Array(outputFile); } catch (e) { logErrorSentry(e, 'ffmpeg run command error'); throw e; } finally { try { - rmSync(tempOutputFilePath); + rmSync(tempOutputFilePath, { force: true }); } catch (e) { logErrorSentry(e, 'failed to remove tempOutputFile'); } @@ -66,5 +71,5 @@ export async function deleteTempFile(tempFilePath: string) { 'tried to delete a non temp file' ); } - rmSync(tempFilePath); + rmSync(tempFilePath, { force: true }); } diff --git a/src/services/fs.ts b/src/services/fs.ts index fa3abd56d..ba22e39ff 100644 --- a/src/services/fs.ts +++ b/src/services/fs.ts @@ -5,6 +5,7 @@ import { ElectronFile } from '../types'; import StreamZip from 'node-stream-zip'; import { Readable } from 'stream'; import { logError } from './logging'; +import { existsSync } from 'fs'; // https://stackoverflow.com/a/63111390 export const getDirFilePaths = async (dirPath: string) => { @@ -64,13 +65,22 @@ export async function getElectronFile(filePath: string): Promise { size: fileStats.size, lastModified: fileStats.mtime.valueOf(), stream: async () => { + if (!existsSync(filePath)) { + throw new Error('electronFile does not exist'); + } return await getFileStream(filePath); }, blob: async () => { + if (!existsSync(filePath)) { + throw new Error('electronFile does not exist'); + } const blob = await fs.readFile(filePath); return new Blob([new Uint8Array(blob)]); }, arrayBuffer: async () => { + if (!existsSync(filePath)) { + throw new Error('electronFile does not exist'); + } const blob = await fs.readFile(filePath); return new Uint8Array(blob); }, @@ -207,5 +217,8 @@ export function writeStream(filePath: string, fileStream: any) { } export async function readTextFile(filePath: string) { + if (!existsSync(filePath)) { + throw new Error('File does not exist'); + } return await fs.readFile(filePath, 'utf-8'); } diff --git a/src/services/heicConverter.ts b/src/services/heicConverter.ts index c094cde06..e1af0a27f 100644 --- a/src/services/heicConverter.ts +++ b/src/services/heicConverter.ts @@ -1,7 +1,7 @@ import util from 'util'; import { exec } from 'child_process'; -import { rmSync } from 'fs'; +import { existsSync, rmSync } from 'fs'; import { readFile, writeFile } from 'promise-fs'; import { generateTempFilePath } from '../utils/temp'; import { logErrorSentry } from './sentry'; @@ -30,6 +30,9 @@ export async function convertHEIC( await runConvertCommand(tempInputFilePath, tempOutputFilePath); + if (!existsSync(tempOutputFilePath)) { + throw new Error('heic convert output file not found'); + } const convertedFileData = new Uint8Array( await readFile(tempOutputFilePath) ); @@ -39,12 +42,12 @@ export async function convertHEIC( throw e; } finally { try { - rmSync(tempInputFilePath); + rmSync(tempInputFilePath, { force: true }); } catch (e) { logErrorSentry(e, 'failed to remove tempInputFile'); } try { - rmSync(tempOutputFilePath); + rmSync(tempOutputFilePath, { force: true }); } catch (e) { logErrorSentry(e, 'failed to remove tempOutputFile'); }