[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 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<string, boolean | null> = 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<boolean | null> {
public async testDataInStorage(hash: string): Promise<Record<string, boolean | null> | null> {
const storages = [storageUrl];
return await testData(storages, hash);

View File

@ -42,27 +42,25 @@ interface TestResponse {
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) {
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;
}