feat: add readChunk & toBlob functions to filehandle

This commit is contained in:
thecodrr
2021-10-15 11:14:13 +05:00
parent 81cae7ea93
commit 7ac80384e0

View File

@@ -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<Uint8Array | null> {
const array = await this.storage.getItem<Uint8Array>(
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 });
}
}