Files
PacketStation/frontend/src/api/assets.ts
T

20 lines
874 B
TypeScript
Raw Normal View History

// Asset upload and the blob URL. The upload is multipart through the shared request.
import { api, request } from "./client";
import type { Asset } from "./types";
/** Same-origin URL of a stored file. */
export function blobUrl(assetId: string, download = false): string {
return `/_blob/${encodeURIComponent(assetId)}${download ? "?download=1" : ""}`;
}
export const assetsApi = {
upload: (file: File, opts: { packet_id?: number; purpose?: string } = {}) => {
const fd = new FormData();
fd.append("file", file, file.name);
if (opts.packet_id !== undefined) fd.append("packet_id", String(opts.packet_id));
if (opts.purpose) fd.append("purpose", opts.purpose);
return request<Asset>("/api/assets", { method: "POST", body: fd });
},
get: (id: string, signal?: AbortSignal) => api.get<Asset>(`/api/assets/${encodeURIComponent(id)}`, signal),
};