From 7ac80384e01f68d7cd3d044eb2931d3bfd2e3a4c Mon Sep 17 00:00:00 2001 From: thecodrr Date: Fri, 15 Oct 2021 11:14:13 +0500 Subject: [PATCH] feat: add readChunk & toBlob functions to filehandle --- .../web/packages/streamablefs/src/filehandle.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/apps/web/packages/streamablefs/src/filehandle.ts b/apps/web/packages/streamablefs/src/filehandle.ts index 8ec63958c..7f2a720b5 100644 --- a/apps/web/packages/streamablefs/src/filehandle.ts +++ b/apps/web/packages/streamablefs/src/filehandle.ts @@ -37,4 +37,21 @@ export default class FileHandle extends ReadableStream { private getChunkKey(offset: number): string { return `${this.file.filename}-chunk-${offset}`; } + + async readChunk(offset: number): Promise { + const array = await this.storage.getItem( + this.getChunkKey(offset) + ); + return array; + } + + async toBlob() { + let blobParts: BlobPart[] = []; + for (let i = 0; i < this.file.chunks; ++i) { + const array = await this.readChunk(i); + if (!array) continue; + blobParts.push(array.buffer); + } + return new Blob(blobParts, { type: this.file.type }); + } }