fix uploading for linux?

This commit is contained in:
KingKDot
2026-03-08 03:19:22 -05:00
parent 82e4a5daaf
commit bb39871766
+62 -26
View File
@@ -1630,42 +1630,76 @@ async function uploadFileViaHttpPull(file, path, transfer) {
console.debug("[filebrowser] upload stage url", { uploadUrl }); console.debug("[filebrowser] upload stage url", { uploadUrl });
const uploadRequestOptions = { const uploadData = await new Promise((resolve, reject) => {
headers: { "Content-Type": "application/octet-stream" }, const xhr = new XMLHttpRequest();
credentials: "include", transfer.uploadXhr = xhr;
body: file,
signal: transfer.abortController?.signal, // Keep request-level timeout high for large WAN uploads.
xhr.timeout = 30 * 60 * 1000;
xhr.open("POST", uploadUrl, true);
xhr.withCredentials = true;
xhr.responseType = "text";
xhr.setRequestHeader("Content-Type", "application/octet-stream");
const onAbort = () => {
try {
xhr.abort();
} catch {}
}; };
let uploadRes; if (transfer.abortController?.signal) {
try { transfer.abortController.signal.addEventListener("abort", onAbort, { once: true });
uploadRes = await fetch(uploadUrl, {
method: "POST",
...uploadRequestOptions,
});
} catch (err) {
console.warn("[filebrowser] upload stage POST failed, retrying as PUT", err);
uploadRes = await fetch(uploadUrl, {
method: "PUT",
...uploadRequestOptions,
});
} }
if (!uploadRes.ok) { xhr.upload.onprogress = (event) => {
const text = await uploadRes.text(); const total = event.total || file.size || transfer.total || 0;
if (!total) return;
const loaded = Math.min(event.loaded, total);
// First 50% is browser -> server staging upload.
const stageRatio = loaded / total;
transfer.receivedBytes = loaded;
transfer.sent = loaded;
transfer.progress = Math.max(0, Math.min(50, Math.round(stageRatio * 50)));
updateTransferProgress(transfer.id, transfer.progress, transfer.sent, transfer.total);
};
xhr.onerror = () => {
reject(new Error("upload staging failed"));
};
xhr.ontimeout = () => {
reject(new Error("upload staging timed out"));
};
xhr.onabort = () => {
reject(new Error("Upload cancelled"));
};
xhr.onload = () => {
const text = xhr.responseText || "";
if (xhr.status < 200 || xhr.status >= 300) {
console.debug("[filebrowser] upload stage failed", { console.debug("[filebrowser] upload stage failed", {
status: uploadRes.status, status: xhr.status,
body: text, body: text,
}); });
throw new Error(text || "upload staging failed"); reject(new Error(text || "upload staging failed"));
return;
} }
transfer.receivedBytes = Math.round(file.size * 0.5); try {
transfer.sent = transfer.receivedBytes; resolve(text ? JSON.parse(text) : {});
transfer.progress = 50; } catch {
updateTransferProgress(transfer.id, transfer.progress, transfer.sent, transfer.total); reject(new Error("upload staging failed"));
}
};
try {
xhr.send(file);
} catch (err) {
reject(err instanceof Error ? err : new Error("upload staging failed"));
}
});
const uploadData = await uploadRes.json();
if (!uploadData?.pullUrl) { if (!uploadData?.pullUrl) {
throw new Error("upload staging failed"); throw new Error("upload staging failed");
} }
@@ -1700,6 +1734,7 @@ async function uploadFileViaHttpPull(file, path, transfer) {
transfer.progress = 100; transfer.progress = 100;
transfer.receivedChunks = transfer.expectedChunks; transfer.receivedChunks = transfer.expectedChunks;
updateTransferProgress(transfer.id, transfer.progress, transfer.sent, transfer.total); updateTransferProgress(transfer.id, transfer.progress, transfer.sent, transfer.total);
transfer.uploadXhr = null;
} }
async function uploadFile(file) { async function uploadFile(file) {
@@ -1725,6 +1760,7 @@ async function uploadFile(file) {
transferId, transferId,
completed: false, completed: false,
abortController: new AbortController(), abortController: new AbortController(),
uploadXhr: null,
}; };
fileUploads.set(path, transfer); fileUploads.set(path, transfer);