Update services/hfService.ts
Browse files- services/hfService.ts +21 -17
services/hfService.ts
CHANGED
|
@@ -1,31 +1,35 @@
|
|
|
|
|
| 1 |
/**
|
| 2 |
* FRONTEND SERVICE
|
| 3 |
*
|
| 4 |
-
* This service
|
| 5 |
-
* It
|
| 6 |
*/
|
| 7 |
|
| 8 |
// Use relative URL so Vite proxy can forward it to localhost:3001
|
| 9 |
const BACKEND_URL = '/api/upload';
|
| 10 |
|
| 11 |
-
interface
|
|
|
|
| 12 |
file: File;
|
| 13 |
path: string;
|
| 14 |
}
|
| 15 |
|
| 16 |
/**
|
| 17 |
-
* Uploads
|
| 18 |
*
|
| 19 |
-
* @param
|
| 20 |
-
* @returns
|
| 21 |
*/
|
| 22 |
-
export const
|
| 23 |
-
const { file, path } = payload;
|
| 24 |
-
|
| 25 |
const formData = new FormData();
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
try {
|
| 31 |
const response = await fetch(BACKEND_URL, {
|
|
@@ -45,15 +49,15 @@ export const uploadFileToHub = async (payload: UploadPayload): Promise<string> =
|
|
| 45 |
throw new Error(data.error || 'Upload failed on server');
|
| 46 |
}
|
| 47 |
|
| 48 |
-
return data.
|
| 49 |
|
| 50 |
} catch (err: any) {
|
| 51 |
-
console.error("[FE]
|
| 52 |
const errorMessage = err?.message || "Unknown error";
|
| 53 |
-
|
| 54 |
if (errorMessage.includes('Failed to fetch') || errorMessage.includes('backend running')) {
|
| 55 |
-
throw new Error("Cannot connect to Backend. Make sure
|
| 56 |
}
|
| 57 |
throw new Error(errorMessage);
|
| 58 |
}
|
| 59 |
-
};
|
|
|
|
| 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; // Used for tracking
|
| 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 |
+
// Append all files and paths to FormData
|
| 28 |
+
// Multer on the backend will receive these as arrays
|
| 29 |
+
items.forEach(item => {
|
| 30 |
+
formData.append('paths', item.path);
|
| 31 |
+
formData.append('files', item.file);
|
| 32 |
+
});
|
| 33 |
|
| 34 |
try {
|
| 35 |
const response = await fetch(BACKEND_URL, {
|
|
|
|
| 49 |
throw new Error(data.error || 'Upload failed on server');
|
| 50 |
}
|
| 51 |
|
| 52 |
+
return data.urls;
|
| 53 |
|
| 54 |
} catch (err: any) {
|
| 55 |
+
console.error("[FE] Batch Upload Failed:", err);
|
| 56 |
const errorMessage = err?.message || "Unknown error";
|
| 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 |
+
};
|