Update services/hfService.ts
Browse files- services/hfService.ts +11 -30
services/hfService.ts
CHANGED
|
@@ -1,35 +1,20 @@
|
|
| 1 |
|
| 2 |
-
/**
|
| 3 |
-
* FRONTEND SERVICE
|
| 4 |
-
*
|
| 5 |
-
* This service communicates with the Node.js backend (server.js).
|
| 6 |
-
* It supports Batch Uploading to maximize speed.
|
| 7 |
-
*/
|
| 8 |
-
|
| 9 |
-
// Use relative URL so Vite proxy can forward it to localhost:3001
|
| 10 |
const BACKEND_URL = '/api/upload';
|
| 11 |
|
| 12 |
interface BatchItem {
|
| 13 |
-
id: string;
|
| 14 |
file: File;
|
| 15 |
path: string;
|
| 16 |
}
|
| 17 |
|
| 18 |
-
/**
|
| 19 |
-
* Uploads multiple files in a single request.
|
| 20 |
-
*
|
| 21 |
-
* @param items Array of files and their destination paths
|
| 22 |
-
* @returns Array of public URLs for the uploaded files
|
| 23 |
-
*/
|
| 24 |
export const uploadBatchToHub = async (items: BatchItem[]): Promise<string[]> => {
|
| 25 |
const formData = new FormData();
|
| 26 |
|
| 27 |
-
//
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
});
|
| 33 |
|
| 34 |
try {
|
| 35 |
const response = await fetch(BACKEND_URL, {
|
|
@@ -37,10 +22,11 @@ export const uploadBatchToHub = async (items: BatchItem[]): Promise<string[]> =>
|
|
| 37 |
body: formData,
|
| 38 |
});
|
| 39 |
|
| 40 |
-
// Handle non-JSON responses (like 404 or 502 from proxy)
|
| 41 |
const contentType = response.headers.get("content-type");
|
| 42 |
if (!contentType || !contentType.includes("application/json")) {
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
}
|
| 45 |
|
| 46 |
const data = await response.json();
|
|
@@ -52,12 +38,7 @@ export const uploadBatchToHub = async (items: BatchItem[]): Promise<string[]> =>
|
|
| 52 |
return data.urls;
|
| 53 |
|
| 54 |
} catch (err: any) {
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
if (errorMessage.includes('Failed to fetch') || errorMessage.includes('backend running')) {
|
| 59 |
-
throw new Error("Cannot connect to Backend. Make sure 'npm run server' is running.");
|
| 60 |
-
}
|
| 61 |
-
throw new Error(errorMessage);
|
| 62 |
}
|
| 63 |
};
|
|
|
|
| 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
const BACKEND_URL = '/api/upload';
|
| 3 |
|
| 4 |
interface BatchItem {
|
| 5 |
+
id: string;
|
| 6 |
file: File;
|
| 7 |
path: string;
|
| 8 |
}
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
export const uploadBatchToHub = async (items: BatchItem[]): Promise<string[]> => {
|
| 11 |
const formData = new FormData();
|
| 12 |
|
| 13 |
+
// Optimized append loop
|
| 14 |
+
for (const item of items) {
|
| 15 |
+
formData.append('paths', item.path);
|
| 16 |
+
formData.append('files', item.file);
|
| 17 |
+
}
|
|
|
|
| 18 |
|
| 19 |
try {
|
| 20 |
const response = await fetch(BACKEND_URL, {
|
|
|
|
| 22 |
body: formData,
|
| 23 |
});
|
| 24 |
|
|
|
|
| 25 |
const contentType = response.headers.get("content-type");
|
| 26 |
if (!contentType || !contentType.includes("application/json")) {
|
| 27 |
+
const text = await response.text();
|
| 28 |
+
console.error("Non-JSON Response:", text);
|
| 29 |
+
throw new Error(`Server returned unexpected status ${response.status}. Check server logs.`);
|
| 30 |
}
|
| 31 |
|
| 32 |
const data = await response.json();
|
|
|
|
| 38 |
return data.urls;
|
| 39 |
|
| 40 |
} catch (err: any) {
|
| 41 |
+
// Retry logic could be added here if needed
|
| 42 |
+
throw new Error(err?.message || "Connection failed");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
}
|
| 44 |
};
|