From d0ce5208463a9ca214f3fee7366f9790bf0304f8 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Wed, 28 Dec 2022 10:19:09 +0530 Subject: [PATCH 1/2] add force flag to rmSync calls to prevent not found errors --- src/services/ffmpeg.ts | 4 ++-- src/services/heicConverter.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/services/ffmpeg.ts b/src/services/ffmpeg.ts index 104a47dcb..3c413de55 100644 --- a/src/services/ffmpeg.ts +++ b/src/services/ffmpeg.ts @@ -45,7 +45,7 @@ export async function runFFmpegCmd( throw e; } finally { try { - rmSync(tempOutputFilePath); + rmSync(tempOutputFilePath, { force: true }); } catch (e) { logErrorSentry(e, 'failed to remove tempOutputFile'); } @@ -66,5 +66,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/heicConverter.ts b/src/services/heicConverter.ts index c094cde06..38da053fe 100644 --- a/src/services/heicConverter.ts +++ b/src/services/heicConverter.ts @@ -39,12 +39,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'); } From 1e7292d8ba55f5e25dbce35449e35f0f6cf47a24 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Wed, 28 Dec 2022 11:28:07 +0530 Subject: [PATCH 2/2] check file exists before reading --- src/services/ffmpeg.ts | 7 ++++++- src/services/fs.ts | 13 +++++++++++++ src/services/heicConverter.ts | 5 ++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/services/ffmpeg.ts b/src/services/ffmpeg.ts index 3c413de55..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,7 +40,11 @@ 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; 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 38da053fe..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) );