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
- 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
- 5fa19e89Merge branch 'day39' into mainJukebox
- cfe95440|||PR day39 re-vendor: Hybriel 51e921d7 (bin 6b481aa7), ids kebab-case since hybriel#256: quoted state rule and tests use jb-shuffle / jb-repeatmre
- f3614c9bRe-vendor to Hybriel day39 (51e921d7, bin 6b481aa7)mre
- 9f5c6532Merge branch 'status-22-pr' into mainJukebox
- 5838d6c7|||PR STATUS: #22 pending decision, PR-only rulemre
- 29d2029cRecords: PR-only rule (Kunibert)mre
- adacb0abRecords: neutral ticket rule, #22 decision recordedmre
- b2d6f121Records: #308 hold liftedmre
- 7ab67533Records: day39 on hold (hybriel#308)mre
- 22f21739Records: note rebase before mergemre
- f7368ea3Records: mission 011 day39, waits for lisbethmre
- 9515d2cdRecords: jb20 livemre
- 019f6601ui gate: comment names the open bug components#27 (workaround, not fixed)mre
- 277a2722README: provenance linemre
- 4169ca6aREADME: sync pinmre