SPB Git

Repository Public

files.ts 8.4 KB 163 lines typescript

1import type { FastifyInstance } from "fastify"2import fs from "node:fs"3import path from "node:path"4import { z } from "zod"5import { config } from "./config.js"6import { sql } from "./db.js"7import { requireDevice } from "./session.js"8import { bad, parse, rateLimit, zUuid } from "./util.js"910/**11 * Blob store for attachments. Every chunk is ciphertext produced in the browser; the server never12 * sees a key, a file name or a media type. Storage is a directory tree behind this small interface.13 */14const root = () => path.join(config.dataDir, "blobs")15const dirOf = (id: string) => path.join(root(), id.slice(0, 2), id)16const chunkPath = (id: string, n: number) => path.join(dirOf(id), `${n}.bin`)1718export const blobs = {19  async put(id: string, n: number, data: Buffer) {20    await fs.promises.mkdir(dirOf(id), { recursive: true })21    const tmp = `${chunkPath(id, n)}.${process.pid}.tmp`22    await fs.promises.writeFile(tmp, data)23    await fs.promises.rename(tmp, chunkPath(id, n))24  },25  async received(id: string): Promise<number[]> {26    try {27      return (await fs.promises.readdir(dirOf(id))).filter((f) => f.endsWith(".bin")).map((f) => parseInt(f, 10)).sort((x, y) => x - y)28    } catch {29      return []30    }31  },32  stream(id: string, n: number) {33    return fs.createReadStream(chunkPath(id, n))34  },35  async size(id: string, n: number) {36    return (await fs.promises.stat(chunkPath(id, n))).size37  },38  async remove(id: string) {39    await fs.promises.rm(dirOf(id), { recursive: true, force: true })40  },41}4243async function canRead(fileId: string, userId: string) {44  const [f] = await sql`select owner_id, chunk_count, completed_at, expires_at from files where id = ${fileId}`45  if (!f || (f.expires_at && new Date(f.expires_at) < new Date())) throw bad("not_found", 404)46  if (f.owner_id !== userId) {47    const [acc] = await sql`48      select 1 from file_access fa join conversation_members m on m.conversation_id = fa.conversation_id49      where fa.file_id = ${fileId} and m.user_id = ${userId} and m.left_at is null`50    if (!acc) throw bad("not_found", 404)51  }52  return f53}5455export function fileRoutes(app: FastifyInstance) {56  app.post("/api/files", async (req) => {57    const a = await requireDevice(req)58    rateLimit(`file-create:${a.userId}`, 120, 60_000)59    const body = parse(60      z.object({ size: z.number().int().min(1).max(config.maxFileBytes), chunk_size: z.number().int().min(4096).max(config.maxChunkBytes), chunk_count: z.number().int().min(1).max(100_000) }),61      req.body,62    )63    if (Math.ceil(body.size / body.chunk_size) !== body.chunk_count) throw bad("inconsistent_chunks")64    // Quota is reserved at creation and released if the upload is abandoned.65    const r = await sql`update users set used_bytes = used_bytes + ${body.size} where id = ${a.userId} and used_bytes + ${body.size} <= quota_bytes returning 1`66    if (!r.length) throw bad("quota_exceeded", 413, "Storage quota exceeded")67    const [f] = await sql`insert into files (owner_id, size, chunk_size, chunk_count) values (${a.userId}, ${body.size}, ${body.chunk_size}, ${body.chunk_count}) returning id`68    return { id: f.id }69  })7071  app.get("/api/files/:id", async (req) => {72    const a = await requireDevice(req)73    const { id } = parse(z.object({ id: zUuid }), req.params)74    const [f] = await sql`select size, chunk_size, chunk_count, completed_at from files where id = ${id} and owner_id = ${a.userId}`75    if (!f) throw bad("not_found", 404)76    return { ...f, received: await blobs.received(id) }77  })7879  app.put("/api/files/:id/chunks/:n", { bodyLimit: config.maxChunkBytes }, async (req) => {80    const a = await requireDevice(req)81    const { id, n } = parse(z.object({ id: zUuid, n: z.coerce.number().int().min(0) }), req.params)82    const [f] = await sql`select size, chunk_size, chunk_count, completed_at from files where id = ${id} and owner_id = ${a.userId}`83    if (!f) throw bad("not_found", 404)84    if (f.completed_at) throw bad("already_complete", 409)85    if (n >= f.chunk_count) throw bad("chunk_out_of_range")86    const data = req.body as Buffer87    if (!Buffer.isBuffer(data)) throw bad("expected_octet_stream", 415)88    const expected = n === f.chunk_count - 1 ? f.size - f.chunk_size * (f.chunk_count - 1) : f.chunk_size89    if (data.length !== expected) throw bad("wrong_chunk_size")90    await blobs.put(id, n, data)91    return { ok: true }92  })9394  app.post("/api/files/:id/complete", async (req) => {95    const a = await requireDevice(req)96    const { id } = parse(z.object({ id: zUuid }), req.params)97    const body = parse(z.object({ conversation_id: zUuid, expires_in: z.number().int().min(60).max(366 * 86400).optional() }), req.body)98    const [f] = await sql`select chunk_count from files where id = ${id} and owner_id = ${a.userId}`99    if (!f) throw bad("not_found", 404)100    const got = await blobs.received(id)101    if (got.length !== f.chunk_count) throw bad("incomplete", 409, `${got.length}/${f.chunk_count}`)102    const [m] = await sql`select 1 from conversation_members where conversation_id = ${body.conversation_id} and user_id = ${a.userId} and left_at is null`103    if (!m) throw bad("not_a_member", 403)104    await sql`update files set completed_at = coalesce(completed_at, now()),105              expires_at = ${body.expires_in ? new Date(Date.now() + body.expires_in * 1000) : null} where id = ${id}`106    await sql`insert into file_access (file_id, conversation_id) values (${id}, ${body.conversation_id}) on conflict do nothing`107    return { ok: true }108  })109110  /** Forwarding: grants another conversation access to a file the caller can already read. */111  app.post("/api/files/:id/share", async (req) => {112    const a = await requireDevice(req)113    const { id } = parse(z.object({ id: zUuid }), req.params)114    const { conversation_id } = parse(z.object({ conversation_id: zUuid }), req.body)115    const f = await canRead(id, a.userId)116    if (!f.completed_at) throw bad("incomplete", 409)117    const [m] = await sql`select 1 from conversation_members where conversation_id = ${conversation_id} and user_id = ${a.userId} and left_at is null`118    if (!m) throw bad("not_a_member", 403)119    await sql`insert into file_access (file_id, conversation_id) values (${id}, ${conversation_id}) on conflict do nothing`120    return { ok: true }121  })122123  app.get("/api/files/:id/chunks/:n", async (req, reply) => {124    const a = await requireDevice(req)125    const { id, n } = parse(z.object({ id: zUuid, n: z.coerce.number().int().min(0) }), req.params)126    const f = await canRead(id, a.userId)127    if (!f.completed_at || n >= f.chunk_count) throw bad("not_found", 404)128    reply.header("content-type", "application/octet-stream")129    reply.header("content-length", await blobs.size(id, n))130    reply.header("cache-control", "private, no-store")131    return reply.send(blobs.stream(id, n))132  })133134  app.delete("/api/files/:id", async (req) => {135    const a = await requireDevice(req)136    const { id } = parse(z.object({ id: zUuid }), req.params)137    const [f] = await sql`delete from files where id = ${id} and owner_id = ${a.userId} returning size`138    if (!f) throw bad("not_found", 404)139    await sql`update users set used_bytes = greatest(0, used_bytes - ${f.size}) where id = ${a.userId}`140    await blobs.remove(id)141    return { ok: true }142  })143}144145/** Periodic cleanup: expired ciphertext, expired files, abandoned uploads, stale challenges. */146export async function janitor(log: (m: string) => void) {147  const m = await sql`delete from messages where expires_at is not null and expires_at < now() and kind = 'app' returning 1`148  const old = await sql`delete from messages where kind = 'app' and created_at < now() - make_interval(days => ${config.messageRetentionDays}) returning 1`149  await sql`delete from challenges where expires_at < now()`150  await sql`delete from welcomes where created_at < now() - interval '120 days'`151  await sql`delete from sessions where expires_at < now() - interval '30 days' or revoked_at < now() - interval '30 days'`152  await sql`delete from audit_log where at < now() - interval '400 days'`153  const dead = await sql`154    delete from files where (expires_at is not null and expires_at < now())155       or (completed_at is null and created_at < now() - interval '2 days')156    returning id, owner_id, size`157  for (const f of dead) {158    await sql`update users set used_bytes = greatest(0, used_bytes - ${f.size}) where id = ${f.owner_id}`159    await blobs.remove(f.id)160  }161  if (m.length || old.length || dead.length) log(`janitor: ${m.length} expired, ${old.length} aged-out ciphertexts, ${dead.length} files removed`)162}163
Files main
Menu

Theme