Let fs.readFile throw on non-existent files

This commit is contained in:
Manav Rathi 2024-03-22 15:39:05 +05:30
parent b6b4ee7e3a
commit 1830a1b931
No known key found for this signature in database
2 changed files with 8 additions and 18 deletions

View file

@ -2,9 +2,8 @@ import { exec } from "child_process";
import util from "util";
import log from "electron-log";
import { existsSync, rmSync } from "fs";
import * as fs from "node:fs/promises";
import path from "path";
import { readFile, writeFile } from "promise-fs";
import { CustomErrors } from "../constants/errors";
import { isDev } from "../utils/common";
import { isPlatform } from "../utils/common/platform";
@ -88,28 +87,22 @@ export async function convertToJPEG(
tempInputFilePath = await generateTempFilePath(filename);
tempOutputFilePath = await generateTempFilePath("output.jpeg");
await writeFile(tempInputFilePath, fileData);
await fs.writeFile(tempInputFilePath, fileData);
await runConvertCommand(tempInputFilePath, tempOutputFilePath);
if (!existsSync(tempOutputFilePath)) {
throw new Error("heic convert output file not found");
}
const convertedFileData = new Uint8Array(
await readFile(tempOutputFilePath),
);
return convertedFileData;
return new Uint8Array(await fs.readFile(tempOutputFilePath));
} catch (e) {
logErrorSentry(e, "failed to convert heic");
throw e;
} finally {
try {
rmSync(tempInputFilePath, { force: true });
await fs.rm(tempInputFilePath, { force: true });
} catch (e) {
logErrorSentry(e, "failed to remove tempInputFile");
}
try {
rmSync(tempOutputFilePath, { force: true });
await fs.rm(tempOutputFilePath, { force: true });
} catch (e) {
logErrorSentry(e, "failed to remove tempOutputFile");
}
@ -183,10 +176,7 @@ export async function generateImageThumbnail(
quality,
);
if (!existsSync(tempOutputFilePath)) {
throw new Error("output thumbnail file not found");
}
thumbnail = new Uint8Array(await readFile(tempOutputFilePath));
thumbnail = new Uint8Array(await fs.readFile(tempOutputFilePath));
quality -= 10;
} while (thumbnail.length > maxSize && quality > MIN_QUALITY);
return thumbnail;
@ -195,7 +185,7 @@ export async function generateImageThumbnail(
throw e;
} finally {
try {
rmSync(tempOutputFilePath, { force: true });
await fs.rm(tempOutputFilePath, { force: true });
} catch (e) {
logErrorSentry(e, "failed to remove tempOutputFile");
}

View file

@ -1,8 +1,8 @@
import { app, BrowserWindow, Menu, nativeImage, Tray } from "electron";
import ElectronLog from "electron-log";
import { existsSync } from "node:fs";
import os from "os";
import path from "path";
import { existsSync } from "promise-fs";
import util from "util";
import { rendererURL } from "../main";
import { setupAutoUpdater } from "../services/appUpdater";