"use client"; export type GatewayMediaKind = "image" | "video" | "audio"; export type GatewayMediaUploadResult = { url: string; filename: string; contentType: string; }; type GatewayMediaUploadResponse = { success?: boolean; url?: string; error?: string; }; export async function uploadFileToGatewayMedia( file: File, kind: GatewayMediaKind ): Promise { return uploadBlobToGatewayMedia(file, file.name || `canvas-${kind}`, file.type, kind); } export async function uploadBlobToGatewayMedia( blob: Blob, filename: string, contentType: string, kind: GatewayMediaKind ): Promise { const formData = new FormData(); formData.append("file", blob, filename || `canvas-${kind}`); const response = await fetch("/api/popi/media/upload", { method: "POST", body: formData, }); const result = (await response.json().catch(() => null)) as GatewayMediaUploadResponse | null; if (!response.ok || !result?.success || !result.url) { throw new Error(result?.error || "Media upload failed"); } return { url: result.url, filename: filename || `canvas-${kind}`, contentType: contentType || blob.type || `${kind}/*`, }; }