Move file uploads to xhr as r2 does not support http2
This commit is contained in:
parent
30b384546d
commit
2411714bce
@ -185,53 +185,32 @@ export default function () {
|
|||||||
|
|
||||||
setUploading(true);
|
setUploading(true);
|
||||||
|
|
||||||
for (let i = 0; i < upload_urls.length; i++) {
|
const increaseProgress = (e: ProgressEvent) => {
|
||||||
const reader = files[i].stream().getReader();
|
bytesRead += e.loaded;
|
||||||
|
|
||||||
try {
|
|
||||||
const uploadReq = await fetch(upload_urls[i], {
|
|
||||||
body: supportsRequestStreams
|
|
||||||
? new ReadableStream({
|
|
||||||
async pull(controller) {
|
|
||||||
const chunk = await reader.read();
|
|
||||||
|
|
||||||
if (chunk.done) {
|
|
||||||
controller.close();
|
|
||||||
|
|
||||||
if (i === upload_urls.length - 1) setUploading(false);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
controller.enqueue(chunk.value);
|
|
||||||
bytesRead += chunk.value.length;
|
|
||||||
setFileProgress(Math.floor((bytesRead / totalSize) * 100));
|
setFileProgress(Math.floor((bytesRead / totalSize) * 100));
|
||||||
},
|
};
|
||||||
})
|
|
||||||
: files[i],
|
for (let i = 0; i < upload_urls.length; i++) {
|
||||||
// @ts-expect-error
|
const xhr = new XMLHttpRequest();
|
||||||
duplex: supportsRequestStreams ? "half" : undefined,
|
|
||||||
headers: {
|
xhr.open("PUT", upload_urls[i], false);
|
||||||
"content-type":
|
xhr.setRequestHeader(
|
||||||
(files[i].name.split(".").at(-1) as string).toLowerCase() ===
|
"content-type",
|
||||||
"mov"
|
(files[i].name.split(".").at(-1) as string).toLowerCase() === "mov"
|
||||||
? "video/mp4"
|
? "video/mp4"
|
||||||
: files[i].type ||
|
: files[i].type ||
|
||||||
fileTypes[files[i].name.split(".").at(-1) as string],
|
fileTypes[files[i].name.split(".").at(-1) as string],
|
||||||
},
|
);
|
||||||
method: "PUT",
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!uploadReq.ok) {
|
xhr.upload.addEventListener("progress", increaseProgress);
|
||||||
|
xhr.upload.onabort = () => {
|
||||||
shouldRecall = true;
|
shouldRecall = true;
|
||||||
break;
|
};
|
||||||
}
|
xhr.upload.onerror = () => {
|
||||||
} catch (e) {
|
shouldRecall = true;
|
||||||
console.error(e);
|
};
|
||||||
|
|
||||||
shouldRecall = true;
|
xhr.send(files[i]);
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shouldRecall) {
|
if (shouldRecall) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user