|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const BACKEND_URL = '/api/upload'; |
|
|
|
|
|
interface UploadPayload { |
|
|
file: File; |
|
|
path: string; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const uploadFileToHub = async (payload: UploadPayload): Promise<string> => { |
|
|
const { file, path } = payload; |
|
|
|
|
|
const formData = new FormData(); |
|
|
|
|
|
formData.append('path', path); |
|
|
formData.append('file', 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.url; |
|
|
|
|
|
} catch (err: any) { |
|
|
console.error("[FE] API Call 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 to run 'npm run server' in a separate terminal."); |
|
|
} |
|
|
throw new Error(errorMessage); |
|
|
} |
|
|
}; |