[bug] testData

This commit is contained in:
NicolasCantu 2025-02-06 12:08:40 +01:00
parent d8948a9776
commit 5be3395154
2 changed files with 22 additions and 26 deletions

View File

@ -6,7 +6,7 @@ import { initWebsocket, sendMessage } from '../websockets';
import { ApiReturn, Device, HandshakeMessage, Member, Process, RoleDefinition, SecretsStore, UserDiff } from '../../pkg/sdk_client'; import { ApiReturn, Device, HandshakeMessage, Member, Process, RoleDefinition, SecretsStore, UserDiff } from '../../pkg/sdk_client';
import ModalService from './modal.service'; import ModalService from './modal.service';
import Database from './database.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'; import { BackUp } from '~/models/backup.model';
export const U32_MAX = 4294967295; 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 // We already have this value, so we check if it's on storage and push it if not
const onStorage = this.testDataInStorage(hash); const dataIsOnServers: Record<string, boolean | null> = await this.testDataInStorage(hash);
if (onStorage === null) { if (dataIsOnServers === null) {
console.error('Failed to test data presence in storage'); console.error('Failed to test data presence in storage');
continue; continue;
} }
if (!onStorage) { for (const [server, status] of Object.entries(dataIsOnServers)) {
// We push the encrypted data on storage with default ttl if (status === false) {
// We need to take the encrypted data from the state const cipher = await this.getCipherForDiff(diff);
const cipher = await getCipherForDiff(diff); if (cipher) {
if (cipher) { try {
try { await this.saveDataToStorage(hash, cipher, null);
await this.saveDataToStorage(hash, cipher, null); } catch (e) {
} catch (e) { console.error(`Failed to save to storage: ${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); return await retrieveData(storages, hash);
} }
public async testDataInStorage(hash: string): Promise<boolean | null> { public async testDataInStorage(hash: string): Promise<Record<string, boolean | null> | null> {
const storages = [storageUrl]; const storages = [storageUrl];
return await testData(storages, hash); return await testData(storages, hash);

View File

@ -42,27 +42,25 @@ interface TestResponse {
value: boolean; value: boolean;
} }
export async function testData(servers: string[], key: string): Promise<boolean | null> { export async function testData(servers: string[], key: string): Promise<Record<string, boolean | null> | null> {
const res = {};
for (const server of servers) { for (const server of servers) {
res[server] = null;
try { try {
const response = await axios.get(`${server}/test/${key}`); const response = await axios.get(`${server}/test/${key}`);
if (response.status !== 200) { if (response.status !== 200) {
console.error('Test response status', response.status); console.error(`${server}: Test response status: ${response.status}`);
continue; continue;
} }
const data: TestResponse = response.data; const data: TestResponse = response.data;
if (data.value === true) { res[server] = data.value;
return true;
} else {
// Keep looking
continue;
}
} catch (error) { } catch (error) {
console.error('Error retrieving data:', error); console.error('Error retrieving data:', error);
return null;
} }
} }
return null; return res;
} }