feat: B4 - progressbar bij single file

This commit is contained in:
kodi
2026-03-14 14:58:07 +01:00
parent d459f3c524
commit 8af4b1a6b0
4 changed files with 76 additions and 5 deletions
+64 -5
View File
@@ -86,6 +86,7 @@ let downloadProgressState = {
archiveLabel: "",
totalItems: 0,
requestKey: null,
mode: null,
taskId: null,
cancelRequested: false,
};
@@ -377,6 +378,10 @@ function zipDownloadRequestKey(paths) {
return paths.join("\n");
}
function singleFileDownloadRequestKey(path) {
return path;
}
function selectedItemCountLabel(totalItems) {
return `${totalItems} selected item${totalItems === 1 ? "" : "s"}`;
}
@@ -465,6 +470,7 @@ function updateDownloadModalDisplay(info) {
function openZipDownloadModal(selectedItems) {
const requestPaths = selectedItems.map((item) => item.path);
downloadProgressState.active = true;
downloadProgressState.mode = "archive";
downloadProgressState.archiveLabel = "ZIP archive";
downloadProgressState.totalItems = selectedItems.length;
downloadProgressState.requestKey = zipDownloadRequestKey(requestPaths);
@@ -483,6 +489,26 @@ function openZipDownloadModal(selectedItems) {
});
}
function openSingleFileDownloadModal(selectedItem) {
downloadProgressState.active = true;
downloadProgressState.mode = "single_file";
downloadProgressState.archiveLabel = selectedItem.name;
downloadProgressState.totalItems = 1;
downloadProgressState.requestKey = singleFileDownloadRequestKey(selectedItem.path);
downloadProgressState.taskId = null;
downloadProgressState.cancelRequested = false;
setDownloadModalVisible(true);
updateDownloadModalDisplay({
active: true,
targetText: `File download requested: ${selectedItem.name}`,
currentFileText: `File: ${selectedItem.path}`,
countText: "Direct file download",
statusText: "Requesting download...",
percent: 0,
cancelVisible: false,
});
}
function markZipDownloadReady(fileName) {
downloadProgressState.active = false;
downloadProgressState.cancelRequested = false;
@@ -527,6 +553,33 @@ function markZipDownloadCancelled() {
});
}
function markSingleFileDownloadRequested(fileName, path) {
downloadProgressState.active = false;
updateDownloadModalDisplay({
active: false,
targetText: `File download requested: ${fileName}`,
currentFileText: `File: ${path}`,
countText: "Browser download requested",
statusText: "Download requested",
percent: 0,
cancelVisible: false,
});
window.setTimeout(closeDownloadModal, 480);
}
function markSingleFileDownloadFailed(err, selectedItem) {
downloadProgressState.active = false;
updateDownloadModalDisplay({
active: false,
targetText: "File download failed",
currentFileText: `File: ${selectedItem.path}`,
countText: "Direct file download",
statusText: `Failed: ${err.message || "Download failed"}`,
percent: 0,
cancelVisible: false,
});
}
function updateZipDownloadTaskProgress(task) {
if (!downloadProgressState.active) {
return;
@@ -572,6 +625,7 @@ function closeDownloadModal() {
if (downloadProgressState.active) {
return;
}
downloadProgressState.mode = null;
downloadProgressState.archiveLabel = "";
downloadProgressState.totalItems = 0;
downloadProgressState.requestKey = null;
@@ -743,17 +797,20 @@ async function startDownloadSelected() {
}
const zipDownload = isZipDownloadSelection(selectedItems);
const selectedPaths = selectedItems.map((item) => item.path);
const requestKey = zipDownloadRequestKey(selectedPaths);
if (zipDownload && downloadProgressState.active && downloadProgressState.requestKey === requestKey) {
setStatus("Preparing download...");
const selected = selectedItems[0];
const requestKey = zipDownload ? zipDownloadRequestKey(selectedPaths) : singleFileDownloadRequestKey(selected.path);
if (downloadProgressState.active && downloadProgressState.requestKey === requestKey) {
setStatus(zipDownload ? "Preparing download..." : "Requesting download...");
return;
}
if (zipDownload) {
openZipDownloadModal(selectedItems);
setStatus("Preparing download...");
} else {
openSingleFileDownloadModal(selected);
setStatus("Requesting download...");
}
try {
const selected = selectedItems[0];
if (zipDownload) {
const created = await createArchiveDownloadTask(selectedPaths);
downloadProgressState.taskId = created.task_id;
@@ -778,7 +835,8 @@ async function startDownloadSelected() {
anchor.click();
anchor.remove();
URL.revokeObjectURL(url);
setStatus(`Download started: ${anchor.download}`);
markSingleFileDownloadRequested(anchor.download, selected.path);
setStatus(`Download requested: ${anchor.download}`);
} catch (err) {
if (zipDownload) {
if (err.code === "download_cancelled") {
@@ -789,6 +847,7 @@ async function startDownloadSelected() {
setStatus("Download failed");
}
} else {
markSingleFileDownloadFailed(err, selected);
setActionError("Download", err);
}
}