import fs from 'node:fs'; import type { APIContext, APIRoute } from 'astro'; import { getVideoThumbnail, resolveVideo } from '@util/video.ts'; async function handle(context: APIContext, headersOnly = false) { const video = await resolveVideo(context.params.name ?? ''); if (!video) return new Response(null, { status: 404 }); const thumbnail = await getVideoThumbnail(video); if (!thumbnail) return new Response(null, { status: 404 }); const stats = await fs.promises.stat(thumbnail.path); const headers = { 'Content-Type': thumbnail.contentType, 'Content-Length': stats.size.toString(), 'Cache-Control': 'public, max-age=86400' }; if (headersOnly) return new Response(null, { headers }); const data = await fs.promises.readFile(thumbnail.path); return new Response(new Uint8Array(data), { headers }); } export const HEAD: APIRoute = (context) => handle(context, true); export const GET: APIRoute = (context) => handle(context);