diff --git a/desktop/src/main/services/upload.ts b/desktop/src/main/services/upload.ts index ecd50df779..3da9cafd7c 100644 --- a/desktop/src/main/services/upload.ts +++ b/desktop/src/main/services/upload.ts @@ -9,21 +9,22 @@ import { clearOpenZipCache, markClosableZip, openZip } from "./zip"; export const listZipItems = async (zipPath: string): Promise => { const zip = openZip(zipPath); - const entries = await zip.entries(); - const entryNames: string[] = []; + try { + const entries = await zip.entries(); + const entryNames: string[] = []; - for (const entry of Object.values(entries)) { - const basename = path.basename(entry.name); - // Ignore "hidden" files (files whose names begins with a dot). - if (entry.isFile && !basename.startsWith(".")) { - // `entry.name` is the path within the zip. - entryNames.push(entry.name); + for (const entry of Object.values(entries)) { + const basename = path.basename(entry.name); + // Ignore "hidden" files (files whose names begins with a dot). + if (entry.isFile && !basename.startsWith(".")) { + // `entry.name` is the path within the zip. + entryNames.push(entry.name); + } } + return entryNames.map((entryName) => [zipPath, entryName]); + } finally { + markClosableZip(zipPath); } - - markClosableZip(zipPath); - - return entryNames.map((entryName) => [zipPath, entryName]); }; export const pathOrZipItemSize = async ( @@ -35,14 +36,16 @@ export const pathOrZipItemSize = async ( } else { const [zipPath, entryName] = pathOrZipItem; const zip = openZip(zipPath); - const entry = await zip.entry(entryName); - if (!entry) - throw new Error( - `An entry with name ${entryName} does not exist in the zip file at ${zipPath}`, - ); - const size = entry.size; - await zip.close(); - return size; + try { + const entry = await zip.entry(entryName); + if (!entry) + throw new Error( + `An entry with name ${entryName} does not exist in the zip file at ${zipPath}`, + ); + return entry.size; + } finally { + markClosableZip(zipPath); + } } }; diff --git a/desktop/src/main/stream.ts b/desktop/src/main/stream.ts index 38feb192af..beddab8488 100644 --- a/desktop/src/main/stream.ts +++ b/desktop/src/main/stream.ts @@ -129,7 +129,7 @@ const handleReadZip = async (zipPath: string, entryName: string) => { const webReadableStream = webReadableStreamAny as ReadableStream; - // Close the zip handle when the underlying stream closes. + // Let go of the zip handle when the underlying stream closes. stream.on("end", () => markClosableZip(zipPath)); // While it is documented that entry.time is the modification time, diff --git a/desktop/src/main/utils/temp.ts b/desktop/src/main/utils/temp.ts index 912264387a..a37f5767f3 100644 --- a/desktop/src/main/utils/temp.ts +++ b/desktop/src/main/utils/temp.ts @@ -129,8 +129,11 @@ export const makeFileForDataOrPathOrZipItem = async ( writeToTemporaryFile = async () => { const [zipPath, entryName] = dataOrPathOrZipItem; const zip = openZip(zipPath); - await zip.extract(entryName, path); - markClosableZip(zipPath); + try { + await zip.extract(entryName, path); + } finally { + markClosableZip(zipPath); + } }; } }