gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain10f8a8c8Merge branch 'code-order' into mainJukeboxmain/lib/covers.hl

3.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, mkDir } from 'hl:fs'
  5. import { run } from 'hl:proc'
  6. import { textDigest } from 'hl:fs'
  7. import { coverDir } from './util.hl'
  8. import { coverByHash, addCover } from './db.hl'
  9. static extOfMime = (mime) => {
  10. if (mime == 'image/png') { return 'png' }
  11. if (mime == 'image/gif') { return 'gif' }
  12. if (mime == 'image/webp') { return 'webp' }
  13. if (mime == 'image/bmp') { return 'bmp' }
  14. return 'jpg'
  15. }
  16. static mimeOfExt = (ext) => {
  17. if (ext == 'png') { return 'image/png' }
  18. if (ext == 'gif') { return 'image/gif' }
  19. if (ext == 'webp') { return 'image/webp' }
  20. if (ext == 'bmp') { return 'image/bmp' }
  21. return 'image/jpeg'
  22. }
  23. // 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
  24. static storeCover = (data, mime) => {
  25. if (data == null || data.length < 16) { return null }
  26. hash = textDigest(data.hex()).slice(0, 32)
  27. if (coverByHash(hash) != null) { return hash }
  28. ext = extOfMime(mime)
  29. writeFile(coverDir + '/' + hash + '.' + ext, data, 420)
  30. addCover(hash, ext, mimeOfExt(ext), data.length)
  31. return hash
  32. }
  33. // is this file name (lower case) a folder cover?
  34. static isCoverName = (n) => {
  35. if (n == 'folder.jpg' || n == 'folder.jpeg' || n == 'folder.png' || n == 'cover.jpg' || n == 'cover.jpeg' || n == 'cover.png') { return true }
  36. if (n == 'front.jpg' || n == 'front.jpeg' || n == 'front.png' || n == 'albumart.jpg' || n == 'albumartsmall.jpg') { return true }
  37. return n.startsWith('albumart') && n.endsWith('.jpg')
  38. }
  39. // the path of the cover file among a folder's entries (listDir records), or null; folder.jpg wins over albumart*.jpg
  40. static folderCoverPath = (entries) => {
  41. let best = null
  42. let bestRank = 99
  43. for (e of entries) {
  44. if (e.type != 'file') { continue }
  45. n = e.name.toLowerCase()
  46. if (!isCoverName(n)) { continue }
  47. rank = n.startsWith('folder.') ? 0 : (n.startsWith('cover.') ? 1 : (n.startsWith('front.') ? 2 : 3))
  48. if (rank < bestRank) {
  49. best = e.path
  50. bestRank = rank
  51. }
  52. }
  53. return best
  54. }
  55. // store the cover file of a folder; answers its id or null
  56. static storeCoverFile = (path) => {
  57. data = readBytes(path)
  58. if (data == null) { return null }
  59. ext = path.slice(path.lastIndexOf('.') + 1).toLowerCase()
  60. return storeCover(data, mimeOfExt(ext == 'jpeg' ? 'jpg' : ext))
  61. }
  62. // what the cover route answers: { bytes, mime } or null
  63. static coverFile = (id) => {
  64. row = coverByHash(id)
  65. if (row == null) { return null }
  66. path = coverDir + '/' + row.hash + '.' + row.ext
  67. if (!exists(path)) { return null }
  68. return { bytes = readBytes(path) mime = row.mime }
  69. }
  70. // THE SMALL PICTURE for the lists (96 x 96, cropped square), made once with ffmpeg from the cached cover and kept in storage/covers/thumbs/:
  71. // a phone shows 20 thumbnails at a time, and the cached pictures are up to hundreds of kilobytes. { bytes, mime } or null
  72. static thumbFile = (id) => {
  73. row = coverByHash(id)
  74. if (row == null) { return null }
  75. src = coverDir + '/' + row.hash + '.' + row.ext
  76. if (!exists(src)) { return null }
  77. small = coverDir + '/thumbs/' + row.hash + '.jpg'
  78. if (!exists(small)) {
  79. mkDir(coverDir + '/thumbs')
  80. r = run(['ffmpeg', '-v', 'error', '-y', '-i', src, '-vf', 'scale=96:96:force_original_aspect_ratio=increase,crop=96:96', '-frames:v', '1', '-q:v', '5', small], { timeout = 30 })
  81. // a picture ffmpeg cannot read: the big one is served instead
  82. if (r == null || r.exit != 0 || !exists(small)) { return { bytes = readBytes(src) mime = row.mime } }
  83. }
  84. return { bytes = readBytes(small) mime = 'image/jpeg' }
  85. }

Branches

Latest commits

  • 10f8a8c8Merge branch 'code-order' into mainJukebox
  • 204fc828Merge branch 'day72' into mainJukebox
  • 95f5faae|||PR jukebox#7 Drop needless let declarationsmre
  • 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