Spaces:
Sleeping
Sleeping
| import { ApiResponse } from "@/types/api"; | |
| const BASE_URL = process.env.NEXT_PUBLIC_API_URL || "https://sid385-fill-the-frames.hf.space/api/v1"; | |
| export interface UploadResponse { | |
| fileId: string; | |
| filename: string; | |
| status: string; | |
| } | |
| export const uploadClient = { | |
| uploadFile: async (file: File, onProgress?: (percent: number) => void): Promise<ApiResponse<UploadResponse>> => { | |
| return new Promise((resolve, reject) => { | |
| const formData = new FormData(); | |
| formData.append("file", file); | |
| const xhr = new XMLHttpRequest(); | |
| xhr.open("POST", `${BASE_URL}/upload/`, true); | |
| if (onProgress) { | |
| xhr.upload.onprogress = (event) => { | |
| if (event.lengthComputable) { | |
| const percentComplete = Math.round((event.loaded / event.total) * 100); | |
| onProgress(percentComplete); | |
| } | |
| }; | |
| } | |
| xhr.onload = () => { | |
| if (xhr.status >= 200 && xhr.status < 300) { | |
| try { | |
| resolve(JSON.parse(xhr.responseText)); | |
| } catch (e) { | |
| reject(new Error("Invalid JSON response")); | |
| } | |
| } else { | |
| try { | |
| const errRes = JSON.parse(xhr.responseText); | |
| reject(new Error(errRes.message || `Upload failed with status ${xhr.status}`)); | |
| } catch(e) { | |
| reject(new Error(`Upload failed with status ${xhr.status}`)); | |
| } | |
| } | |
| }; | |
| xhr.onerror = () => reject(new Error("Network error during upload")); | |
| xhr.send(formData); | |
| }); | |
| }, | |
| }; | |