Added support for Reader in blob store.

This commit is contained in:
didier amyot committed 2023-06-10 09:30:58 -04:00
1 parent 8ce2dadc47
commit 459dc26b4f
5 files changed
+57 -11

No files matched your search

+21 -3
View File
@@ -1,4 +1,6 @@
import { sha1 } from 'https://denopkg.com/chiefbiiko/sha1/mod.ts';
import { iterateReader } from "https://deno.land/std@0.191.0/streams/iterate_reader.ts";
export interface FileService {
createFromFullBody(
@@ -58,16 +60,32 @@ export class DenoKVBlobService {
}
}
private async unsafePersistBlob(mainIndex: string, index: number, body: Uint8Array): Promise<void> {
if(body.length > this.blobSize){
throw new Error('Persisted a too large blob');
}
const blob_id = sha1(body, 'utf8', 'hex'); // todo, should not be utf8
await this.kv.set(['blob_index', mainIndex, index], blob_id);
await this.kv.set(['blob', blob_id], body);
}
async persistBlob(index: string, body: Uint8Array): Promise<void> {
let blobCount = 0;
for (const txt of this.splitText(body)) {
const blob_id = sha1(txt, 'utf8', 'hex');
await this.kv.set(['blob_index', index, blobCount], blob_id);
await this.kv.set(['blob', blob_id], txt);
await this.unsafePersistBlob(index, blobCount, txt);
blobCount += 1;
}
}
async persistReader(index: string, body: Deno.Reader): Promise<void>{
let i = 0;
for await (const chunk of iterateReader(body, {bufSize: this.blobSize})){
this.unsafePersistBlob(index, i, chunk);
i++;
}
}
// todo: remove
private concatUint8Array(arr1: Uint8Array, arr2: Uint8Array): Uint8Array {
const mergedArray = new Uint8Array(arr1.length + arr2.length);