fix: stabilize mobile uploads and show transfer speed

This commit is contained in:
justme committed 2026-10-03 01:37:43 +08:00
1 parent 09d66092f5
commit c0d155c608
12 files changed
+279 -69

No files matched your search

+28 -1
View File
@@ -134,7 +134,22 @@ async function* chunksOf(source: ContentSource): AsyncGenerator<Uint8Array, void
return;
}
if (typeof Blob !== 'undefined' && source instanceof Blob) {
yield* chunksOf(source.stream());
if (typeof source.stream === 'function') {
yield* chunksOf(source.stream());
return;
}
// Older Safari releases don't expose Blob.stream(). Read bounded slices
// instead of materializing the complete file in memory.
const sliceSize = 256 * 1024;
for (let offset = 0; offset < source.size; offset += sliceSize) {
const part = source.slice(offset, Math.min(offset + sliceSize, source.size));
if (typeof part.arrayBuffer === 'function') {
yield new Uint8Array(await part.arrayBuffer());
} else {
yield await readBlobPart(part);
}
}
return;
}
if (typeof ReadableStream !== 'undefined' && source instanceof ReadableStream) {
@@ -153,6 +168,18 @@ async function* chunksOf(source: ContentSource): AsyncGenerator<Uint8Array, void
yield* source as AsyncIterable<Uint8Array>;
}
function readBlobPart(blob: Blob): Promise<Uint8Array> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => reader.result instanceof ArrayBuffer
? resolve(new Uint8Array(reader.result))
: reject(new TypeError('文件读取失败'));
reader.onerror = () => reject(reader.error ?? new TypeError('文件读取失败'));
reader.onabort = () => reject(new DOMException('文件读取已取消', 'AbortError'));
reader.readAsArrayBuffer(blob);
});
}
interface Chunk {
data: Bytes;
last: boolean;