From 091a8d4bd29548eef0eaf174defa5f532b7e5bd0 Mon Sep 17 00:00:00 2001 From: NicolasCantu Date: Thu, 13 Feb 2025 10:18:41 +0100 Subject: [PATCH] Fix storage requests --- src/services/service.ts | 23 ++++++++--------- src/services/storage.service.ts | 44 +++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/src/services/service.ts b/src/services/service.ts index f1564b7..5ec8aa7 100755 --- a/src/services/service.ts +++ b/src/services/service.ts @@ -11,8 +11,9 @@ import { BackUp } from '~/models/backup.model'; export const U32_MAX = 4294967295; -const storageUrl = `/storage`; -const BOOTSTRAPURL = [`https://demo.4nkweb.com/ws/`]; +const BASEURL = `https://demo.4nkweb.com`; +const BOOTSTRAPURL = [`${BASEURL}/ws/`]; +const STORAGEURL = `${BASEURL}/storage` const DEFAULTAMOUNT = 1000n; export default class Services { @@ -264,7 +265,7 @@ export default class Services { min_sig_member: 0.0, }, ], - storages: [storageUrl] + storages: [STORAGEURL] }, owner: { members: meAndOne, @@ -275,7 +276,7 @@ export default class Services { min_sig_member: 1.0, }, ], - storages: [storageUrl] + storages: [STORAGEURL] }, users: { members: everyOneElse, @@ -286,7 +287,7 @@ export default class Services { min_sig_member: 0.0, }, ], - storages: [storageUrl] + storages: [STORAGEURL] }, }, }; @@ -313,7 +314,7 @@ export default class Services { min_sig_member: 1.0, }, ], - storages: [storageUrl] + storages: [STORAGEURL] }, }; const pairingTemplate = { @@ -370,7 +371,7 @@ export default class Services { min_sig_member: 0.01, }, ], - storages: [storageUrl] + storages: [STORAGEURL] } } }; @@ -879,7 +880,7 @@ export default class Services { // We check how many copies in storage nodes // We check the storage nodes in the process itself // this.sdkClient.get_storages(commitedIn); - const storages = [storageUrl]; + const storages = [STORAGEURL]; for (const state of process.states) { if (state.state_id === "") { @@ -913,7 +914,7 @@ export default class Services { } public async saveDataToStorage(hash: string, data: string, ttl: number | null) { - const storages = [storageUrl]; + const storages = [STORAGEURL]; try { await storeData(storages, hash, data, ttl); @@ -923,13 +924,13 @@ export default class Services { } public async fetchValueFromStorage(hash: string): Promise { - const storages = [storageUrl]; + const storages = [STORAGEURL]; return await retrieveData(storages, hash); } public async testDataInStorage(hash: string): Promise | null> { - const storages = [storageUrl]; + const storages = [STORAGEURL]; return await testData(storages, hash); } diff --git a/src/services/storage.service.ts b/src/services/storage.service.ts index ee3938a..d8ca20a 100644 --- a/src/services/storage.service.ts +++ b/src/services/storage.service.ts @@ -1,23 +1,35 @@ import axios, { AxiosResponse } from 'axios'; export async function storeData(servers: string[], key: string, value: any, ttl: number | null): Promise { - for (const server of servers) { - try { - const response = await axios.post(`${server}/store`, { key, value, ttl }); - console.log('Data stored successfully:', key); - if (response.status !== 200) { - console.error('Received response status', response.status); - continue; - } - return response; - } catch (error) { - if (error?.response?.status === 409) { - return null; - } - console.error('Error storing data:', error); - } + for (const server of servers) { + try { + // Append key and ttl as query parameters + const url = new URL(`${server}/store`); + url.searchParams.append('key', key); + if (ttl !== null) { + url.searchParams.append('ttl', ttl.toString()); + } + + // Send the encrypted Blob as the raw request body. + const response = await axios.post(url.toString(), value, { + headers: { + 'Content-Type': 'application/octet-stream' + }, + }); + console.log('Data stored successfully:', key); + if (response.status !== 200) { + console.error('Received response status', response.status); + continue; + } + return response; + } catch (error) { + if (error?.response?.status === 409) { + return null; + } + console.error('Error storing data:', error); } - return null; + } + return null; } export async function retrieveData(servers: string[], key: string): Promise {