SPB Git forge

spb/polyllm

Public
15commits 1branches 0releases
2.2 MBsize
maindefault branch
13 days agolast push
TypeScript 97.4% SQL 1% JavaScript 0.9% CSS 0.6%
1.9 KB · 46 lines typescript
Raw Blame History
1import { withUser, json, ApiError } from "@/lib/api";2import { LIMITS } from "@/lib/rate-limit";3import { listFiles, saveFile, deleteFile, LIBRARY_MAX_BYTES } from "@/lib/library/service";45export const dynamic = "force-dynamic";67/** GET /api/library/files?projectId=<id|none>&q=&kind= → { files } (metadata only). */8export const GET = withUser(async ({ req, user }) => {9  const p = new URL(req.url).searchParams;10  const files = await listFiles(user.id, {11    projectId: p.get("projectId") ?? undefined,12    q: p.get("q") ?? undefined,13    kind: p.get("kind") ?? undefined,14    limit: p.get("limit") ? Number(p.get("limit")) : undefined,15  });16  return json({ files });17});1819/** POST /api/library/files — multipart: file, projectId?, description? → 201 { file } */20export const POST = withUser(21  async ({ req, user }) => {22    const len = Number(req.headers.get("content-length") ?? 0);23    if (len > LIBRARY_MAX_BYTES + 8192) throw new ApiError(413, "File too large (max 10 MB)", "PAYLOAD_TOO_LARGE");24    const form = await req.formData().catch(() => null);25    const file = form?.get("file");26    if (!(file instanceof File)) throw new ApiError(400, "Missing file", "BAD_REQUEST");27    const projectId = form?.get("projectId");28    const description = form?.get("description");29    const saved = await saveFile(user.id, {30      file,31      projectId: typeof projectId === "string" && projectId && projectId !== "none" ? projectId.slice(0, 64) : null,32      description: typeof description === "string" ? description : null,33    });34    return json({ file: saved }, { status: 201 });35  },36  { limit: { ...LIMITS.upload, key: "library-upload" } },37);3839/** DELETE /api/library/files?id= */40export const DELETE = withUser(async ({ req, user }) => {41  const id = new URL(req.url).searchParams.get("id");42  if (!id) throw new ApiError(400, "Missing id");43  await deleteFile(user.id, id);44  return json({ ok: true });45});46