File size: 1,134 Bytes
1134a0a
67a39f2
 
1134a0a
67b1438
67a39f2
 
 
 
1134a0a
67a39f2
1134a0a
67b1438
 
 
 
 
67a39f2
 
 
 
 
 
 
 
 
67b1438
 
 
67a39f2
 
 
 
 
 
 
 
1134a0a
67a39f2
 
67b1438
 
67a39f2
1134a0a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

const BACKEND_URL = '/api/upload';

interface BatchItem {
  id: string; 
  file: File;
  path: string;
}

export const uploadBatchToHub = async (items: BatchItem[]): Promise<string[]> => {
  const formData = new FormData();

  // Optimized append loop
  for (const item of items) {
      formData.append('paths', item.path);
      formData.append('files', item.file);
  }

  try {
    const response = await fetch(BACKEND_URL, {
      method: 'POST',
      body: formData,
    });

    const contentType = response.headers.get("content-type");
    if (!contentType || !contentType.includes("application/json")) {
        const text = await response.text();
        console.error("Non-JSON Response:", text);
        throw new Error(`Server returned unexpected status ${response.status}. Check server logs.`);
    }

    const data = await response.json();

    if (!response.ok || !data.success) {
      throw new Error(data.error || 'Upload failed on server');
    }

    return data.urls;
    
  } catch (err: any) {
    // Retry logic could be added here if needed
    throw new Error(err?.message || "Connection failed");
  }
};