refactored out fileChunkReaderMaker

This commit is contained in:
Abhinav-grd 2021-08-06 10:46:33 +05:30
parent 5891a19547
commit d83462b3ea

View file

@ -234,20 +234,7 @@ async function generateThumbnail(
} }
function getFileStream(reader: FileReader, file: globalThis.File) { function getFileStream(reader: FileReader, file: globalThis.File) {
const self = this; const fileChunkReader = fileChunkReaderMaker(reader, file);
const fileChunkReader = (async function* fileChunkReaderMaker(
fileSize,
self,
) {
let offset = 0;
while (offset < fileSize) {
const blob = file.slice(offset, ENCRYPTION_CHUNK_SIZE + offset);
const fileChunk = await self.getUint8ArrayView(reader, blob);
yield fileChunk;
offset += ENCRYPTION_CHUNK_SIZE;
}
return null;
})(file.size, self);
return { return {
stream: new ReadableStream<Uint8Array>({ stream: new ReadableStream<Uint8Array>({
async pull(controller: ReadableStreamDefaultController) { async pull(controller: ReadableStreamDefaultController) {
@ -262,6 +249,18 @@ function getFileStream(reader: FileReader, file: globalThis.File) {
chunkCount: Math.ceil(file.size / ENCRYPTION_CHUNK_SIZE), chunkCount: Math.ceil(file.size / ENCRYPTION_CHUNK_SIZE),
}; };
} }
async function* fileChunkReaderMaker(reader:FileReader, file:globalThis.File) {
let offset = 0;
while (offset < file.size) {
const blob = file.slice(offset, ENCRYPTION_CHUNK_SIZE + offset);
const fileChunk = await getUint8ArrayView(reader, blob);
yield fileChunk;
offset += ENCRYPTION_CHUNK_SIZE;
}
return null;
}
async function getUint8ArrayView( async function getUint8ArrayView(
reader: FileReader, reader: FileReader,
file: Blob, file: Blob,