import { NextRequest } from "next/server"; import fs from "fs"; import path from "path"; const IMAGES_ROOT = path.join(process.cwd(), "src/assets/images"); const MIME: Record = { ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".png": "image/png", ".webp": "image/webp", ".gif": "image/gif", ".avif": "image/avif", ".svg": "image/svg+xml", }; export async function GET( _request: NextRequest, { params }: { params: Promise<{ path: string[] }> }, ) { const segments = (await params).path.map((part) => decodeURIComponent(part)); const filePath = path.resolve(IMAGES_ROOT, ...segments); if (!filePath.startsWith(IMAGES_ROOT + path.sep) || !fs.existsSync(filePath) || !fs.statSync(filePath).isFile()) { return new Response("Not found", { status: 404 }); } const ext = path.extname(filePath).toLowerCase(); const body = fs.readFileSync(filePath); return new Response(body, { headers: { "Content-Type": MIME[ext] ?? "application/octet-stream", "Cache-Control": "public, max-age=3600", }, }); }