fix: remove the partial file when a desktop download is aborted early

This commit is contained in:
LukeGus committed 2026-11-04 15:30:17 -06:00
1 parent c7aa1c64b5
commit ea4e2d2124
2 files changed
+22 -1

No files matched your search

@@ -560,7 +560,13 @@ export async function openDownload(
}
},
abort: async () => {
stream.destroy();
// The file opens in the background, so wait for the stream to close
// or a late open would leave the partial file behind.
await new Promise<void>((resolve) => {
if (stream.closed) return resolve();
stream.once("close", () => resolve());
stream.destroy();
});
await fsp.rm(partialPath, { force: true }).catch(() => {});
release();
},
@@ -186,6 +186,21 @@ describe("ctx.desktop.local", () => {
expect(fs.readdirSync(dir)).toEqual([]);
});
it("leaves no partial file when aborted before the file has opened", async () => {
grants.set("demo", ["desktop:local"]);
const ctx = contextFor("demo", ["desktop:local"]);
for (let i = 0; i < 20; i++) {
const download = await ctx.desktop.local.fs.openDownload({
rootPath: dir,
destPath: path.join(dir, `early-${i}.txt`),
transferId: `early-${i}`,
});
await download.abort();
}
await new Promise((resolve) => setTimeout(resolve, 20));
expect(fs.readdirSync(dir)).toEqual([]);
});
it("sends trash and open through Electron main and audits each call", async () => {
grants.set("demo", ["desktop:local"]);
const ctx = contextFor("demo", ["desktop:local"]);