add tagging to tv

This commit is contained in:
Garret Patti
2026-04-05 19:24:28 -04:00
parent 8829188c58
commit 6858c1e8cf
5 changed files with 170 additions and 25 deletions

View File

@@ -76,3 +76,55 @@ export function scanDirectory(
return { path: subpath, entries }
}
/**
* Recursively walks every subdirectory under `subpath` and returns a flat list
* of all files. Directory entries are omitted. Each FileEntry.name is the full
* relative path from the library root (e.g. FolderA/SubFolder/video.mp4).
*/
export function scanDirectoryRecursive(
libraryRoot: string,
libraryId: string,
subpath: string
): DirectoryListing {
let rootAbsPath: string
try {
rootAbsPath = subpath ? resolveAndJail(libraryRoot, subpath) : libraryRoot
} catch {
return { path: subpath, entries: [] }
}
const entries: FileEntry[] = []
function walk(absDir: string, relDir: string): void {
let dirents: fs.Dirent[]
try {
dirents = fs.readdirSync(absDir, { withFileTypes: true })
} catch {
return
}
for (const d of dirents) {
if (HIDDEN_FILES.test(d.name)) continue
const relPath = relDir ? path.join(relDir, d.name) : d.name
if (d.isDirectory()) {
walk(path.join(absDir, d.name), relPath)
} else {
const mediaType = getMediaType(d.name)
const hasThumbnail = mediaType === 'image' || mediaType === 'video'
// name = full relative path from library root so media keys match
const fullRelPath = subpath ? path.join(subpath, relPath) : relPath
entries.push({
name: fullRelPath,
type: 'file',
mediaType,
url: fileApiUrl(libraryId, fullRelPath),
thumbnailUrl: hasThumbnail ? thumbnailApiUrl(libraryId, fullRelPath) : null,
})
}
}
}
walk(rootAbsPath, '')
entries.sort((a, b) => a.name.localeCompare(b.name))
return { path: subpath, entries }
}