import { define } from "@/utils.ts"; import { authenticate, unauthorized } from "@/src/auth.ts"; import { db, type FileRow } from "@/src/db.ts"; import { delete_upload, open_upload } from "@/src/storage.ts"; const not_found = () => Response.json({ error: "File not found" }, { status: 404 }); export const handler = define.handlers({ // Streams the encrypted file, the client decrypts it async GET(ctx) { const user = await authenticate(ctx.req); if (!user) { return unauthorized(); } const file = await db.get_entry("files", ctx.params.id) as FileRow | undefined; if (!file || file.user_id !== user.id) { return not_found(); } const stored = await open_upload(file.storage_id).catch(() => undefined); if (!stored) { return not_found(); } return new Response(stored.readable, { headers: { "Content-Type": "application/octet-stream", "Cache-Control": "no-store", }, }); }, async DELETE(ctx) { const user = await authenticate(ctx.req); if (!user) { return unauthorized(); } const file = await db.get_entry("files", ctx.params.id) as FileRow | undefined; if (!file || file.user_id !== user.id) { return not_found(); } await db.delete_entry("files", file); await delete_upload(file.storage_id); return new Response(null, { status: 204 }); }, });