mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
feat(api): add persistent file management helpers to frontend api module
Adds UserFile/UserFileDetail types plus apiListFiles, apiGetFileDetails, apiUploadUserFiles, apiDeleteUserFiles, getFileThumbnailUrl, and getFileDownloadUrl to support the Files page.
This commit is contained in:
@@ -80,6 +80,82 @@ export function getDownloadUrl(jobId: string, filename: string): string {
|
|||||||
return `/api/v1/download/${jobId}/${filename}`;
|
return `/api/v1/download/${jobId}/${filename}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── Persistent File Management ──────────────────────────────────
|
||||||
|
|
||||||
|
export interface UserFile {
|
||||||
|
id: string;
|
||||||
|
originalName: string;
|
||||||
|
mimeType: string;
|
||||||
|
size: number;
|
||||||
|
width: number | null;
|
||||||
|
height: number | null;
|
||||||
|
version: number;
|
||||||
|
toolChain: string[];
|
||||||
|
createdAt: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UserFileDetail extends UserFile {
|
||||||
|
versions: Array<{
|
||||||
|
id: string;
|
||||||
|
version: number;
|
||||||
|
size: number;
|
||||||
|
toolChain: string[];
|
||||||
|
createdAt: string;
|
||||||
|
}>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function apiListFiles(params?: {
|
||||||
|
search?: string;
|
||||||
|
limit?: number;
|
||||||
|
offset?: number;
|
||||||
|
}): Promise<{ files: UserFile[]; total: number }> {
|
||||||
|
const searchParams = new URLSearchParams();
|
||||||
|
if (params?.search) searchParams.set("search", params.search);
|
||||||
|
if (params?.limit) searchParams.set("limit", String(params.limit));
|
||||||
|
if (params?.offset) searchParams.set("offset", String(params.offset));
|
||||||
|
const qs = searchParams.toString();
|
||||||
|
return apiGet(`/v1/files${qs ? `?${qs}` : ""}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function apiGetFileDetails(id: string): Promise<UserFileDetail> {
|
||||||
|
return apiGet(`/v1/files/${id}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function apiUploadUserFiles(
|
||||||
|
files: File[],
|
||||||
|
): Promise<{ files: Array<{ id: string; originalName: string; size: number; version: number }> }> {
|
||||||
|
const formData = new FormData();
|
||||||
|
files.forEach((f) => formData.append("files", f));
|
||||||
|
const res = await fetch("/api/v1/files/upload", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { Authorization: `Bearer ${getToken()}` },
|
||||||
|
body: formData,
|
||||||
|
});
|
||||||
|
if (!res.ok) throw new Error(`Upload failed: ${res.status}`);
|
||||||
|
return res.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function apiDeleteUserFiles(ids: string[]): Promise<{ deleted: number }> {
|
||||||
|
const res = await fetch("/api/v1/files", {
|
||||||
|
method: "DELETE",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `Bearer ${getToken()}`,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ ids }),
|
||||||
|
});
|
||||||
|
if (!res.ok) throw new Error(`Delete failed: ${res.status}`);
|
||||||
|
return res.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getFileThumbnailUrl(id: string): string {
|
||||||
|
return `/api/v1/files/${id}/thumbnail`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getFileDownloadUrl(id: string): string {
|
||||||
|
return `/api/v1/files/${id}/download`;
|
||||||
|
}
|
||||||
|
|
||||||
export async function apiDownloadBlob(
|
export async function apiDownloadBlob(
|
||||||
jobId: string,
|
jobId: string,
|
||||||
filename: string,
|
filename: string,
|
||||||
|
|||||||
Reference in New Issue
Block a user