Adapt storage to new api
This commit is contained in:
parent
c422881cd1
commit
bfca596e8b
@ -1076,7 +1076,7 @@ export default class Services {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async fetchValueFromStorage(hash: string): Promise<any | null> {
|
public async fetchValueFromStorage(hash: string): Promise<ArrayBuffer | null> {
|
||||||
const storages = [STORAGEURL];
|
const storages = [STORAGEURL];
|
||||||
|
|
||||||
return await retrieveData(storages, hash);
|
return await retrieveData(storages, hash);
|
||||||
|
@ -3,15 +3,26 @@ import axios, { AxiosResponse } from 'axios';
|
|||||||
export async function storeData(servers: string[], key: string, value: Blob, ttl: number | null): Promise<AxiosResponse | null> {
|
export async function storeData(servers: string[], key: string, value: Blob, ttl: number | null): Promise<AxiosResponse | null> {
|
||||||
for (const server of servers) {
|
for (const server of servers) {
|
||||||
try {
|
try {
|
||||||
// Append key and ttl as query parameters
|
// Handle relative paths (for development proxy) vs absolute URLs (for production)
|
||||||
const url = new URL(`${server}/store`);
|
let url: string;
|
||||||
url.searchParams.append('key', key);
|
if (server.startsWith('/')) {
|
||||||
if (ttl !== null) {
|
// Relative path - construct manually for proxy
|
||||||
url.searchParams.append('ttl', ttl.toString());
|
url = `${server}/store?key=${encodeURIComponent(key)}`;
|
||||||
|
if (ttl !== null) {
|
||||||
|
url += `&ttl=${ttl}`;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Absolute URL - use URL constructor
|
||||||
|
const urlObj = new URL(`${server}/store`);
|
||||||
|
urlObj.searchParams.append('key', key);
|
||||||
|
if (ttl !== null) {
|
||||||
|
urlObj.searchParams.append('ttl', ttl.toString());
|
||||||
|
}
|
||||||
|
url = urlObj.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send the encrypted ArrayBuffer as the raw request body.
|
// Send the encrypted ArrayBuffer as the raw request body.
|
||||||
const response = await axios.post(url.toString(), value, {
|
const response = await axios.post(url, value, {
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/octet-stream'
|
'Content-Type': 'application/octet-stream'
|
||||||
},
|
},
|
||||||
@ -35,21 +46,48 @@ export async function storeData(servers: string[], key: string, value: Blob, ttl
|
|||||||
export async function retrieveData(servers: string[], key: string): Promise<ArrayBuffer | null> {
|
export async function retrieveData(servers: string[], key: string): Promise<ArrayBuffer | null> {
|
||||||
for (const server of servers) {
|
for (const server of servers) {
|
||||||
try {
|
try {
|
||||||
|
// Handle relative paths (for development proxy) vs absolute URLs (for production)
|
||||||
|
const url = server.startsWith('/')
|
||||||
|
? `${server}/retrieve/${key}` // Relative path - use as-is for proxy
|
||||||
|
: new URL(`${server}/retrieve/${key}`).toString(); // Absolute URL - construct properly
|
||||||
|
|
||||||
|
console.log('Retrieving data', key,' from:', url);
|
||||||
// When fetching the data from the server:
|
// When fetching the data from the server:
|
||||||
const response = await axios.get(`${server}/retrieve/${key}`, {
|
const response = await axios.get(url, {
|
||||||
responseType: 'arraybuffer'
|
responseType: 'arraybuffer'
|
||||||
});
|
});
|
||||||
if (response.status !== 200) {
|
|
||||||
console.error('Received response status', response.status);
|
if (response.status === 200) {
|
||||||
|
// Validate that we received an ArrayBuffer
|
||||||
|
if (response.data instanceof ArrayBuffer) {
|
||||||
|
return response.data;
|
||||||
|
} else {
|
||||||
|
console.error('Server returned non-ArrayBuffer data:', typeof response.data);
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.error(`Server ${server} returned status ${response.status}`);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
// console.log('Retrieved data:', response.data);
|
|
||||||
return response.data;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error retrieving data:', error);
|
if (axios.isAxiosError(error)) {
|
||||||
|
if (error.response?.status === 404) {
|
||||||
|
console.log(`Data not found on server ${server} for key ${key}`);
|
||||||
|
continue; // Try next server
|
||||||
|
} else if (error.response?.status) {
|
||||||
|
console.error(`Server ${server} error ${error.response.status}:`, error.response.statusText);
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
console.error(`Network error connecting to ${server}:`, error.message);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.error(`Unexpected error retrieving data from ${server}:`, error);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface TestResponse {
|
interface TestResponse {
|
||||||
@ -62,7 +100,12 @@ export async function testData(servers: string[], key: string): Promise<Record<s
|
|||||||
for (const server of servers) {
|
for (const server of servers) {
|
||||||
res[server] = null;
|
res[server] = null;
|
||||||
try {
|
try {
|
||||||
const response = await axios.get(`${server}/test/${key}`);
|
// Handle relative paths (for development proxy) vs absolute URLs (for production)
|
||||||
|
const url = server.startsWith('/')
|
||||||
|
? `${server}/test/${key}` // Relative path - use as-is for proxy
|
||||||
|
: new URL(`${server}/test/${key}`).toString(); // Absolute URL - construct properly
|
||||||
|
|
||||||
|
const response = await axios.get(url);
|
||||||
if (response.status !== 200) {
|
if (response.status !== 200) {
|
||||||
console.error(`${server}: Test response status: ${response.status}`);
|
console.error(`${server}: Test response status: ${response.status}`);
|
||||||
continue;
|
continue;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user