ultisuite-client/lib/api/upload.ts
R3D347HR4Y d6d18f911b
Some checks failed
E2E / Playwright e2e (push) Has been cancelled
Lots of stuff and mobile app
2026-06-17 00:13:28 +02:00

23 lines
773 B
TypeScript

import { useAuthStore } from "@/lib/api/auth-store"
import { getApiBaseUrl } from "@/lib/runtime-config"
export async function uploadFile(targetPath: string, file: File, onProgress?: (pct: number) => void) {
const token = useAuthStore.getState().accessToken
const path = targetPath.startsWith("/") ? targetPath : `/${targetPath}`
const url = `${getApiBaseUrl()}/drive/files${path}`
const res = await fetch(url, {
method: "POST",
headers: {
Authorization: token ? `Bearer ${token}` : "",
"Content-Type": file.type || "application/octet-stream",
},
body: file,
})
if (!res.ok) {
const err = await res.json().catch(() => ({}))
throw new Error((err as { message?: string }).message ?? res.statusText)
}
onProgress?.(100)
}