// Immagini delle slide del Campus. Passano dal middleware come il resto di // /campus, quindi non sono raggiungibili senza login con ruolo abilitato. import type { APIRoute } from 'astro'; import { createReadStream, statSync } from 'node:fs'; import { extname } from 'node:path'; import { Readable } from 'node:stream'; import { resolveAsset } from '../../../lib/campus'; export const prerender = false; const TYPES: Record = { '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.webp': 'image/webp', '.gif': 'image/gif', '.svg': 'image/svg+xml', }; export const GET: APIRoute = ({ params }) => { const rel = params.path; if (!rel) return new Response('Not found', { status: 404 }); const type = TYPES[extname(rel).toLowerCase()]; if (!type) return new Response('Tipo non consentito', { status: 400 }); const file = resolveAsset(rel); if (!file) return new Response('Not found', { status: 404 }); const { size } = statSync(file); const stream = Readable.toWeb(createReadStream(file)) as ReadableStream; return new Response(stream, { headers: { 'Content-Type': type, 'Content-Length': String(size), 'Cache-Control': 'private, max-age=86400', }, }); };