Added raw byte array in storage
This commit is contained in:
1 parent
b0fe9f308e
commit
ce7b6ae523
2 files changed
+53
-17
No files matched your search
+26
-11
@@ -2,10 +2,11 @@ import { sha1 } from "https://denopkg.com/chiefbiiko/sha1/mod.ts";
|
||||
|
||||
|
||||
export interface FileService {
|
||||
createFromFullBody(path: string, body: string): Promise<FileMetaData>;
|
||||
createFromFullBody(path: string, body: string|Uint8Array): Promise<FileMetaData>;
|
||||
list(): AsyncIterable<FileMetaData>;
|
||||
deleteByPath(path: string): Promise<void>;
|
||||
getFullBodyByPath(path: string): Promise<string|null>;
|
||||
getRawBodyByPath(path: string): Promise<Uint8Array|null>;
|
||||
getMetaDataByID(id: string): Promise<FileMetaData| null>;
|
||||
renameById(id: string, newPath: string): Promise<FileMetaData|null>;
|
||||
renameByPath(originalPath: string, newPath: string): Promise<FileMetaData|null>;
|
||||
@@ -37,18 +38,18 @@ export class DenoKVBlobService {
|
||||
this.blobSize = blobSize;
|
||||
}
|
||||
|
||||
private *splitText(body: string): Generator<string>{
|
||||
private *splitText(body: Uint8Array): Generator<Uint8Array>{
|
||||
let blobCount = 0;
|
||||
let txt = body.substring(blobCount * this.blobSize, (blobCount + 1) * this.blobSize);
|
||||
let txt = body.slice(blobCount * this.blobSize, (blobCount + 1) * this.blobSize);
|
||||
while(txt.length > 0 ){
|
||||
yield txt;
|
||||
blobCount += 1;
|
||||
txt = body.substring(blobCount * this.blobSize, (blobCount + 1) * this.blobSize);
|
||||
txt = body.slice(blobCount * this.blobSize, (blobCount + 1) * this.blobSize);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async persistBlob(index: string, body: string): Promise<void>{
|
||||
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");
|
||||
@@ -58,14 +59,22 @@ export class DenoKVBlobService {
|
||||
}
|
||||
}
|
||||
|
||||
async getBlob(index: string): Promise<string>{
|
||||
let ret = "";
|
||||
// todo: remove
|
||||
private concatUint8Array(arr1: Uint8Array, arr2: Uint8Array): Uint8Array {
|
||||
const mergedArray = new Uint8Array(arr1.length + arr2.length);
|
||||
mergedArray.set(arr1);
|
||||
mergedArray.set(arr2, arr1.length);
|
||||
return mergedArray;
|
||||
}
|
||||
|
||||
async getBlob(index: string): Promise<Uint8Array>{
|
||||
let ret = new Uint8Array();
|
||||
for await(const itm of this.kv.list<string>({prefix: ['blob_index', index]})){
|
||||
const foo = await this.kv.get<string>(['blob', itm.value])
|
||||
const foo = await this.kv.get<Uint8Array>(['blob', itm.value])
|
||||
if(foo.value === null){
|
||||
throw new Error(`Inconsistant blob index for key "${foo.value}", this is very bad.`);
|
||||
}
|
||||
ret += foo.value;
|
||||
ret = this.concatUint8Array(ret, foo.value);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@@ -91,13 +100,14 @@ export class DenoKVFileService implements FileService {
|
||||
this.blobService = new DenoKVBlobService(kv, 500);
|
||||
}
|
||||
|
||||
async createFromFullBody(path: string, body: string): Promise<FileMetaData> {
|
||||
async createFromFullBody(path: string, body: string|Uint8Array): Promise<FileMetaData> {
|
||||
const newId = crypto.randomUUID();
|
||||
const serializedFile: SerializedFile = {id: newId, path, bodySize: body.length};
|
||||
const idx: SerializedFilePathIndex = {fileId: newId};
|
||||
await this.kv.set(['file', newId], serializedFile);
|
||||
await this.kv.set(['file_index_path', path], idx);
|
||||
await this.blobService.persistBlob(newId, body);
|
||||
const bodyBytes: Uint8Array = body instanceof Uint8Array ? body: new TextEncoder().encode(body);
|
||||
await this.blobService.persistBlob(newId, bodyBytes);
|
||||
return {id: newId, path, bodySize: body.length};
|
||||
}
|
||||
|
||||
@@ -124,6 +134,11 @@ export class DenoKVFileService implements FileService {
|
||||
}
|
||||
|
||||
async getFullBodyByPath(path: string): Promise<string|null> {
|
||||
const ret = await this.getRawBodyByPath(path);
|
||||
return ret == null ? null : new TextDecoder().decode(ret);
|
||||
}
|
||||
|
||||
async getRawBodyByPath(path: string): Promise<Uint8Array|null> {
|
||||
const idx = await this.kv.get<SerializedFilePathIndex>(["file_index_path", path]);
|
||||
if(idx.value === null){
|
||||
return null;
|
||||
|
||||
Reference in new issue
Block a user