This commit is contained in:
1 parent
2a0e5e5591
commit
83b91bb122
8 files changed
+149
-216
No files matched your search
@@ -0,0 +1,26 @@
|
||||
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);
|
||||
Reference in new issue
Block a user