Replace blobs with buffers
This commit is contained in:
parent
6569686634
commit
80dc42bbe6
@ -985,31 +985,31 @@ export class Service {
|
||||
}
|
||||
|
||||
// Blob and data storage methods
|
||||
async saveBlobToDb(hash: string, data: Blob) {
|
||||
async saveBufferToDb(hash: string, data: Buffer) {
|
||||
const db = await Database.getInstance();
|
||||
try {
|
||||
await db.addObject({
|
||||
storeName: 'data',
|
||||
object: data,
|
||||
key: hash,
|
||||
});
|
||||
}, true);
|
||||
} catch (e) {
|
||||
console.error(`Failed to save data to db: ${e}`);
|
||||
}
|
||||
}
|
||||
|
||||
async getBlobFromDb(hash: string): Promise<Blob | null> {
|
||||
async getBufferFromDb(hash: string): Promise<Buffer | null> {
|
||||
const db = await Database.getInstance();
|
||||
try {
|
||||
return await db.getObject('data', hash);
|
||||
return await db.getObject('data', hash, true);
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async saveDataToStorage(hash: string, data: Blob, ttl: number | null) {
|
||||
async saveDataToStorage(hash: string, data: Buffer, ttl: number | null) {
|
||||
console.log('💾 Saving data to storage:', hash);
|
||||
// TODO: Implement actual storage service
|
||||
console.debug('TODO');
|
||||
// const storages = [STORAGEURL];
|
||||
// try {
|
||||
// await storeData(storages, hash, data, ttl);
|
||||
@ -1050,6 +1050,10 @@ export class Service {
|
||||
return uint8Array;
|
||||
}
|
||||
|
||||
hexToBuffer(hexString: string): Buffer {
|
||||
return Buffer.from(this.hexToUInt8Array(hexString));
|
||||
}
|
||||
|
||||
public async handleApiReturn(apiReturn: ApiReturn) {
|
||||
// Check for errors in the returned objects
|
||||
if (apiReturn.new_tx_to_send && apiReturn.new_tx_to_send.error) {
|
||||
@ -1114,9 +1118,9 @@ export class Service {
|
||||
|
||||
if (updatedProcess.encrypted_data && Object.keys(updatedProcess.encrypted_data).length != 0) {
|
||||
for (const [hash, cipher] of Object.entries(updatedProcess.encrypted_data)) {
|
||||
const blob = this.hexToBlob(cipher);
|
||||
const buffer = this.hexToBuffer(cipher);
|
||||
try {
|
||||
await this.saveBlobToDb(hash, blob);
|
||||
await this.saveBufferToDb(hash, buffer);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
@ -1137,9 +1141,9 @@ export class Service {
|
||||
|
||||
if (apiReturn.push_to_storage && apiReturn.push_to_storage.length != 0) {
|
||||
for (const hash of apiReturn.push_to_storage) {
|
||||
const blob = await this.getBlobFromDb(hash);
|
||||
if (blob) {
|
||||
await this.saveDataToStorage(hash, blob, null);
|
||||
const buffer = await this.getBufferFromDb(hash);
|
||||
if (buffer) {
|
||||
await this.saveDataToStorage(hash, buffer, null);
|
||||
} else {
|
||||
console.error('Failed to get data from db');
|
||||
}
|
||||
@ -1240,11 +1244,10 @@ export class Service {
|
||||
}
|
||||
|
||||
if (hash && key) {
|
||||
const blob = await this.getBlobFromDb(hash);
|
||||
if (blob) {
|
||||
const buffer = await this.getBufferFromDb(hash);
|
||||
if (buffer) {
|
||||
// Decrypt the data
|
||||
const buf = await blob.arrayBuffer();
|
||||
const cipher = new Uint8Array(buf);
|
||||
const cipher = new Uint8Array(buffer);
|
||||
|
||||
const keyUIntArray = this.hexToUInt8Array(key);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user