marriedtermiteblyi commited on
Commit
1134a0a
·
verified ·
1 Parent(s): c0659f6

Update services/hfService.ts

Browse files
Files changed (1) hide show
  1. services/hfService.ts +21 -17
services/hfService.ts CHANGED
@@ -1,31 +1,35 @@
 
1
  /**
2
  * FRONTEND SERVICE
3
  *
4
- * This service no longer uses @huggingface/hub directly.
5
- * It sends files to our local Node.js backend (server.js).
6
  */
7
 
8
  // Use relative URL so Vite proxy can forward it to localhost:3001
9
  const BACKEND_URL = '/api/upload';
10
 
11
- interface UploadPayload {
 
12
  file: File;
13
  path: string;
14
  }
15
 
16
  /**
17
- * Uploads a file by sending it to the Node.js backend.
18
  *
19
- * @param payload File and destination path
20
- * @returns The public URL of the uploaded file
21
  */
22
- export const uploadFileToHub = async (payload: UploadPayload): Promise<string> => {
23
- const { file, path } = payload;
24
-
25
  const formData = new FormData();
26
- // Append text fields BEFORE files is good practice for some parsers
27
- formData.append('path', path);
28
- formData.append('file', file);
 
 
 
 
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.url;
49
 
50
  } catch (err: any) {
51
- console.error("[FE] API Call Failed:", err);
52
  const errorMessage = err?.message || "Unknown error";
53
- // Suggest running the server if connection fails
54
  if (errorMessage.includes('Failed to fetch') || errorMessage.includes('backend running')) {
55
- throw new Error("Cannot connect to Backend. Make sure to run 'npm run server' in a separate terminal.");
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
+ };