31 lines
1.2 KiB
TypeScript
31 lines
1.2 KiB
TypeScript
import { existsSync, createReadStream } from 'node:fs'
|
|
import { fileEtag, stillPath, stillThumbPath, existingStillThumbPath } from '~/server/utils/library'
|
|
|
|
export default defineEventHandler((event) => {
|
|
const { owner } = assertLibraryOwner(event)
|
|
const id = String(getRouterParam(event, 'id') || '')
|
|
const still = getStill(owner, id)
|
|
assertFolderAccess(event, still.folderId)
|
|
|
|
const path = existingStillThumbPath(owner, id)
|
|
const original = stillPath(owner, still.id)
|
|
// Ready thumb → stream. Never await ffmpeg on the request path.
|
|
if (path && existsSync(path) && path !== original) {
|
|
const etag = fileEtag(path)
|
|
setHeader(event, 'ETag', etag)
|
|
setHeader(event, 'Cache-Control', 'public, max-age=86400')
|
|
if (getHeader(event, 'if-none-match') === etag) {
|
|
setResponseStatus(event, 304)
|
|
return null
|
|
}
|
|
setHeader(event, 'Content-Type', 'image/jpeg')
|
|
return sendStream(event, createReadStream(path))
|
|
}
|
|
|
|
// Missing → answer now; build .thumb.jpg in the background (job save / upload also kick this)
|
|
void stillThumbPath(owner, id).catch(() => null)
|
|
setHeader(event, 'Cache-Control', 'no-store')
|
|
setResponseStatus(event, 204)
|
|
return null
|
|
})
|