Replace blobs with buffers

This commit is contained in:
Sosthene 2025-09-03 15:10:42 +02:00
parent 6569686634
commit 80dc42bbe6

View File

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