gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchstatus-22-pr5838d6c7|||PR STATUS: #22 pending decision, PR-only rulemrestatus-22-pr/lib/library.hl

5.2 KB

  1. // lib/library.hl — THE SCANNER: walk the library paths, read the tags of new and changed files, remove the vanished
  2. // ones, keep the derived tables and the in-memory order lists (lib/browse.hl) current. The library is only listed and
  3. // read (listDir, changeTime, `head`/`tail`/ffprobe on a file): nothing under it is ever written, moved or touched.
  4. // A file is unchanged when its path, size and change time (hl:fs changeTime) equal the database's: a rescan of an
  5. // unchanged library reads no tag at all.
  6. import { listDir, changeTime, exists } from 'hl:fs'
  7. import { now, monotonic } from 'hl:time'
  8. import { loadSettings } from './settings.hl'
  9. import { songsTable, addSong, replaceSong, removeSong, addScan } from './db.hl'
  10. import { readMp3, readProbe, songFrom } from './tags.hl'
  11. import { storeCover, folderCoverPath, storeCoverFile } from './covers.hl'
  12. import { rebuildCatalog } from './catalog.hl'
  13. import { dropOrderCache } from './state.hl'
  14. import { extOf, envOr } from './util.hl'
  15. import ScanState from './scan-state.hl'
  16. static audioExt = {
  17. 'mp3' = true 'm4a' = true 'mp4' = true 'm4v' = true 'ogg' = true 'oga' = true 'opus' = true 'flac' = true
  18. 'aac' = true 'wav' = true 'ape' = true 'wma' = true 'mkv' = true 'webm' = true
  19. }
  20. // what the status route shows while and after a scan
  21. static scanState = new ScanState()
  22. // A scan run by tools/scan.hl for the server (JUKEBOX_PROGRESS=1) prints its numbers as lines the server reads
  23. static progressOn = envOr('JUKEBOX_PROGRESS', '0') == '1'
  24. static progress = (st) => {
  25. if (progressOn) {
  26. console.log('JB-PROGRESS ' + JSON.stringify({ phase = st.phase seen = st.seen added = st.added updated = st.updated removed = st.removed unreadable = st.unreadable }))
  27. }
  28. }
  29. // the files of the current database by path: { id, changeTime, size }
  30. static knownFiles = () => {
  31. known = {}
  32. for (s of songsTable.iterate(null, null)) {
  33. known[s.path] = { id = s.id changeTime = s.changeTime size = s.size addedAt = s.addedAt }
  34. }
  35. return known
  36. }
  37. // Read one file and store its record. `folderCover` is the cover id of the folder (a function: resolved at most once).
  38. static readSong = (path, size, ct, folderCoverOf) => {
  39. ext = extOf(path)
  40. raw = ext == 'mp3' ? readMp3(path, size) : readProbe(path)
  41. if (raw == null) { return null }
  42. let coverId = null
  43. if (raw.cover != null) { coverId = storeCover(raw.cover.data, raw.cover.mime) }
  44. raw.cover = null
  45. if (coverId == null) { coverId = folderCoverOf() }
  46. return songFrom(raw, { path = path size = size changeTime = ct }, coverId)
  47. }
  48. // One scan of all library paths. Answers the finished state's numbers. A scan already running is not started twice.
  49. static scanLibrary = (paths) => {
  50. st = scanState
  51. if (st.running) { return null }
  52. st.running = true
  53. st.phase = 'starting'
  54. st.seen = 0
  55. st.added = 0
  56. st.updated = 0
  57. st.removed = 0
  58. st.unreadable = 0
  59. st.lastError = ''
  60. st.startedAt = now()
  61. t0 = monotonic()
  62. known = knownFiles()
  63. seen = {}
  64. okRoots = []
  65. let folderCovers = {}
  66. for (root of paths) {
  67. if (!exists(root)) {
  68. st.lastError = 'library path missing, nothing removed under it: ' + root
  69. continue
  70. }
  71. okRoots.push(root)
  72. st.phase = 'walking ' + root
  73. stack = [root]
  74. while (stack.length > 0) {
  75. dir = stack.pop()
  76. entries = []
  77. for (e of listDir(dir)) { entries.push(e) }
  78. let coverPath = folderCoverPath(entries)
  79. let coverDone = false
  80. let coverId = null
  81. folderCoverOf = () => {
  82. if (!coverDone) {
  83. coverDone = true
  84. coverId = coverPath == null ? null : storeCoverFile(coverPath)
  85. }
  86. return coverId
  87. }
  88. for (e of entries) {
  89. if (e.type == 'directory') {
  90. stack.push(e.path)
  91. } else if (e.type == 'file' && audioExt[extOf(e.path)] == true) {
  92. path = e.path
  93. seen[path] = true
  94. st.seen = st.seen + 1
  95. if (st.seen % 100 == 0) { progress(st) }
  96. ct = changeTime(path)
  97. k = known[path]
  98. if (k != null && k.changeTime == ct && k.size == e.size) { continue }
  99. rec = readSong(path, e.size, ct, folderCoverOf)
  100. if (rec == null) {
  101. st.unreadable = st.unreadable + 1
  102. continue
  103. }
  104. if (k == null) {
  105. rec.addedAt = now()
  106. addSong(rec)
  107. st.added = st.added + 1
  108. } else {
  109. rec.addedAt = k.addedAt
  110. replaceSong(k.id, rec)
  111. st.updated = st.updated + 1
  112. }
  113. }
  114. }
  115. }
  116. }
  117. // the vanished files go, but only below a library path that could be read
  118. st.phase = 'removing vanished files'
  119. progress(st)
  120. for (p in known) {
  121. if (seen[p] == true) { continue }
  122. let under = false
  123. for (r of okRoots) {
  124. if (p.startsWith(r + '/')) { under = true }
  125. }
  126. if (under) {
  127. removeSong(known[p].id)
  128. st.removed = st.removed + 1
  129. }
  130. }
  131. changed = st.added + st.updated + st.removed
  132. if (changed > 0) {
  133. st.phase = 'building albums and artists'
  134. progress(st)
  135. rebuildCatalog()
  136. dropOrderCache()
  137. }
  138. st.lastSeconds = (monotonic() - t0) / 1000000000
  139. st.finishedAt = now()
  140. addScan({ at = st.startedAt seconds = st.lastSeconds seen = st.seen added = st.added updated = st.updated removed = st.removed unreadable = st.unreadable })
  141. st.phase = 'idle'
  142. st.running = false
  143. return { seen = st.seen added = st.added updated = st.updated removed = st.removed unreadable = st.unreadable seconds = st.lastSeconds }
  144. }
  145. static scanNow = () => { return scanLibrary(loadSettings().libraryPaths) }

Branches

Latest commits

  • 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
  • a01f3aa8ui gate: back to the top before the sort checks (the screen reopens where it was left)mre
  • f7936c20Lists keep their scroll position across a reload (jukebox#20): per-list offsets in storage/ui.json, gate jb20mre
  • d67906devirtual-list and icons from components.hybriel e5b98ec (centerId, scrollTop, onScroll)mre
  • df7920ceRecords: R3 startedmre
  • 0549f038Mission 010: jb20 livemre
  • 273494e2STATUS rewrittenmre
  • 227c9b96Records: jb19 livemre
  • 9f365376README: the bar's controls (jb19)mre
  • d18851fdjb19: pressed switches look different (whole selector), queue column cannot widen past the panel, gates follow the new bar (jb3, jb11, jb15)mre