feat(core): stream flash-transfer uploads from disk

create_flash_task loaded every fileset file through loadBinarySource
and kept the bytes through hash, commit, and sliceupload, so a single
file over 1 GiB could not be sent. Stage to disk like group files,
stream MD5/SHA1/Sha1StateV, then POST 1 MiB chunks from FileChunkSource.

Do not reuse video sha1Blocks as Sha1StateV: an exact N-MiB file would
ship an extra un-finalized last-slice state. Local/HTTP sources use the
4 GiB group-file ceiling; inline base64 stays at 1 GiB because stage
still decodes it in RAM. Protocol order, seconds-pass, and multi-file
commit are unchanged.

Fixes #359
This commit is contained in:
DrSmoothl committed 2026-08-13 18:34:27 +08:00
1 parent e7322e2e72
commit b8b9be83c3
5 files changed
+542 -79

No files matched your search

@@ -95,3 +95,68 @@ export async function hashFileStreaming(
await src.close();
}
}
export interface StreamedFlashHashes {
md5: Uint8Array;
sha1: Uint8Array;
md5Hex: string;
sha1Hex: string;
/** Flash sliceupload Sha1StateV — un-finalized little-endian state after
* every full 1 MiB slice except the last; the last entry is the finalized
* whole-file SHA1. Same shape as `computeSha1StateV`, not `sha1Blocks`. */
sha1StateV: Uint8Array[];
sliceCount: number;
}
/**
* Streaming hash for flash-transfer sliceupload. Equivalent to
* `computeHashes` + `computeSha1StateV(bytes, ceil(size/1MiB), 1MiB)`
* without buffering the file.
*
* Do not substitute `hashFileStreaming().sha1Blocks`: when
* `size % 1 MiB === 0` that list has an extra un-finalized last-slice
* state that flash's Sha1StateV must not carry.
*/
export async function hashFlashFileStreaming(filePath: string): Promise<StreamedFlashHashes> {
const { size } = await fsp.stat(filePath);
const sliceCount = Math.ceil(size / SHA1_STREAM_BLOCK_SIZE);
const src = await FileChunkSource.open(filePath, size);
try {
const md5 = createHash('md5');
const sha1 = createHash('sha1');
const blockSha1 = new Sha1Stream();
const sha1StateV: Uint8Array[] = [];
let offset = 0;
let sliceIndex = 0;
while (offset < size) {
const len = Math.min(SHA1_STREAM_BLOCK_SIZE, size - offset);
const chunk = await src.read(offset, len);
md5.update(chunk);
sha1.update(chunk);
blockSha1.update(chunk);
if (sliceIndex !== sliceCount - 1) {
sha1StateV.push(blockSha1.hash(true));
}
offset += len;
sliceIndex += 1;
}
const sha1Digest = sha1.digest();
if (sliceCount > 0) {
sha1StateV.push(new Uint8Array(sha1Digest));
}
const md5Digest = md5.digest();
return {
md5: new Uint8Array(md5Digest),
sha1: new Uint8Array(sha1Digest),
md5Hex: md5Digest.toString('hex'),
sha1Hex: sha1Digest.toString('hex'),
sha1StateV,
sliceCount,
};
} finally {
await src.close();
}
}
+7
View File
@@ -20,6 +20,13 @@ export interface LoadedBinary {
const DEFAULT_MAX_BINARY_SIZE = 1024 * 1024 * 1024; // 1 GiB
/** Hard ceiling QQ's file protocol supports — used by group/private files. */
export const FILE_UPLOAD_MAX_BYTES = 4 * 1024 * 1024 * 1024; // 4 GiB
/** Local / HTTP flash sources. Official fileset max is unknown; reuse the
* group/private file ceiling so create_flash_task is not stuck on
* `loadBinarySource`'s 1 GiB default. */
export const FLASH_TRANSFER_MAX_BYTES = FILE_UPLOAD_MAX_BYTES;
/** Inline `base64://` / `data:` flash sources still decode into RAM inside
* `stageSourceToDisk`, so they keep the 1 GiB buffered-load ceiling. */
export const FLASH_TRANSFER_INLINE_MAX_BYTES = DEFAULT_MAX_BINARY_SIZE;
const FETCH_TIMEOUT_MS = 60_000;
/**