diff --git a/src/services/service.ts b/src/services/service.ts index dc9c1e5..b988bb4 100755 --- a/src/services/service.ts +++ b/src/services/service.ts @@ -6,7 +6,7 @@ import { initWebsocket, sendMessage } from '../websockets'; import { ApiReturn, Device, HandshakeMessage, Member, Process, RoleDefinition, SecretsStore, UserDiff } from '../../pkg/sdk_client'; import ModalService from './modal.service'; import Database from './database.service'; -import { storeData, retrieveData } from './storage.service'; +import { storeData, retrieveData, testData } from './storage.service'; import { BackUp } from '~/models/backup.model'; export const U32_MAX = 4294967295; @@ -582,26 +582,24 @@ export default class Services { } // We already have this value, so we check if it's on storage and push it if not - const onStorage = this.testDataInStorage(hash); - if (onStorage === null) { + const dataIsOnServers: Record = await this.testDataInStorage(hash); + if (dataIsOnServers === null) { console.error('Failed to test data presence in storage'); continue; } - if (!onStorage) { - // We push the encrypted data on storage with default ttl - // We need to take the encrypted data from the state - const cipher = await getCipherForDiff(diff); - if (cipher) { - try { - await this.saveDataToStorage(hash, cipher, null); - } catch (e) { - console.error(`Failed to save to storage: ${e}`); + for (const [server, status] of Object.entries(dataIsOnServers)) { + if (status === false) { + const cipher = await this.getCipherForDiff(diff); + if (cipher) { + try { + await this.saveDataToStorage(hash, cipher, null); + } catch (e) { + console.error(`Failed to save to storage: ${e}`); + } + } else { + console.error('Failed to get cipher for diff'); } } - } else { - // We could pump the ttl here - // for now, do nothing - continue; } } } @@ -935,7 +933,7 @@ export default class Services { return await retrieveData(storages, hash); } - public async testDataInStorage(hash: string): Promise { + public async testDataInStorage(hash: string): Promise | null> { const storages = [storageUrl]; return await testData(storages, hash); diff --git a/src/services/storage.service.ts b/src/services/storage.service.ts index 06daae8..ee3938a 100644 --- a/src/services/storage.service.ts +++ b/src/services/storage.service.ts @@ -42,27 +42,25 @@ interface TestResponse { value: boolean; } -export async function testData(servers: string[], key: string): Promise { +export async function testData(servers: string[], key: string): Promise | null> { + const res = {}; for (const server of servers) { + res[server] = null; try { const response = await axios.get(`${server}/test/${key}`); if (response.status !== 200) { - console.error('Test response status', response.status); + console.error(`${server}: Test response status: ${response.status}`); continue; } const data: TestResponse = response.data; - if (data.value === true) { - return true; - } else { - // Keep looking - continue; - } + res[server] = data.value; } catch (error) { console.error('Error retrieving data:', error); + return null; } } - return null; + return res; }