media viewer consistency
This commit is contained in:
@@ -33,18 +33,37 @@ export async function GET(request: NextRequest) {
|
||||
? scanDirectoryRecursive(root, libraryId, subpath)
|
||||
: scanDirectory(root, libraryId, subpath)
|
||||
|
||||
// Annotate image entries with whether they have extracted text
|
||||
// Annotate image files with hasExtractedText, and directories if any descendant has extracted text
|
||||
const db = getDb()
|
||||
const rows = db
|
||||
.prepare('SELECT item_key FROM media_items WHERE library_id = ? AND extracted_text IS NOT NULL')
|
||||
.all(libraryId) as { item_key: string }[]
|
||||
const withText = new Set(rows.map((r) => r.item_key))
|
||||
|
||||
// Build a set of all ancestor directory relative paths that contain at least one item with text
|
||||
// e.g. item_key "lib:mixed_file:manga%2Fch1%2Fp1.jpg" → ancestors "manga", "manga/ch1"
|
||||
const dirsWithText = new Set<string>()
|
||||
const keyPrefix = `${libraryId}:mixed_file:`
|
||||
for (const key of withText) {
|
||||
const decoded = decodeURIComponent(key.slice(keyPrefix.length))
|
||||
const parts = decoded.split('/')
|
||||
for (let i = 1; i < parts.length; i++) {
|
||||
dirsWithText.add(parts.slice(0, i).join('/'))
|
||||
}
|
||||
}
|
||||
|
||||
listing.entries = listing.entries.map((e) => {
|
||||
if (e.type !== 'file' || e.mediaType !== 'image') return e
|
||||
const relPath = subpath ? path.join(subpath, e.name) : e.name
|
||||
const itemKey = `${libraryId}:mixed_file:${encodeURIComponent(relPath)}`
|
||||
return { ...e, hasExtractedText: withText.has(itemKey) }
|
||||
if (e.type === 'file') {
|
||||
if (e.mediaType !== 'image') return e
|
||||
const relPath = subpath ? path.join(subpath, e.name) : e.name
|
||||
const itemKey = `${libraryId}:mixed_file:${encodeURIComponent(relPath)}`
|
||||
return { ...e, hasExtractedText: withText.has(itemKey) }
|
||||
}
|
||||
if (e.type === 'directory') {
|
||||
const dirRel = subpath ? `${subpath}/${e.name}` : e.name
|
||||
if (dirsWithText.has(dirRel)) return { ...e, hasExtractedText: true }
|
||||
}
|
||||
return e
|
||||
})
|
||||
|
||||
return NextResponse.json(listing)
|
||||
|
||||
Reference in New Issue
Block a user