fix uploading for linux?
This commit is contained in:
@@ -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,
|
|
||||||
};
|
|
||||||
|
|
||||||
let uploadRes;
|
// Keep request-level timeout high for large WAN uploads.
|
||||||
try {
|
xhr.timeout = 30 * 60 * 1000;
|
||||||
uploadRes = await fetch(uploadUrl, {
|
xhr.open("POST", uploadUrl, true);
|
||||||
method: "POST",
|
xhr.withCredentials = true;
|
||||||
...uploadRequestOptions,
|
xhr.responseType = "text";
|
||||||
});
|
xhr.setRequestHeader("Content-Type", "application/octet-stream");
|
||||||
} catch (err) {
|
|
||||||
console.warn("[filebrowser] upload stage POST failed, retrying as PUT", err);
|
|
||||||
uploadRes = await fetch(uploadUrl, {
|
|
||||||
method: "PUT",
|
|
||||||
...uploadRequestOptions,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!uploadRes.ok) {
|
const onAbort = () => {
|
||||||
const text = await uploadRes.text();
|
try {
|
||||||
console.debug("[filebrowser] upload stage failed", {
|
xhr.abort();
|
||||||
status: uploadRes.status,
|
} catch {}
|
||||||
body: text,
|
};
|
||||||
});
|
|
||||||
throw new Error(text || "upload staging failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
transfer.receivedBytes = Math.round(file.size * 0.5);
|
if (transfer.abortController?.signal) {
|
||||||
transfer.sent = transfer.receivedBytes;
|
transfer.abortController.signal.addEventListener("abort", onAbort, { once: true });
|
||||||
transfer.progress = 50;
|
}
|
||||||
updateTransferProgress(transfer.id, transfer.progress, transfer.sent, transfer.total);
|
|
||||||
|
xhr.upload.onprogress = (event) => {
|
||||||
|
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", {
|
||||||
|
status: xhr.status,
|
||||||
|
body: text,
|
||||||
|
});
|
||||||
|
reject(new Error(text || "upload staging failed"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
resolve(text ? JSON.parse(text) : {});
|
||||||
|
} catch {
|
||||||
|
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);
|
||||||
|
|||||||
Reference in New Issue
Block a user