gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitbf5167dabf5167dajukebox: index keys without commas (mpackdb loses them after a reopen), comma fixture and gate checksmrebf5167da/lib/covers.hl

2.8 KB

  1. // lib/covers.hl — THE COVER CACHE. A cover is an embedded picture (APIC) or a picture file in the song's folder
  2. // (folder.jpg, cover.jpg, front.jpg, AlbumArt*.jpg, ...). Either way it is copied into storage/covers/<content hash>.<ext>
  3. // once (the same picture from many songs is one file) and indexed in the covers table. Nothing is written into the library.
  4. import { readBytes, writeFile, exists } from 'hl:fs'
  5. import { textDigest } from 'hl:fs'
  6. import { coverDir } from './util.hl'
  7. import { coverByHash, addCover } from './db.hl'
  8. static extOfMime = (mime) => {
  9. if (mime == 'image/png') { return 'png' }
  10. if (mime == 'image/gif') { return 'gif' }
  11. if (mime == 'image/webp') { return 'webp' }
  12. if (mime == 'image/bmp') { return 'bmp' }
  13. return 'jpg'
  14. }
  15. static mimeOfExt = (ext) => {
  16. if (ext == 'png') { return 'image/png' }
  17. if (ext == 'gif') { return 'image/gif' }
  18. if (ext == 'webp') { return 'image/webp' }
  19. if (ext == 'bmp') { return 'image/bmp' }
  20. return 'image/jpeg'
  21. }
  22. // store a picture (a Bytes) and answer its id (the first 32 hex digits of the hash of its content); null for an empty one
  23. static storeCover = (data, mime) => {
  24. if (data == null || data.length < 16) { return null }
  25. hash = textDigest(data.hex()).slice(0, 32)
  26. if (coverByHash(hash) != null) { return hash }
  27. ext = extOfMime(mime)
  28. writeFile(coverDir + '/' + hash + '.' + ext, data, 420)
  29. addCover(hash, ext, mimeOfExt(ext), data.length)
  30. return hash
  31. }
  32. // is this file name (lower case) a folder cover?
  33. static isCoverName = (n) => {
  34. if (n == 'folder.jpg' || n == 'folder.jpeg' || n == 'folder.png' || n == 'cover.jpg' || n == 'cover.jpeg' || n == 'cover.png') { return true }
  35. if (n == 'front.jpg' || n == 'front.jpeg' || n == 'front.png' || n == 'albumart.jpg' || n == 'albumartsmall.jpg') { return true }
  36. return n.startsWith('albumart') && n.endsWith('.jpg')
  37. }
  38. // the path of the cover file among a folder's entries (listDir records), or null; folder.jpg wins over albumart*.jpg
  39. static folderCoverPath = (entries) => {
  40. let best = null
  41. let bestRank = 99
  42. for (e of entries) {
  43. if (e.type != 'file') { continue }
  44. n = e.name.toLowerCase()
  45. if (!isCoverName(n)) { continue }
  46. rank = n.startsWith('folder.') ? 0 : (n.startsWith('cover.') ? 1 : (n.startsWith('front.') ? 2 : 3))
  47. if (rank < bestRank) {
  48. best = e.path
  49. bestRank = rank
  50. }
  51. }
  52. return best
  53. }
  54. // store the cover file of a folder; answers its id or null
  55. static storeCoverFile = (path) => {
  56. data = readBytes(path)
  57. if (data == null) { return null }
  58. ext = path.slice(path.lastIndexOf('.') + 1).toLowerCase()
  59. return storeCover(data, mimeOfExt(ext == 'jpeg' ? 'jpg' : ext))
  60. }
  61. // what the cover route answers: { bytes, mime } or null
  62. static coverFile = (id) => {
  63. row = coverByHash(id)
  64. if (row == null) { return null }
  65. path = coverDir + '/' + row.hash + '.' + row.ext
  66. if (!exists(path)) { return null }
  67. return { bytes = readBytes(path) mime = row.mime }
  68. }

Branches

Latest commits

  • bf5167dajukebox: index keys without commas (mpackdb loses them after a reopen), comma fixture and gate checksmre
  • 28a82fa2jukebox: README, STATUS, LOG, DECISIONSmre
  • d13987dfjukebox: library scanner, ID3 reader, mpackdb database and JSON API, with fixtures and a gatemre