Skip to content

Instantly share code, notes, and snippets.

@thomaskonrad
Last active April 29, 2023 11:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomaskonrad/8ced18322ebfcba47d7e8765176eeefb to your computer and use it in GitHub Desktop.
Save thomaskonrad/8ced18322ebfcba47d7e8765176eeefb to your computer and use it in GitHub Desktop.
Split files into chunks of a given size using the FileReader API.
// This source code is taken from Firefox Send (https://github.com/mozilla/send) and slightly modified.
export default async function splitFile(
file: File,
chunkSize: number,
callback: (chunk: Uint8Array, sequenceNumber: number) => void,
transformer?: (chunk: Uint8Array, chunkIndex: number) => any,
) {
const fileSize = file.size;
let offset = 0;
const readChunk = () => {
const r = new FileReader();
const blob = file.slice(offset, chunkSize + offset);
r.onload = readEventHandler;
r.readAsArrayBuffer(blob);
};
const readEventHandler = async (evt: any) => {
if (evt.target.error == null) {
const sequenceNumber = (offset / chunkSize);
offset += evt.target.result.byteLength;
let data = new Uint8Array(evt.target.result);
if (transformer) {
data = await transformer(data, sequenceNumber);
}
await callback(data, sequenceNumber);
} else {
// Read error.
return;
}
if (offset >= fileSize) {
// Done reading file.
return;
}
// Off to the next chunk.
readChunk();
};
// Let's start reading the first block.
readChunk();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment