Merge pull request #126 from ente-io/fs-better-checks

Fs better checks
This commit is contained in:
Abhinav Kumar 2022-12-28 11:55:18 +05:30 committed by GitHub
commit 7ef5b0f35a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 6 deletions

View file

@ -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 });
}

View file

@ -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<ElectronFile> {
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');
}

View file

@ -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');
}