refactored uploadFiles()

This commit is contained in:
2025-08-30 00:45:12 +02:00
parent 9d1d2b3299
commit f2f6f0f24c

View File

@@ -188,6 +188,17 @@
return link;
}
function finishUpload(success) {
UI.overallProgressContainer.style.display = "none";
UI.overallProgress.value = 0;
UI.overallStatus.textContent = "";
UI.currentFileName.textContent = "";
fetchFiles();
if (success) {
showSuccess("Upload successful");
}
}
function getUIElements() {
UI.currentFileName = document.getElementById("currentFileName");
UI.dropzone = document.getElementById("dropzone");
@@ -219,6 +230,13 @@
return (bytesPerSec / (1024 * 1024)).toFixed(2) + " MB/s";
}
function initUIProgress() {
UI.overallProgressContainer.style.display = "block";
UI.overallProgress.value = 0;
UI.overallStatus.textContent = "";
UI.currentFileName.textContent = "";
}
function renderFileList(files) {
if (!UI.fileList) return;
@@ -324,43 +342,23 @@
const files = Array.from(fileListLike);
if (files.length === 0) return;
for (const f of files) {
if (sanitizeFilename(f.name) == ".upload") {
showError("Invalid filename: .upload");
return;
}
if (sanitizeFilename(f.name) in Files) {
showError("File already exists: " + f.name);
return;
}
}
if (!validateFiles(files)) return;
let noErrorOccurred = true;
UI.overallProgressContainer.style.display = "block";
UI.overallProgress.value = 0;
UI.overallStatus.textContent = "";
UI.currentFileName.textContent = "";
initUIProgress();
const totalSize = files.reduce((sum, f) => sum + f.size, 0);
let uploadedBytes = 0;
const t0 = Date.now();
let idx = 0;
let currentIndex = 0;
const startTime = Date.now();
let allSuccessful = true;
const uploadNext = () => {
if (idx >= files.length) {
UI.overallProgressContainer.style.display = "none";
UI.overallProgress.value = 0;
UI.overallStatus.textContent = "";
UI.currentFileName.textContent = "";
fetchFiles();
if (noErrorOccurred) {
showSuccess("Upload successful");
}
function uploadNext() {
if (currentIndex >= files.length) {
finishUpload(allSuccessful);
return;
}
const file = files[idx];
const file = files[currentIndex];
UI.currentFileName.textContent = file.name;
const xhr = new XMLHttpRequest();
@@ -368,13 +366,45 @@
form.append("uploadfile", file);
xhr.upload.addEventListener("progress", (e) => {
if (!e.lengthComputable) return;
if (e.lengthComputable) {
updateProgressUI(uploadedBytes + e.loaded, totalSize, startTime);
}
});
const totalUploaded = uploadedBytes + e.loaded;
xhr.addEventListener("load", () => {
if (xhr.status === 200) {
uploadedBytes += file.size;
} else if (xhr.status === 409) {
showError("File already exists: " + file.name);
allSuccessful = false;
} else {
showError("Upload failed: " + file.name);
allSuccessful = false;
}
currentIndex++;
uploadNext();
});
xhr.addEventListener("error", () => {
showError("Network or server error during upload.");
allSuccessful = false;
currentIndex++;
uploadNext();
});
xhr.open("POST", AppConfig.Endpoints.Upload);
xhr.send(form);
}
fetchFiles();
uploadNext();
}
function updateProgressUI(totalUploaded, totalSize, startTime) {
const percent = (totalUploaded / totalSize) * 100;
UI.overallProgress.value = percent;
const elapsed = (Date.now() - t0) / 1000;
const elapsed = (Date.now() - startTime) / 1000;
const speed = totalUploaded / elapsed;
const speedStr = humanReadableSpeed(speed);
@@ -390,37 +420,21 @@
`Speed: ${speedStr}, Est. time left: ${
isFinite(etaSec) ? `${min}m ${sec}s` : "calculating…"
}`;
});
}
xhr.addEventListener("load", () => {
if (xhr.status === 200) {
uploadedBytes += file.size;
} else {
if (xhr.status === 409) {
showError("File already exists: " + file.name);
noErrorOccurred = false;
} else {
showError("Upload failed: " + file.name);
noErrorOccurred = false;
function validateFiles(files) {
for (const f of files) {
const safeName = sanitizeFilename(f.name);
if (safeName === ".upload") {
showError("Invalid filename: .upload");
return false;
}
if (safeName in Files) {
showError("File already exists: " + f.name);
return false;
}
}
idx++;
uploadNext();
});
xhr.addEventListener("error", () => {
showError("Network or server error during upload.");
noErrorOccurred = false;
idx++;
uploadNext();
});
xhr.open("POST", AppConfig.Endpoints.Upload);
xhr.send(form);
};
fetchFiles();
uploadNext();
return true;
}
document.addEventListener("DOMContentLoaded", initApp);