|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
|
|
|
|
|
items.forEach(item => { |
|
|
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")) { |
|
|
throw new Error(`Server returned unexpected ${response.status} ${response.statusText}. Is the backend running?`); |
|
|
} |
|
|
|
|
|
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) { |
|
|
console.error("[FE] Batch Upload Failed:", err); |
|
|
const errorMessage = err?.message || "Unknown error"; |
|
|
|
|
|
if (errorMessage.includes('Failed to fetch') || errorMessage.includes('backend running')) { |
|
|
throw new Error("Cannot connect to Backend. Make sure 'npm run server' is running."); |
|
|
} |
|
|
throw new Error(errorMessage); |
|
|
} |
|
|
}; |
|
|
|