import path from "node:path"; import { readFile, stat } from "node:fs/promises"; import { NextResponse } from "next/server"; import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { POSTS_MEDIA_DIRNAME } from "yt-dlp-transcript-common/social/postCapture"; export const dynamic = "force-dynamic"; // GET /api/channels//posts-media// — one file of a post's // capture (release 37 S4: a page record's shot.png and images, shown on the // Wayback pages panel). `channels//posts-media//` is on the corpus // disk (the media tier never moves it); images only, read-only. const SAFE_SEGMENT = /^[A-Za-z0-9._-]+$/; const IMAGE_TYPES: Record = { png: "image/png", jpg: "image/jpeg", jpeg: "image/jpeg", gif: "image/gif", webp: "image/webp", avif: "image/avif", }; export async function GET(_request: Request, { params }: { params: Promise<{ slug: string; id: string; name: string }> }) { const { slug, id, name } = await params; if (![slug, id, name].every((s) => SAFE_SEGMENT.test(s)) || name.startsWith(".")) { return NextResponse.json({ error: "Invalid path" }, { status: 400 }); } const type = IMAGE_TYPES[name.split(".").pop()!.toLowerCase()]; if (!type) return NextResponse.json({ error: "Not an image" }, { status: 404 }); const dir = path.resolve(getPaths().channelsDir, slug, POSTS_MEDIA_DIRNAME, id); const file = path.resolve(dir, name); if (!file.startsWith(dir + path.sep)) return NextResponse.json({ error: "Forbidden" }, { status: 403 }); try { if (!(await stat(file)).isFile()) throw new Error("not a file"); return new NextResponse(new Uint8Array(await readFile(file)), { headers: { "content-type": type, "cache-control": "no-store" }, }); } catch { return NextResponse.json({ error: "Not found" }, { status: 404 }); } }