import { existsSync, readFileSync } from 'node:fs' export default defineEventHandler(async (event) => { const { owner } = assertLibraryOwner(event) const id = String(getRouterParam(event, 'id') || '') const clip = getClip(owner, id) assertFolderAccess(event, clip.folderId) const videoPath = clipVideoPath(owner, clip.id) if (!existsSync(videoPath)) { throw createError({ statusCode: 404, statusMessage: 'Video file is missing' }) } const framePath = clipLastFramePath(owner, clip.id) if (!existsSync(framePath)) { try { await extractLastFrame(videoPath, framePath) } catch (error) { const message = error instanceof Error ? error.message : String(error) throw createError({ statusCode: 500, statusMessage: `Could not extract the last frame: ${message}` }) } } const buf = readFileSync(framePath) setHeader(event, 'Content-Type', 'image/png') setHeader(event, 'Cache-Control', 'private, no-store') return buf })