Skip to content

Commit 0c4d0fd

Browse files
author
Joonatan Mäkinen
committed
Remove temporary files after direct download abort (merge commit)
Merge branch 'feature/remove-temp-files' into 'main' * Fix removal of temp files for multiple concurrent downloads * Remove temporary files after direct download abort Closes #1230 See merge request https://gitlab.ci.csc.fi/sds-dev/sd-connect/swift-browser-ui/-/merge_requests/308 Approved-by: Hang Le <[email protected]> Approved-by: Joonatan Mäkinen <[email protected]> Co-authored-by: Monika Radaviciute <[email protected]> Merged by Joonatan Mäkinen <[email protected]>
2 parents 5dcd564 + a00d65a commit 0c4d0fd

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

swift_browser_ui_frontend/wasm/js/crypt-post-downworker.js

+26-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ let downProgressInterval = undefined;
2525
let totalDone = 0;
2626
let totalToDo = 0;
2727
let aborted = false;
28+
let cancelled = false;
2829

2930
// Use a 50 MiB segment when downloading
3031
const DOWNLOAD_SEGMENT_SIZE = 1024 * 1024 * 50;
@@ -66,6 +67,7 @@ if (inServiceWorker) {
6667
// Create a download session
6768
function createDownloadSession(container, handle, archive) {
6869
aborted = false; //reset
70+
cancelled = false;
6971

7072
let keypairPtr = Module.ccall(
7173
"create_keypair",
@@ -292,6 +294,7 @@ class FileSlicer {
292294
await this.getStart();
293295

294296
while (!this.done) {
297+
if (cancelled) return;
295298
if (this.output instanceof WritableStream) {
296299
await this.output.write(this.chunk);
297300
} else {
@@ -321,6 +324,7 @@ class FileSlicer {
321324

322325
// Slice the file and write decrypted content to output
323326
while (!this.done) {
327+
if (cancelled) return;
324328
await this.getSlice();
325329

326330
if (this.output instanceof WritableStream) {
@@ -361,6 +365,13 @@ class FileSlicer {
361365
);
362366
return true;
363367
}
368+
369+
async abortStream() {
370+
if (this.output instanceof WritableStream) {
371+
//stop writing to stream
372+
await this.output.abort();
373+
}
374+
}
364375
}
365376

366377
function clear() {
@@ -372,7 +383,8 @@ function clear() {
372383
totalToDo = 0;
373384
}
374385

375-
function abortDownloads(direct, abortReason) {
386+
async function abortDownloads(direct, abortReason) {
387+
aborted = true;
376388
const msg = {
377389
eventType: "abort",
378390
reason: abortReason,
@@ -386,7 +398,7 @@ function abortDownloads(direct, abortReason) {
386398
});
387399
}
388400
clear();
389-
aborted = true;
401+
390402
for (let container in downloads) {
391403
finishDownloadSession(container);
392404
}
@@ -511,10 +523,18 @@ async function beginDownloadInSession(
511523
res = await slicer.sliceFile().catch(() => {
512524
return false;
513525
});
514-
if (!res) {
515-
if (!aborted) abortDownloads(!inServiceWorker, "error");
516-
return;
526+
}
527+
if (!res) {
528+
await slicer.abortStream().then(async() => {
529+
//remove temporary files
530+
if (downloads[container].direct) {
531+
await downloads[container].handle.remove();
532+
}
533+
});
534+
if (!aborted) {
535+
await abortDownloads(!inServiceWorker, cancelled ? "cancel" : "error");
517536
}
537+
return;
518538
}
519539
}
520540

@@ -711,7 +731,7 @@ self.addEventListener("message", async (e) => {
711731
clear();
712732
break;
713733
case "cancel":
714-
abortDownloads(!inServiceWorker, "cancel");
734+
cancelled = true;
715735
break;
716736
}
717737
});

0 commit comments

Comments
 (0)