Serve the bench before the catalog and never await ffmpeg on /thumb.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Towsty
2026-09-29 20:35:28 -05:00
co-authored by Cursor
parent a465dd68ec
commit 4ec8ffa8d6
5 changed files with 141 additions and 67 deletions
+24 -16
View File
@@ -1,7 +1,7 @@
import { existsSync, createReadStream } from 'node:fs'
import { fileEtag, stillPath, stillThumbPath, sniffImageType } from '~/server/utils/library'
import { fileEtag, stillPath, existingStillThumbPath, stillThumbPath, sniffImageType } from '~/server/utils/library'
export default defineEventHandler(async (event) => {
export default defineEventHandler((event) => {
const { owner } = assertLibraryOwner(event)
const id = String(getRouterParam(event, 'id') || '')
const still = getStill(owner, id)
@@ -9,28 +9,36 @@ export default defineEventHandler(async (event) => {
const wantThumb = String(getQuery(event).thumb || '') === '1'
const original = stillPath(owner, still.id)
let path = original
if (wantThumb) {
path = await stillThumbPath(owner, id)
// Never fall back to the archive PNG/JPEG for ?thumb=1
if (!path || path === original || !existsSync(path)) {
throw createError({ statusCode: 404, statusMessage: 'Thumbnail not found' })
const path = existingStillThumbPath(owner, id)
if (path && path !== original && existsSync(path)) {
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))
}
} else if (!existsSync(path)) {
void stillThumbPath(owner, id).catch(() => null)
setHeader(event, 'Cache-Control', 'no-store')
setResponseStatus(event, 204)
return null
}
if (!existsSync(original)) {
throw createError({ statusCode: 404, statusMessage: 'Image file is missing' })
}
const etag = fileEtag(path)
const etag = fileEtag(original)
setHeader(event, 'ETag', etag)
setHeader(
event,
'Cache-Control',
wantThumb ? 'public, max-age=86400' : 'private, max-age=3600'
)
setHeader(event, 'Cache-Control', 'private, max-age=3600')
if (getHeader(event, 'if-none-match') === etag) {
setResponseStatus(event, 304)
return null
}
setHeader(event, 'Content-Type', wantThumb ? 'image/jpeg' : sniffImageType(path))
return sendStream(event, createReadStream(path))
setHeader(event, 'Content-Type', sniffImageType(original))
return sendStream(event, createReadStream(original))
})
+19 -15
View File
@@ -1,26 +1,30 @@
import { existsSync, createReadStream } from 'node:fs'
import { fileEtag, stillPath, stillThumbPath } from '~/server/utils/library'
import { fileEtag, stillPath, stillThumbPath, existingStillThumbPath } from '~/server/utils/library'
export default defineEventHandler(async (event) => {
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 = await stillThumbPath(owner, id)
const path = existingStillThumbPath(owner, id)
const original = stillPath(owner, still.id)
// Never stream the archive master as a "thumb"
if (!path || !existsSync(path) || path === original) {
throw createError({ statusCode: 404, statusMessage: 'Thumbnail not found' })
// 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))
}
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
})
+30
View File
@@ -187,6 +187,7 @@ interface Catalog {
}
const writeChains = new Map<string, Promise<unknown>>()
const catalogCache = new Map<string, { catalog: Catalog; mtimeMs: number }>()
export function libraryRoot() {
const config = useRuntimeConfig()
@@ -355,6 +356,13 @@ function ensureOwner(owner: string) {
function readCatalog(owner: string): Catalog {
ensureOwner(owner)
const path = catalogPath(owner)
let mtimeMs = 0
try {
mtimeMs = existsSync(path) ? statSync(path).mtimeMs : 0
} catch { mtimeMs = 0 }
const hit = catalogCache.get(owner)
if (hit && hit.mtimeMs === mtimeMs && mtimeMs > 0) return hit.catalog
let catalog: Catalog
try {
catalog = normalizeCatalog(JSON.parse(readFileSync(path, 'utf8')))
@@ -364,8 +372,11 @@ function readCatalog(owner: string): Catalog {
}
catalog = emptyCatalog()
writeFileSync(path, JSON.stringify(catalog, null, 2))
try { mtimeMs = statSync(path).mtimeMs } catch { mtimeMs = Date.now() }
catalogCache.set(owner, { catalog, mtimeMs })
return catalog
}
catalogCache.set(owner, { catalog, mtimeMs })
return catalog
}
@@ -374,6 +385,14 @@ function writeCatalog(owner: string, catalog: Catalog) {
const tmp = catalogPath(owner) + '.tmp'
writeFileSync(tmp, JSON.stringify(catalog, null, 2))
renameSync(tmp, catalogPath(owner))
let mtimeMs = Date.now()
try { mtimeMs = statSync(catalogPath(owner)).mtimeMs } catch { /* ignore */ }
catalogCache.set(owner, { catalog, mtimeMs })
}
function invalidateCatalogCache(owner?: string) {
if (owner) catalogCache.delete(owner)
else catalogCache.clear()
}
function mutate<T>(owner: string, fn: (catalog: Catalog) => T): Promise<T> {
@@ -1252,6 +1271,17 @@ export function stillThumbFile(owner: string, id: string) {
return join(ownerRoot(owner), 'stills', `${id}.thumb.jpg`)
}
/** Sync check — never kicks ffmpeg. Empty string when thumb is missing or stale. */
export function existingStillThumbPath(owner: string, id: string) {
const source = stillPath(owner, id)
const dest = stillThumbFile(owner, id)
if (!existsSync(source) || !existsSync(dest)) return ''
try {
if (statSync(dest).mtimeMs >= statSync(source).mtimeMs) return dest
} catch { return '' }
return ''
}
function removeStillFiles(owner: string, id: string) {
rmSync(stillPath(owner, id), { force: true })
rmSync(stillThumbFile(owner, id), { force: true })