Skip to content

Commit

Permalink
Refactor download abort to fix download temp files staying when cance…
Browse files Browse the repository at this point in the history
…lling before download begins
  • Loading branch information
mradavi committed Apr 25, 2024
1 parent d7acc2a commit 89161b9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
4 changes: 3 additions & 1 deletion swift_browser_ui_frontend/src/common/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ export default class UploadSocket {
`Got headers for download in container ${e.data.container}`,
);
}
}).catch(() => {
this.downWorker.postMessage({ command: "abort", reason: "error" });
});
break;
case "downloadStarted":
Expand Down Expand Up @@ -360,7 +362,7 @@ export default class UploadSocket {
}

cancelDownload() {
this.downWorker.postMessage({ command: "cancel" });
this.downWorker.postMessage({ command: "abort", reason: "cancel" });
if (DEV) console.log("Cancel direct downloads");
}

Expand Down
51 changes: 23 additions & 28 deletions swift_browser_ui_frontend/wasm/js/crypt-post-downworker.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ let downProgressInterval = undefined;
let totalDone = 0;
let totalToDo = 0;
let aborted = false;
let cancelled = false;

// Use a 50 MiB segment when downloading
const DOWNLOAD_SEGMENT_SIZE = 1024 * 1024 * 50;
Expand Down Expand Up @@ -69,7 +68,6 @@ if (inServiceWorker) {
// Create a download session
function createDownloadSession(id, container, handle, archive) {
aborted = false; //reset
cancelled = false;

let keypairPtr = Module.ccall(
"create_keypair",
Expand Down Expand Up @@ -297,7 +295,7 @@ class FileSlicer {
await this.getStart();

while (!this.done) {
if (cancelled) return;
if (aborted) return;
if (this.output instanceof WritableStream) {
await this.output.write(this.chunk);
} else {
Expand All @@ -318,7 +316,7 @@ class FileSlicer {
// Round up to a multiple of 512, because tar
await this.padFile();

return;
return true;
}

async sliceFile() {
Expand All @@ -327,7 +325,7 @@ class FileSlicer {

// Slice the file and write decrypted content to output
while (!this.done) {
if (cancelled) return;
if (aborted) return;
await this.getSlice();

if (this.output instanceof WritableStream) {
Expand Down Expand Up @@ -368,13 +366,6 @@ class FileSlicer {
);
return true;
}

async abortStream() {
if (this.output instanceof WritableStream) {
//stop writing to stream
await this.output.abort();
}
}
}

function clear() {
Expand All @@ -386,7 +377,7 @@ function clear() {
totalToDo = 0;
}

async function abortDownloads(direct, abortReason) {
function startAbort(direct, abortReason) {
aborted = true;
const msg = {
eventType: "abort",
Expand All @@ -401,11 +392,16 @@ async function abortDownloads(direct, abortReason) {
});
}
clear();
for (let id in downloads) {
finishDownloadSession(id);
}
}

async function abortDownload(id, stream = null) {
if (downloads[id].direct) {
//remove temp files
if (stream) await stream.abort();
await downloads[id].handle.remove();
}
finishDownloadSession(id);
}

// Safely free and remove a download session
function finishDownloadSession(id) {
Expand Down Expand Up @@ -485,6 +481,10 @@ async function beginDownloadInSession(
}

for (const file in downloads[id].files) {
if (aborted) {
await abortDownload(id, fileStream);
return;
}
if (inServiceWorker) {
self.clients.matchAll().then(clients => {
clients.forEach(client =>
Expand Down Expand Up @@ -527,15 +527,8 @@ async function beginDownloadInSession(
});
}
if (!res) {
await slicer.abortStream().then(async() => {
//remove temporary files
if (downloads[id].direct) {
await downloads[id].handle.remove();
}
});
if (!aborted) {
await abortDownloads(!inServiceWorker, cancelled ? "cancel" : "error");
}
if (!aborted) startAbort(!inServiceWorker, "error");
await abortDownload(id, fileStream);
return;
}
}
Expand Down Expand Up @@ -705,7 +698,6 @@ self.addEventListener("message", async (e) => {
}
break;
case "addHeaders":
if (aborted) return;
addSessionFiles(e.data.id, e.data.container, e.data.headers).then(ret => {
if (ret && inServiceWorker) {
e.source.postMessage({
Expand All @@ -718,6 +710,9 @@ self.addEventListener("message", async (e) => {
container: e.data.container,
});
}
}).catch(async () => {
if (!aborted) startAbort(!inServiceWorker, "error");
await abortDownload(e.data.id);
});
if (inServiceWorker) {
e.source.postMessage({
Expand All @@ -741,8 +736,8 @@ self.addEventListener("message", async (e) => {
case "clear":
clear();
break;
case "cancel":
cancelled = true;
case "abort":
if (!aborted) startAbort(!inServiceWorker, e.data.reason);
break;
}
});
Expand Down

0 comments on commit 89161b9

Please sign in to comment.