Last active
April 29, 2023 11:24
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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