86 lines
3.2 KiB
JavaScript
86 lines
3.2 KiB
JavaScript
|
|
async function startUpload() {
|
||
|
|
if (!uploadsEnabled) {
|
||
|
|
showToast("Guest uploads are disabled.", "warning");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (uploadLocked) {
|
||
|
|
showToast("Upload already started. Press Clear to create another box.", "warning");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (!files.length) {
|
||
|
|
showWarningDialog("No files selected", "There are no files selected. Please select files to upload.");
|
||
|
|
showToast("No files selected. Please select files to upload.", "warning");
|
||
|
|
setStatus("No files selected");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (hasQuotaError()) {
|
||
|
|
showWarningDialog("Over maximum upload size", quotaWarningMessage() || "Over maximum upload size.");
|
||
|
|
showToast("Over maximum upload size.", "error");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
uploadLocked = true;
|
||
|
|
setBoxOptionsLocked(true);
|
||
|
|
if (el.fileInput) el.fileInput.disabled = true;
|
||
|
|
el.dropzone?.classList.add("is-locked");
|
||
|
|
setShareUrl("");
|
||
|
|
files.forEach((item) => {
|
||
|
|
item.loaded = 0;
|
||
|
|
item.uploaded = false;
|
||
|
|
item.failed = false;
|
||
|
|
item.error = "";
|
||
|
|
});
|
||
|
|
completedImpactKeys = new Set();
|
||
|
|
overallImpactDone = false;
|
||
|
|
renderFiles();
|
||
|
|
|
||
|
|
let completedCount = 0;
|
||
|
|
const totalCount = files.length;
|
||
|
|
const statusPrefix = () => `${completedCount}/${totalCount}`;
|
||
|
|
setStatus(`${statusPrefix()} Uploading.`);
|
||
|
|
animateUploadStatus(statusPrefix);
|
||
|
|
|
||
|
|
try {
|
||
|
|
const box = await createBox();
|
||
|
|
setShareUrl(box.box_url);
|
||
|
|
files.forEach((item, index) => {
|
||
|
|
item.boxID = box.box_id;
|
||
|
|
item.boxFile = box.files[index];
|
||
|
|
item.displayName = item.boxFile?.name || item.displayName;
|
||
|
|
const icon = item.row?.querySelector(".upload-file-icon");
|
||
|
|
if (icon && item.boxFile?.thumbnail_path) {
|
||
|
|
item.row.classList.add("has-thumbnail");
|
||
|
|
icon.src = item.boxFile.thumbnail_path;
|
||
|
|
} else if (icon && item.boxFile?.icon_path && !item.previewURL) {
|
||
|
|
icon.src = item.boxFile.icon_path;
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
const results = await Promise.allSettled(files.map((item) => uploadFile(item, () => { completedCount += 1; })));
|
||
|
|
stopStatusAnimation();
|
||
|
|
|
||
|
|
const failedCount = results.filter((result) => result.status === "rejected").length;
|
||
|
|
if (failedCount > 0) {
|
||
|
|
setStatus(`${completedCount}/${totalCount} uploaded, ${failedCount} failed`);
|
||
|
|
showToast(`${failedCount} file${failedCount === 1 ? "" : "s"} failed. The share URL contains the successful files.`, "error");
|
||
|
|
renderFiles();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
setOverallProgress(100);
|
||
|
|
setStatus(`${completedCount}/${totalCount} uploaded. Share URL created. Press Clear to start another upload.`);
|
||
|
|
showToast("Upload complete. Share URL created.");
|
||
|
|
renderFiles();
|
||
|
|
} catch (error) {
|
||
|
|
stopStatusAnimation();
|
||
|
|
uploadLocked = false;
|
||
|
|
setBoxOptionsLocked(false);
|
||
|
|
if (el.fileInput) el.fileInput.disabled = !uploadsEnabled;
|
||
|
|
el.dropzone?.classList.remove("is-locked");
|
||
|
|
setShareUrl("");
|
||
|
|
setStatus(error.message || "Upload failed");
|
||
|
|
showToast(error.message || "Upload failed", "error");
|
||
|
|
renderFiles();
|
||
|
|
}
|
||
|
|
}
|