Skip to content

Commit

Permalink
Skip: Enable concurrent object downloads from a single container (mer…
Browse files Browse the repository at this point in the history
…ge commit)

Merge branch 'bugfix/download-multiple-objs' into 'main'
* Refactor download abort to fix download temp files staying when cancelling before download begins

* Fix download progress resetting to 0 when starting another direct download

* Identify download sessions by random ids to enable concurrent downloads from a single container

Closes #1231
See merge request https://gitlab.ci.csc.fi/sds-dev/sd-connect/swift-browser-ui/-/merge_requests/311

Approved-by: Joonatan Mäkinen <jmakine@csc.fi>
Approved-by: Hang Le <lhang@csc.fi>
Merged by Monika Radaviciute <mradavic@csc.fi>
  • Loading branch information
mradavi committed Apr 26, 2024
2 parents addfcbf + 89161b9 commit 348b216
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 111 deletions.
27 changes: 21 additions & 6 deletions swift_browser_ui_frontend/src/common/socket.js
Expand Up @@ -102,6 +102,7 @@ export default class UploadSocket {
}
this.$store.commit("addDownload");
this.getHeaders(
e.data.id,
e.data.container,
e.data.files,
e.data.pubkey,
Expand All @@ -112,13 +113,17 @@ export default class UploadSocket {
this.$store.commit("removeDownload");
this.$store.commit("toggleDownloadNotification", false);
} else {
this.$store.commit("updateDownloadProgress", 0);
if (this.$store.state.downloadProgress === undefined) {
this.$store.commit("updateDownloadProgress", 0);
}
}
if (DEV) {
console.log(
`Got headers for download in container ${e.data.container}`,
);
}
}).catch(() => {
this.downWorker.postMessage({ command: "abort", reason: "error" });
});
break;
case "downloadStarted":
Expand All @@ -131,14 +136,14 @@ export default class UploadSocket {
this.downloadFinished = false;
if (e.data.archive) {
let downloadUrl = new URL(
`/archive/${e.data.container}.tar`,
`/archive/${e.data.id}/${e.data.container}.tar`,
document.location.origin,
);
if (DEV) console.log(downloadUrl);
window.open(downloadUrl, "_blank");
} else {
let downloadUrl = new URL(
`/file/${e.data.container}/${e.data.path}`,
`/file/${e.data.id}/${e.data.container}/${e.data.path}`,
document.location.origin,
);
if (DEV) console.log(downloadUrl);
Expand Down Expand Up @@ -221,7 +226,7 @@ export default class UploadSocket {
}

// Get headers for download
async getHeaders(container, fileList, pubkey, owner, ownerName) {
async getHeaders(id, container, fileList, pubkey, owner, ownerName) {
let headers = {};

// Cache the container ID
Expand Down Expand Up @@ -314,13 +319,15 @@ export default class UploadSocket {
if (!this.useServiceWorker) {
this.downWorker.postMessage({
command: "addHeaders",
id: id,
container: container,
headers: headers,
});
} else {
navigator.serviceWorker.ready.then((reg) => {
reg.active.postMessage({
command: "addHeaders",
id: id,
container: container,
headers: headers,
});
Expand Down Expand Up @@ -355,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 Expand Up @@ -395,12 +402,15 @@ export default class UploadSocket {
objects,
owner = "",
) {

//get random id
const sessionId = Math.random().toString(36).slice(2, 8);

let ownerName = "";
if (owner) {
let ids = await this.$store.state.client.projectCheckIDs(owner);
ownerName = ids.name;
}

let fileHandle = undefined;
if (objects.length == 1) {
// Download directly into the file if available.
Expand All @@ -424,8 +434,10 @@ export default class UploadSocket {
];
}
fileHandle = await window.showSaveFilePicker(opts);

this.downWorker.postMessage({
command: "downloadFile",
id: sessionId,
container: container,
file: objects[0],
handle: fileHandle,
Expand All @@ -439,6 +451,7 @@ export default class UploadSocket {
navigator.serviceWorker.ready.then(reg => {
reg.active.postMessage({
command: "downloadFile",
id: sessionId,
container: container,
file: objects[0],
owner: owner,
Expand All @@ -463,6 +476,7 @@ export default class UploadSocket {
});
this.downWorker.postMessage({
command: "downloadFiles",
id: sessionId,
container: container,
files: objects.length < 1 ? [] : objects,
handle: fileHandle,
Expand All @@ -473,6 +487,7 @@ export default class UploadSocket {
navigator.serviceWorker.ready.then(reg => {
reg.active.postMessage({
command: "downloadFiles",
id: sessionId,
container: container,
files: objects.length < 1 ? [] : objects,
owner: owner,
Expand Down

0 comments on commit 348b216

Please sign in to comment.