import { resolveLibraryMedia } from "@/lib/library/media.mjs"; import { CHANNELS_DIR } from "@/lib/paths.mjs"; import { crossSiteRefusal } from "@/lib/report/sameOrigin.mjs"; import { rangeResponse } from "@/lib/report/serve.mjs"; export const dynamic = "force-dynamic"; // GET /api/library/media/// one captured picture or video of a library post (byte ranges) // // THE ONE ROUTE THAT SERVES A TRENDING LIBRARY'S MEDIA (lane posts-library), // for the Posts library page. A library is PRIVATE, so this route is narrow // (lib/library/media.mjs resolveLibraryMedia is the whole rule): // // - `lib` must carry the library marker; any other channel is a 404 // - only `channels//trending/posts-media//`: digits for the // id, one plain segment for the name, a picture's or a video's extension // (never capture.json, an article, the raw reads, the judgements), and // the file's real path inside that library's posts-media // - it lists nothing: there is no directory answer and no index of ids // - READ ONLY: GET and HEAD; the corpus is never written from here // - another site's page cannot embed or fetch it: a cross-site request, or // one naming a host that is not this machine, is refused // (crossSiteRefusal, asked of a READ here), and the answer says // `Cross-Origin-Resource-Policy: same-origin` // - never cached by a shared cache: `private, no-store` // // umtool is bound to the LAN (the operator's phone), as every media route of // it is: anyone on that network who knows a post's id and file name can ask. const PRIVATE = { "cache-control": "private, no-store", "cross-origin-resource-policy": "same-origin", "x-content-type-options": "nosniff", "x-robots-tag": "noindex", }; async function serve(request: Request, params: Promise<{ lib: string; id: string; name: string }>): Promise { const refused = crossSiteRefusal(request.headers); if (refused) return Response.json({ error: "refused: this file is served to umtool's own pages only" }, { status: 403, headers: PRIVATE }); const { lib, id, name } = await params; const found = await resolveLibraryMedia({ channelsDir: CHANNELS_DIR, lib, id, name }); if (!found.ok) return Response.json({ error: found.status === 400 ? "bad request" : "not found" }, { status: found.status, headers: PRIVATE }); const res = rangeResponse(request, { abs: found.abs, size: found.size, headers: { ...PRIVATE, "content-type": found.type, "accept-ranges": "bytes" } }); // A range that cannot be met is answered by the shared helper with its Content-Range alone: the // same private headers go on it as on every other answer of this route. if (res.status === 416) return new Response(null, { status: 416, headers: { ...PRIVATE, "content-range": res.headers.get("content-range") ?? `bytes */${found.size}` } }); return res; } export async function GET(request: Request, { params }: { params: Promise<{ lib: string; id: string; name: string }> }) { return serve(request, params); } export async function HEAD(request: Request, { params }: { params: Promise<{ lib: string; id: string; name: string }> }) { const res = await serve(request, params); await res.body?.cancel(); return new Response(null, { status: res.status, headers: res.headers }); }