jukebox
All repositories: gitoria
5.2 KB
// lib/library.hl — THE SCANNER: walk the library paths, read the tags of new and changed files, remove the vanished// ones, keep the derived tables and the in-memory order lists (lib/browse.hl) current. The library is only listed and// read (listDir, changeTime, `head`/`tail`/ffprobe on a file): nothing under it is ever written, moved or touched.// A file is unchanged when its path, size and change time (hl:fs changeTime) equal the database's: a rescan of an// unchanged library reads no tag at all.import { listDir, changeTime, exists } from 'hl:fs'import { now, monotonic } from 'hl:time'import { loadSettings } from './settings.hl'import { songsTable, addSong, replaceSong, removeSong, addScan } from './db.hl'import { readMp3, readProbe, songFrom } from './tags.hl'import { storeCover, folderCoverPath, storeCoverFile } from './covers.hl'import { rebuildCatalog } from './catalog.hl'import { dropOrderCache } from './state.hl'import { extOf, envOr } from './util.hl'import ScanState from './scan-state.hl'static audioExt = {'mp3' = true 'm4a' = true 'mp4' = true 'm4v' = true 'ogg' = true 'oga' = true 'opus' = true 'flac' = true'aac' = true 'wav' = true 'ape' = true 'wma' = true 'mkv' = true 'webm' = true}// what the status route shows while and after a scanstatic scanState = new ScanState()// A scan run by tools/scan.hl for the server (JUKEBOX_PROGRESS=1) prints its numbers as lines the server readsstatic progressOn = envOr('JUKEBOX_PROGRESS', '0') == '1'static progress = (st) => {if (progressOn) {console.log('JB-PROGRESS ' + JSON.stringify({ phase = st.phase seen = st.seen added = st.added updated = st.updated removed = st.removed unreadable = st.unreadable }))}}// the files of the current database by path: { id, changeTime, size }static knownFiles = () => {known = {}for (s of songsTable.iterate(null, null)) {known[s.path] = { id = s.id changeTime = s.changeTime size = s.size addedAt = s.addedAt }}return known}// Read one file and store its record. `folderCover` is the cover id of the folder (a function: resolved at most once).static readSong = (path, size, ct, folderCoverOf) => {ext = extOf(path)raw = ext == 'mp3' ? readMp3(path, size) : readProbe(path)if (raw == null) { return null }let coverId = nullif (raw.cover != null) { coverId = storeCover(raw.cover.data, raw.cover.mime) }raw.cover = nullif (coverId == null) { coverId = folderCoverOf() }return songFrom(raw, { path = path size = size changeTime = ct }, coverId)}// One scan of all library paths. Answers the finished state's numbers. A scan already running is not started twice.static scanLibrary = (paths) => {st = scanStateif (st.running) { return null }st.running = truest.phase = 'starting'st.seen = 0st.added = 0st.updated = 0st.removed = 0st.unreadable = 0st.lastError = ''st.startedAt = now()t0 = monotonic()known = knownFiles()seen = {}okRoots = []let folderCovers = {}for (root of paths) {if (!exists(root)) {st.lastError = 'library path missing, nothing removed under it: ' + rootcontinue}okRoots.push(root)st.phase = 'walking ' + rootstack = [root]while (stack.length > 0) {dir = stack.pop()entries = []for (e of listDir(dir)) { entries.push(e) }let coverPath = folderCoverPath(entries)let coverDone = falselet coverId = nullfolderCoverOf = () => {if (!coverDone) {coverDone = truecoverId = coverPath == null ? null : storeCoverFile(coverPath)}return coverId}for (e of entries) {if (e.type == 'directory') {stack.push(e.path)} else if (e.type == 'file' && audioExt[extOf(e.path)] == true) {path = e.pathseen[path] = truest.seen = st.seen + 1if (st.seen % 100 == 0) { progress(st) }ct = changeTime(path)k = known[path]if (k != null && k.changeTime == ct && k.size == e.size) { continue }rec = readSong(path, e.size, ct, folderCoverOf)if (rec == null) {st.unreadable = st.unreadable + 1continue}if (k == null) {rec.addedAt = now()addSong(rec)st.added = st.added + 1} else {rec.addedAt = k.addedAtreplaceSong(k.id, rec)st.updated = st.updated + 1}}}}}// the vanished files go, but only below a library path that could be readst.phase = 'removing vanished files'progress(st)for (p in known) {if (seen[p] == true) { continue }let under = falsefor (r of okRoots) {if (p.startsWith(r + '/')) { under = true }}if (under) {removeSong(known[p].id)st.removed = st.removed + 1}}changed = st.added + st.updated + st.removedif (changed > 0) {st.phase = 'building albums and artists'progress(st)rebuildCatalog()dropOrderCache()}st.lastSeconds = (monotonic() - t0) / 1000000000st.finishedAt = now()addScan({ at = st.startedAt seconds = st.lastSeconds seen = st.seen added = st.added updated = st.updated removed = st.removed unreadable = st.unreadable })st.phase = 'idle'st.running = falsereturn { seen = st.seen added = st.added updated = st.updated removed = st.removed unreadable = st.unreadable seconds = st.lastSeconds }}static scanNow = () => { return scanLibrary(loadSettings().libraryPaths) }
Branches
Latest commits
- 33072bb5|||PR Record day72 gates and Chrome proofmre
- 95f2cd73|||PR Use typed parent methods for screen playback and local server facesmre
- 6422a020Sync media-player at 51c3818mre
- b582ba1cVendor complete Hybriel day72 plugin setmre
- 02dccbc9Merge branch 'records-1119' into mainJukebox
- a39e1c23|||PR Records: missions 013-015, reconnect fix livemre
- fc7db075Merge branch 'reconnect-retry' into mainJukebox
- ac6f4c6fL2: plain declaration for a in pickedNextmre
- 2f335086Review round 2: only an explicit queue pick drops pend (not the automatic advance); jb23 records every source changemre
- 3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre
- 7a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre
- 1f0cd77bMerge branch 'records-day41' into mainJukebox
- 67be229f|||PR Records: day41 live (PR #4), mission 012, statusmre
- 0bb027daconductor: caramboleyo is the user, not owner/creatorApollo
- 04f5d9d5Merge branch 'day41' into mainJukebox
- 8ed6da45|||PR README: media-player pin df9e7dfmre
- d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre
- 89cdabb1Vendor Hybriel day41 (57054d9e): plugin dirs, set .somre
- 1fecd6a7Merge branch 'records-day39' into mainJukebox
- d6b3e068|||PR Records: day39 live (PR #2), mission 011 PR step, statusmre