This commit is contained in:
Manav Rathi 2024-05-13 15:48:20 +05:30
parent 1b8b840ecf
commit 0e3649f9b6
No known key found for this signature in database
4 changed files with 47 additions and 40 deletions

View file

@ -1,11 +1,10 @@
import pathToFfmpeg from "ffmpeg-static";
import fs from "node:fs/promises";
import type { ZipItem } from "../../types/ipc";
import log from "../log";
import { ensure, withTimeout } from "../utils/common";
import { execAsync } from "../utils/electron";
import {
deleteTempFile,
deleteTempFileIgnoringErrors,
makeFileForDataOrPathOrZipItem,
makeTempFilePath,
} from "../utils/temp";
@ -74,12 +73,9 @@ export const ffmpegExec = async (
return fs.readFile(outputFilePath);
} finally {
try {
if (isInputFileTemporary) await deleteTempFile(inputFilePath);
await deleteTempFile(outputFilePath);
} catch (e) {
log.error("Could not clean up temp files", e);
}
if (isInputFileTemporary)
await deleteTempFileIgnoringErrors(inputFilePath);
await deleteTempFileIgnoringErrors(outputFilePath);
}
};
@ -157,11 +153,8 @@ export const ffmpegConvertToMP4 = async (
return fs.readFile(outputFilePath);
} finally {
try {
if (isInputFileTemporary) await deleteTempFile(inputFilePath);
await deleteTempFile(outputFilePath);
} catch (e) {
log.error("Could not clean up temp files", e);
}
if (isInputFileTemporary)
await deleteTempFileIgnoringErrors(inputFilePath);
await deleteTempFileIgnoringErrors(outputFilePath);
}
};

View file

@ -6,7 +6,7 @@ import { CustomErrorMessage, type ZipItem } from "../../types/ipc";
import log from "../log";
import { execAsync, isDev } from "../utils/electron";
import {
deleteTempFile,
deleteTempFileIgnoringErrors,
makeFileForDataOrPathOrZipItem,
makeTempFilePath,
} from "../utils/temp";
@ -23,12 +23,8 @@ export const convertToJPEG = async (imageData: Uint8Array) => {
await execAsync(command);
return new Uint8Array(await fs.readFile(outputFilePath));
} finally {
try {
await deleteTempFile(inputFilePath);
await deleteTempFile(outputFilePath);
} catch (e) {
log.error("Could not clean up temp files", e);
}
await deleteTempFileIgnoringErrors(inputFilePath);
await deleteTempFileIgnoringErrors(outputFilePath);
}
};
@ -107,12 +103,9 @@ export const generateImageThumbnail = async (
} while (thumbnail.length > maxSize && quality > 50);
return thumbnail;
} finally {
try {
if (isInputFileTemporary) await deleteTempFile(inputFilePath);
await deleteTempFile(outputFilePath);
} catch (e) {
log.error("Could not clean up temp files", e);
}
if (isInputFileTemporary)
await deleteTempFileIgnoringErrors(inputFilePath);
await deleteTempFileIgnoringErrors(outputFilePath);
}
};

View file

@ -3,6 +3,7 @@
*/
import { net, protocol } from "electron/main";
import StreamZip from "node-stream-zip";
import { randomUUID } from "node:crypto";
import { createWriteStream, existsSync } from "node:fs";
import fs from "node:fs/promises";
import { Readable } from "node:stream";
@ -10,7 +11,11 @@ import { ReadableStream } from "node:stream/web";
import { pathToFileURL } from "node:url";
import log from "./log";
import { ensure } from "./utils/common";
import { deleteTempFile } from "./utils/temp";
import {
deleteTempFile,
deleteTempFileIgnoringErrors,
makeTempFilePath,
} from "./utils/temp";
/**
* Register a protocol handler that we use for streaming large files between the
@ -224,20 +229,22 @@ export const clearConvertToMP4Results = () => convertToMP4Results.clear();
* See also: [Note: IPC streams]
*/
const handleConvertToMP4Write = (request: Request) => {
/*
try {
const inputTempFilePath = await makeTempFilePath();
const handleConvertToMP4Write = async (request: Request) => {
const inputTempFilePath = await makeTempFilePath();
await writeStream(inputTempFilePath, ensure(request.body));
await writeStream(path, ensure(request.body));
return new Response("", { status: 200 });
}
const outputTempFilePath = await makeTempFilePath();
try {
//
} catch (e) {
log.error("Failed to handle convert-to-mp4 stream", e);
return new Response(`Failed to write stream: ${String(e)}`, {
status: 500,
});
}*/
await deleteTempFileIgnoringErrors(inputTempFilePath);
await deleteTempFileIgnoringErrors(outputTempFilePath);
throw e;
}
const token = randomUUID();
convertToMP4Results.set(token, outputTempFilePath);
return new Response(token, { status: 200 });
};
const handleConvertToMP4Read = async (token: string) => {

View file

@ -4,6 +4,7 @@ import { existsSync } from "node:fs";
import fs from "node:fs/promises";
import path from "node:path";
import type { ZipItem } from "../../types/ipc";
import log from "../log";
import { ensure } from "./common";
/**
@ -62,6 +63,19 @@ export const deleteTempFile = async (tempFilePath: string) => {
await fs.rm(tempFilePath, { force: true });
};
/**
* A variant of {@link deleteTempFile} that supresses any errors, making it
* safe to call them in a sequence without needing to handle the scenario where
* one of them failing causes the rest to be skipped.
*/
export const deleteTempFileIgnoringErrors = async (tempFilePath: string) => {
try {
await deleteTempFile(tempFilePath);
} catch (e) {
log.error(`Could not delete temporary file at path ${tempFilePath}`, e);
}
};
/** The result of {@link makeFileForDataOrPathOrZipItem}. */
interface FileForDataOrPathOrZipItem {
/**