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);
|
||||
|
||||
for (let i = 0; i < upload_urls.length; i++) {
|
||||
const reader = files[i].stream().getReader();
|
||||
|
||||
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;
|
||||
const increaseProgress = (e: ProgressEvent) => {
|
||||
bytesRead += e.loaded;
|
||||
setFileProgress(Math.floor((bytesRead / totalSize) * 100));
|
||||
},
|
||||
})
|
||||
: files[i],
|
||||
// @ts-expect-error
|
||||
duplex: supportsRequestStreams ? "half" : undefined,
|
||||
headers: {
|
||||
"content-type":
|
||||
(files[i].name.split(".").at(-1) as string).toLowerCase() ===
|
||||
"mov"
|
||||
};
|
||||
|
||||
for (let i = 0; i < upload_urls.length; i++) {
|
||||
const xhr = new XMLHttpRequest();
|
||||
|
||||
xhr.open("PUT", upload_urls[i], false);
|
||||
xhr.setRequestHeader(
|
||||
"content-type",
|
||||
(files[i].name.split(".").at(-1) as string).toLowerCase() === "mov"
|
||||
? "video/mp4"
|
||||
: files[i].type ||
|
||||
fileTypes[files[i].name.split(".").at(-1) as string],
|
||||
},
|
||||
method: "PUT",
|
||||
});
|
||||
);
|
||||
|
||||
if (!uploadReq.ok) {
|
||||
xhr.upload.addEventListener("progress", increaseProgress);
|
||||
xhr.upload.onabort = () => {
|
||||
shouldRecall = true;
|
||||
break;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
};
|
||||
xhr.upload.onerror = () => {
|
||||
shouldRecall = true;
|
||||
};
|
||||
|
||||
shouldRecall = true;
|
||||
break;
|
||||
}
|
||||
xhr.send(files[i]);
|
||||
}
|
||||
|
||||
if (shouldRecall) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user