gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitd7906b36d7906b36Records: mission 005 startedmred7906b36/lib/settings.hl

2.1 KB

  1. // lib/settings.hl — THE SETTINGS, kept in storage/settings.json: the library paths to watch and how often to rescan.
  2. // They are the app's own data; the library is only ever read.
  3. import { readFile, writeFile, exists, mkDir } from 'hl:fs'
  4. import { storageRoot, settingsFile, coverDir, storageDir } from './util.hl'
  5. static defaultSettings = () => {
  6. return { libraryPaths = ['/media/SLOW1TB1/Musik'] rescanMinutes = 30 }
  7. }
  8. // the storage folders exist (the first start)
  9. static ensureStorage = () => {
  10. mkDir(storageRoot)
  11. mkDir(storageDir)
  12. mkDir(coverDir)
  13. }
  14. static loadSettings = () => {
  15. d = defaultSettings()
  16. if (!exists(settingsFile)) { return d }
  17. text = readFile(settingsFile)
  18. if (text == null || !text.trim().startsWith('{')) { return d }
  19. j = JSON.parse(text)
  20. if (j == null) { return d }
  21. if (j.libraryPaths != null && j.libraryPaths.length != null && j.libraryPaths.length > 0) {
  22. paths = []
  23. for (p of j.libraryPaths) {
  24. if (hlTypeName(p) == 'String' && p.startsWith('/')) { paths.push(p) }
  25. }
  26. if (paths.length > 0) { d.libraryPaths = paths }
  27. }
  28. if (j.rescanMinutes != null && hlTypeName(j.rescanMinutes) == 'Number' && j.rescanMinutes >= 1) { d.rescanMinutes = j.rescanMinutes }
  29. return d
  30. }
  31. // the input is checked field by field; answers the saved settings, or { error } for something wrong
  32. static saveSettings = (given) => {
  33. if (given == null || given.libraryPaths == null || given.libraryPaths.length == null) { return { error = 'libraryPaths must be a list of absolute folder paths' } }
  34. paths = []
  35. for (p of given.libraryPaths) {
  36. if (hlTypeName(p) != 'String' || !p.startsWith('/') || p.includes('..')) { return { error = 'every library path must be an absolute folder path' } }
  37. paths.push(p)
  38. }
  39. if (paths.length == 0) { return { error = 'at least one library path is needed' } }
  40. let mins = 30
  41. if (given.rescanMinutes != null) {
  42. if (hlTypeName(given.rescanMinutes) != 'Number' || given.rescanMinutes < 1) { return { error = 'rescanMinutes must be a number of at least 1' } }
  43. mins = given.rescanMinutes
  44. }
  45. s = { libraryPaths = paths rescanMinutes = mins }
  46. ensureStorage()
  47. writeFile(settingsFile, JSON.stringify(s), 420)
  48. return s
  49. }

Branches

Latest commits

  • d7906b36Records: mission 005 startedmre
  • d3df773cMission 005: doc linemre
  • 6a5d840aMission 005: jukebox#22mre
  • 13e5e748Records: mission 004 startedmre
  • a8098271Mission 004: jukebox#21mre
  • df8f5205Records: jukebox#19-21, #11 closedmre
  • b7a4e692Records: hybriel#298mre
  • 9bcbb017Records: day38b waits for day39 fixmre
  • f80504e1Records: day38b repro sentmre
  • 8048258bRecords: day38b jb3 regression, L1 Chromes stoppedmre
  • 68aace7fRecords: mission 003 day38bmre
  • d105d8a9Records: mpackdb integrity check cleanmre
  • 6e980c53Records: mpackdb rule refinementmre
  • 6d335ac4Records: storage copy rulingmre
  • 7eb2898dRecords: mpackdb second-opener rule until day38mre
  • b011f98eRecords: style form live on day35mre
  • 60d90cb2Records: L1 woken for the restartmre
  • 75ae544fRecords: restart waits for the connection to closemre
  • e0b75951Records: L1 merged and pushed, restart waits for an open connectionmre
  • f2b5db98styles: format styles with nested rules and bare queriesmre