import { define } from "@/utils.ts"; import { authenticate, unauthorized } from "@/src/auth.ts"; import { db, type FileRow, type ShareRow } from "@/src/db.ts"; import { delete_upload, upload_response } 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(); } return await upload_response(file.storage_id) ?? not_found(); }, 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(); } const shares = await db.get_entries_by_index("shares", "file_id", file.id) as ShareRow[]; for (const share of shares) { await db.delete_entry("shares", share); } await db.delete_entry("files", file); await delete_upload(file.storage_id); return new Response(null, { status: 204 }); }, });