Add paginated library JSON and dedicated still thumbs that never stream archive masters.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -13,7 +13,7 @@ export default defineEventHandler(async (event) => {
|
||||
}
|
||||
const etag = fileEtag(path)
|
||||
setHeader(event, 'ETag', etag)
|
||||
setHeader(event, 'Cache-Control', 'private, max-age=86400, stale-while-revalidate=604800')
|
||||
setHeader(event, 'Cache-Control', 'public, max-age=86400')
|
||||
if (getHeader(event, 'if-none-match') === etag) {
|
||||
setResponseStatus(event, 304)
|
||||
return null
|
||||
|
||||
@@ -1,5 +1,26 @@
|
||||
import { publicLibrary, publicLibraryPage } from '~/server/utils/library'
|
||||
|
||||
export default defineEventHandler((event) => {
|
||||
const light = String(getQuery(event).light || '') === '1'
|
||||
const query = getQuery(event)
|
||||
const light = String(query.light || '') === '1'
|
||||
const limitRaw = query.limit
|
||||
const wantsPage = limitRaw != null && String(limitRaw) !== ''
|
||||
|
||||
if (wantsPage) {
|
||||
const page = publicLibraryPage(event, {
|
||||
limit: Number(limitRaw) || 50,
|
||||
cursor: String(query.cursor || ''),
|
||||
folderId: String(query.folderId || query.folder || ''),
|
||||
type: String(query.type || query.filter || 'all')
|
||||
})
|
||||
try {
|
||||
const { owner } = assertLibraryOwner(event)
|
||||
const folderId = page.folders.find(folder => folder.unlocked)?.id
|
||||
void maintainLibrary(owner, folderId).catch(() => null)
|
||||
} catch { /* locked or anonymous */ }
|
||||
return page
|
||||
}
|
||||
|
||||
const library = publicLibrary(event, { light })
|
||||
try {
|
||||
const { owner } = assertLibraryOwner(event)
|
||||
|
||||
@@ -7,13 +7,26 @@ export default defineEventHandler(async (event) => {
|
||||
const still = getStill(owner, id)
|
||||
assertFolderAccess(event, still.folderId)
|
||||
const wantThumb = String(getQuery(event).thumb || '') === '1'
|
||||
const path = wantThumb ? await stillThumbPath(owner, id) : stillPath(owner, still.id)
|
||||
if (!path || !existsSync(path)) {
|
||||
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' })
|
||||
}
|
||||
} else if (!existsSync(path)) {
|
||||
throw createError({ statusCode: 404, statusMessage: 'Image file is missing' })
|
||||
}
|
||||
|
||||
const etag = fileEtag(path)
|
||||
setHeader(event, 'ETag', etag)
|
||||
setHeader(event, 'Cache-Control', wantThumb ? 'private, max-age=86400, stale-while-revalidate=604800' : 'private, max-age=3600')
|
||||
setHeader(
|
||||
event,
|
||||
'Cache-Control',
|
||||
wantThumb ? 'public, max-age=86400' : 'private, max-age=3600'
|
||||
)
|
||||
if (getHeader(event, 'if-none-match') === etag) {
|
||||
setResponseStatus(event, 304)
|
||||
return null
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
import { existsSync, createReadStream } from 'node:fs'
|
||||
import { fileEtag, stillPath, stillThumbPath } from '~/server/utils/library'
|
||||
|
||||
export default defineEventHandler(async (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 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' })
|
||||
}
|
||||
|
||||
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))
|
||||
})
|
||||
@@ -1,9 +1,15 @@
|
||||
import { existsSync } from 'node:fs';
|
||||
import { studio2LoraCatalog } from '../../utils/studio2/lora-catalog';
|
||||
import { publicLibrary, stillPath, clipVideoPath } from '../../utils/library';
|
||||
export default defineEventHandler(event => {
|
||||
const { owner } = assertLibraryOwner(event);
|
||||
const library = publicLibrary(event);
|
||||
const catalog=studio2LoraCatalog(owner);
|
||||
return {...library,loraAliases:catalog.aliases,loraTriggers:catalog.triggers, stills:library.stills.filter(s=>existsSync(stillPath(owner,s.id))), clips:library.clips.filter(c=>existsSync(clipVideoPath(owner,c.id))), tracks:library.tracks || []};
|
||||
});
|
||||
import { studio2LoraCatalog } from '../../utils/studio2/lora-catalog'
|
||||
import { publicLibrary } from '../../utils/library'
|
||||
|
||||
export default defineEventHandler((event) => {
|
||||
const { owner } = assertLibraryOwner(event)
|
||||
// light:true — metadata only; never existsSync every still/clip on the volume
|
||||
const library = publicLibrary(event, { light: true })
|
||||
const catalog = studio2LoraCatalog(owner)
|
||||
return {
|
||||
...library,
|
||||
loraAliases: catalog.aliases,
|
||||
loraTriggers: catalog.triggers,
|
||||
tracks: library.tracks || []
|
||||
}
|
||||
})
|
||||
|
||||
+119
-3
@@ -615,6 +615,121 @@ export function publicLibrary(event: H3Event, opts?: { light?: boolean }) {
|
||||
}
|
||||
}
|
||||
|
||||
/** Paginated metadata page — no file bytes, no existsSync join of the volume. */
|
||||
export function publicLibraryPage(
|
||||
event: H3Event,
|
||||
opts: { limit?: number; cursor?: string; folderId?: string; type?: string } = {}
|
||||
) {
|
||||
const limit = Math.min(Math.max(Number(opts.limit) || 50, 1), 60)
|
||||
const cursor = String(opts.cursor || '')
|
||||
const folderId = String(opts.folderId || '')
|
||||
const typeFilter = String(opts.type || 'all')
|
||||
const full = publicLibrary(event, { light: true })
|
||||
|
||||
type Row = {
|
||||
id: string
|
||||
name: string
|
||||
type: 'still' | 'clip' | 'track'
|
||||
createdAt: number
|
||||
thumbUrl: string
|
||||
folderId: string
|
||||
prompt?: string
|
||||
promptPre?: string
|
||||
promptPost?: string
|
||||
width?: number
|
||||
height?: number
|
||||
role?: string
|
||||
hideInput?: boolean
|
||||
filename?: string
|
||||
duration?: number
|
||||
ext?: string
|
||||
}
|
||||
|
||||
const rows: Row[] = []
|
||||
for (const still of full.stills) {
|
||||
if (folderId && folderId !== 'all' && still.folderId !== folderId) continue
|
||||
if (typeFilter === 'videos' || typeFilter === 'music') continue
|
||||
rows.push({
|
||||
id: still.id,
|
||||
name: still.name || still.filename || still.id,
|
||||
type: 'still',
|
||||
createdAt: still.createdAt || 0,
|
||||
thumbUrl: `/api/library/stills/${still.id}/thumb`,
|
||||
folderId: still.folderId,
|
||||
prompt: still.prompt,
|
||||
promptPre: still.promptPre,
|
||||
promptPost: still.promptPost,
|
||||
width: still.width,
|
||||
height: still.height,
|
||||
role: still.role,
|
||||
hideInput: still.hideInput,
|
||||
filename: still.filename
|
||||
})
|
||||
}
|
||||
for (const clip of full.clips) {
|
||||
if (folderId && folderId !== 'all' && clip.folderId !== folderId) continue
|
||||
if (typeFilter === 'images' || typeFilter === 'music') continue
|
||||
rows.push({
|
||||
id: clip.id,
|
||||
name: clip.name || clip.id,
|
||||
type: 'clip',
|
||||
createdAt: clip.createdAt || 0,
|
||||
thumbUrl: `/api/library/clips/${clip.id}/thumb`,
|
||||
folderId: clip.folderId,
|
||||
prompt: clip.prompt,
|
||||
promptPre: clip.promptPre,
|
||||
promptPost: clip.promptPost,
|
||||
width: clip.width,
|
||||
height: clip.height,
|
||||
duration: clip.duration
|
||||
})
|
||||
}
|
||||
for (const track of full.tracks || []) {
|
||||
if (folderId && folderId !== 'all' && track.folderId !== folderId) continue
|
||||
if (typeFilter === 'images' || typeFilter === 'videos') continue
|
||||
rows.push({
|
||||
id: track.id,
|
||||
name: track.name || track.id,
|
||||
type: 'track',
|
||||
createdAt: track.createdAt || 0,
|
||||
thumbUrl: '',
|
||||
folderId: track.folderId,
|
||||
duration: track.duration,
|
||||
ext: track.ext
|
||||
})
|
||||
}
|
||||
|
||||
rows.sort((a, b) => (b.createdAt || 0) - (a.createdAt || 0) || String(b.id).localeCompare(String(a.id)))
|
||||
|
||||
let start = 0
|
||||
if (cursor) {
|
||||
const idx = rows.findIndex(row => `${row.type}:${row.id}` === cursor)
|
||||
start = idx >= 0 ? idx + 1 : 0
|
||||
}
|
||||
const page = rows.slice(start, start + limit)
|
||||
const last = page[page.length - 1]
|
||||
const nextCursor = page.length === limit && last ? `${last.type}:${last.id}` : null
|
||||
|
||||
const stillIds = new Set(page.filter(r => r.type === 'still').map(r => r.id))
|
||||
const clipIds = new Set(page.filter(r => r.type === 'clip').map(r => r.id))
|
||||
const trackIds = new Set(page.filter(r => r.type === 'track').map(r => r.id))
|
||||
|
||||
return {
|
||||
folders: full.folders,
|
||||
items: page,
|
||||
nextCursor,
|
||||
limit,
|
||||
stills: full.stills.filter(s => stillIds.has(s.id)),
|
||||
clips: full.clips.filter(c => clipIds.has(c.id)),
|
||||
tracks: (full.tracks || []).filter(t => trackIds.has(t.id)),
|
||||
drafts: full.drafts,
|
||||
loraAliases: full.loraAliases,
|
||||
loraTriggers: full.loraTriggers,
|
||||
keepPrompts: full.keepPrompts,
|
||||
total: rows.length
|
||||
}
|
||||
}
|
||||
|
||||
export function assertLibraryOwner(event: H3Event) {
|
||||
const owner = libraryOwnerKey(event)
|
||||
return { owner, catalog: readCatalog(owner) }
|
||||
@@ -830,7 +945,7 @@ export async function ensureScaledThumb(source: string, dest: string) {
|
||||
if (pending) return pending
|
||||
const build = withThumbSlot(async () => {
|
||||
const ok = await buildStillThumb(source, dest)
|
||||
return ok ? dest : source
|
||||
return ok ? dest : ''
|
||||
}).finally(() => {
|
||||
thumbBuilds.delete(key)
|
||||
})
|
||||
@@ -1130,7 +1245,7 @@ function buildStillThumb(source: string, dest: string) {
|
||||
const proc = spawn('ffmpeg', [
|
||||
'-y',
|
||||
'-i', source,
|
||||
'-vf', 'scale=320:-2',
|
||||
'-vf', 'scale=384:-2',
|
||||
'-frames:v', '1',
|
||||
'-q:v', '5',
|
||||
dest
|
||||
@@ -1164,7 +1279,8 @@ export async function stillThumbPath(owner: string, id: string) {
|
||||
if (pending) return pending
|
||||
const build = withThumbSlot(async () => {
|
||||
const ok = await buildStillThumb(source, dest)
|
||||
return ok ? dest : source
|
||||
// Never return the archive master — callers must not stream originals as thumbs
|
||||
return ok ? dest : ''
|
||||
}).finally(() => {
|
||||
thumbBuilds.delete(key)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user