File uploading

This commit is contained in:
2026-10-29 17:59:34 -03:00
parent ec9bdb8e8e
commit b65d8efad8
18 changed files with 686 additions and 51 deletions
+48
View File
@@ -0,0 +1,48 @@
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 });
},
});
+78
View File
@@ -0,0 +1,78 @@
import { z } from "@zod/zod";
import { define } from "@/utils.ts";
import { authenticate, unauthorized } from "@/src/auth.ts";
import { db, type FileRow } from "@/src/db.ts";
import type { FileEntry } from "@/src/files.ts";
import { MAX_UPLOAD_SIZE } from "@/src/server_constants.ts";
import { delete_upload, save_upload, UploadTooLargeError } from "@/src/storage.ts";
const Metadata = z.base64().min(1).max(4096);
const too_large = () => Response.json({ error: "The file is too large" }, { status: 413 });
export const handler = define.handlers({
async GET(ctx) {
const user = await authenticate(ctx.req);
if (!user) {
return unauthorized();
}
const files = await db.get_entries_by_index("files", "user_id", user.id) as FileRow[];
return Response.json(
files.map((file): FileEntry => ({
id: file.id,
date_created: file.date_created,
size: file.size,
metadata: file.metadata,
})).reverse(),
);
},
// The body is the encrypted file, its encrypted metadata goes in the X-File-Metadata header
async POST(ctx) {
const user = await authenticate(ctx.req);
if (!user) {
return unauthorized();
}
const metadata = Metadata.safeParse(ctx.req.headers.get("X-File-Metadata"));
if (!metadata.success || !ctx.req.body) {
return Response.json({ error: "Invalid request" }, { status: 400 });
}
if (Number(ctx.req.headers.get("Content-Length")) > MAX_UPLOAD_SIZE) {
return too_large();
}
let saved;
try {
saved = await save_upload(ctx.req.body, MAX_UPLOAD_SIZE);
} catch (err) {
if (err instanceof UploadTooLargeError) {
return too_large();
}
throw err;
}
try {
const file = await db.create_entry("files", {
user_id: user.id,
storage_id: saved.storage_id,
size: saved.size,
metadata: metadata.data,
});
return Response.json(
{
id: file.id,
date_created: file.date_created,
size: file.size,
metadata: file.metadata,
} satisfies FileEntry,
{ status: 201 },
);
} catch (err) {
await delete_upload(saved.storage_id);
throw err;
}
},
});