23 lines
773 B
TypeScript
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)
|
|
}
|