import { useSyncExternalStore } from "react"; /** One file of an upload batch. */ export interface UploadItem { id: number; name: string; size: number; sent: number; status: "wait" | "run" | "done" | "error"; error?: string; } /** Uploads started together (a drop, a file dialog); shown as one panel with a total bar. */ export interface UploadBatch { id: number; items: UploadItem[]; closed: boolean; } const CHUNK = 8 * 1024 * 1024; let seq = 0; let batches: UploadBatch[] = []; const listeners = new Set<() => void>(); const emit = () => { batches = [...batches]; listeners.forEach((l) => l()); }; export const useUploads = () => useSyncExternalStore( (l) => (listeners.add(l), () => listeners.delete(l)), () => batches, ); export const closeUploadBatch = (id: number) => { batches = batches.filter((b) => b.id !== id); emit(); }; /** PUT one chunk; `onBytes` reports the bytes of this chunk sent so far. */ function putChunk(url: string, chunk: Blob, onBytes: (n: number) => void): Promise { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); xhr.open("PUT", url); xhr.withCredentials = true; xhr.upload.onprogress = (e) => e.lengthComputable && onBytes(e.loaded); xhr.onload = () => { if (xhr.status >= 200 && xhr.status < 300) return resolve(); let msg = `HTTP ${xhr.status}`; try { msg = JSON.parse(xhr.responseText).error ?? msg; } catch { if (xhr.status === 413) msg = "too large for the reverse proxy"; } reject(new Error(msg)); }; xhr.onerror = () => reject(new Error("network error")); xhr.send(chunk); }); } /** * Upload a file in 8 MB chunks (stays below reverse proxy body limits; the server appends at `offset`). * `onProgress` gets the bytes sent so far. */ export async function chunkedUpload(endpoint: string, file: Blob, onProgress?: (sent: number) => void): Promise { const sep = endpoint.includes("?") ? "&" : "?"; let offset = 0; do { const chunk = file.slice(offset, offset + CHUNK); const base = offset; await putChunk(`${endpoint}${sep}offset=${offset}`, chunk, (n) => onProgress?.(base + n)); offset += chunk.size; onProgress?.(offset); } while (offset < file.size); } export interface UploadJob { file: File | Blob; /** Absolute target path on the server (files upload). */ path: string; /** Other chunked endpoint instead of /api/files/upload. */ endpoint?: string; name?: string; /** Called with the fraction 0..1 of this file. */ onProgress?: (fraction: number) => void; } /** * Upload files one after the other with the progress panel (per file and in total). * Resolves with the paths that arrived; failed files are listed in the panel. */ export async function uploadBatch(jobs: UploadJob[]): Promise { if (!jobs.length) return []; const batch: UploadBatch = { id: ++seq, closed: false, items: jobs.map((j) => ({ id: ++seq, name: j.name ?? (j.file as File).name ?? j.path.split("/").pop() ?? "file", size: j.file.size, sent: 0, status: "wait" as const })), }; batches = [...batches, batch]; emit(); const done: string[] = []; for (const [i, j] of jobs.entries()) { const it = batch.items[i]; it.status = "run"; emit(); try { await chunkedUpload(j.endpoint ?? `/api/files/upload?path=${encodeURIComponent(j.path)}`, j.file, (n) => { it.sent = n; j.onProgress?.(j.file.size ? n / j.file.size : 1); emit(); }); it.sent = it.size; it.status = "done"; done.push(j.path); } catch (e: any) { it.status = "error"; it.error = e.message ?? String(e); } emit(); } // A clean batch fades away, one with errors stays until it is closed. if (batch.items.every((x) => x.status === "done")) setTimeout(() => closeUploadBatch(batch.id), 2500); return done; }