Files
website/src/pages/video/[name]/thumbnail.jpg.ts
T
bytedream 83b91bb122
deploy / build-and-deploy (push) Successful in 45s
add video thumbnail generation for opengraph
2026-10-31 03:59:25 +01:00

27 lines
967 B
TypeScript

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);