File size: 1,840 Bytes
1134a0a 67a39f2 1134a0a 67a39f2 1134a0a 67a39f2 1134a0a 67a39f2 1134a0a 67a39f2 1134a0a 67a39f2 1134a0a 67a39f2 1134a0a 67a39f2 1134a0a 67a39f2 1134a0a 67a39f2 1134a0a 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
/**
* FRONTEND SERVICE
*
* This service communicates with the Node.js backend (server.js).
* It supports Batch Uploading to maximize speed.
*/
// Use relative URL so Vite proxy can forward it to localhost:3001
const BACKEND_URL = '/api/upload';
interface BatchItem {
id: string; // Used for tracking
file: File;
path: string;
}
/**
* Uploads multiple files in a single request.
*
* @param items Array of files and their destination paths
* @returns Array of public URLs for the uploaded files
*/
export const uploadBatchToHub = async (items: BatchItem[]): Promise<string[]> => {
const formData = new FormData();
// Append all files and paths to FormData
// Multer on the backend will receive these as arrays
items.forEach(item => {
formData.append('paths', item.path);
formData.append('files', item.file);
});
try {
const response = await fetch(BACKEND_URL, {
method: 'POST',
body: formData,
});
// Handle non-JSON responses (like 404 or 502 from proxy)
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);
}
};
|