jukebox
All repositories: gitoria
3.8 KB
// lib/covers.hl — THE COVER CACHE. A cover is an embedded picture (APIC) or a picture file in the song's folder// (folder.jpg, cover.jpg, front.jpg, AlbumArt*.jpg, ...). Either way it is copied into storage/covers/<content hash>.<ext>// once (the same picture from many songs is one file) and indexed in the covers table. Nothing is written into the library.import { readBytes, writeFile, exists, mkDir } from 'hl:fs'import { run } from 'hl:proc'import { textDigest } from 'hl:fs'import { coverDir } from './util.hl'import { coverByHash, addCover } from './db.hl'static extOfMime = (mime) => {if (mime == 'image/png') { return 'png' }if (mime == 'image/gif') { return 'gif' }if (mime == 'image/webp') { return 'webp' }if (mime == 'image/bmp') { return 'bmp' }return 'jpg'}static mimeOfExt = (ext) => {if (ext == 'png') { return 'image/png' }if (ext == 'gif') { return 'image/gif' }if (ext == 'webp') { return 'image/webp' }if (ext == 'bmp') { return 'image/bmp' }return 'image/jpeg'}// store a picture (a Bytes) and answer its id (the first 32 hex digits of the hash of its content); null for an empty onestatic storeCover = (data, mime) => {if (data == null || data.length < 16) { return null }hash = textDigest(data.hex()).slice(0, 32)if (coverByHash(hash) != null) { return hash }ext = extOfMime(mime)writeFile(coverDir + '/' + hash + '.' + ext, data, 420)addCover(hash, ext, mimeOfExt(ext), data.length)return hash}// is this file name (lower case) a folder cover?static isCoverName = (n) => {if (n == 'folder.jpg' || n == 'folder.jpeg' || n == 'folder.png' || n == 'cover.jpg' || n == 'cover.jpeg' || n == 'cover.png') { return true }if (n == 'front.jpg' || n == 'front.jpeg' || n == 'front.png' || n == 'albumart.jpg' || n == 'albumartsmall.jpg') { return true }return n.startsWith('albumart') && n.endsWith('.jpg')}// the path of the cover file among a folder's entries (listDir records), or null; folder.jpg wins over albumart*.jpgstatic folderCoverPath = (entries) => {let best = nulllet bestRank = 99for (e of entries) {if (e.type != 'file') { continue }n = e.name.toLowerCase()if (!isCoverName(n)) { continue }rank = n.startsWith('folder.') ? 0 : (n.startsWith('cover.') ? 1 : (n.startsWith('front.') ? 2 : 3))if (rank < bestRank) {best = e.pathbestRank = rank}}return best}// store the cover file of a folder; answers its id or nullstatic storeCoverFile = (path) => {data = readBytes(path)if (data == null) { return null }ext = path.slice(path.lastIndexOf('.') + 1).toLowerCase()return storeCover(data, mimeOfExt(ext == 'jpeg' ? 'jpg' : ext))}// what the cover route answers: { bytes, mime } or nullstatic coverFile = (id) => {row = coverByHash(id)if (row == null) { return null }path = coverDir + '/' + row.hash + '.' + row.extif (!exists(path)) { return null }return { bytes = readBytes(path) mime = row.mime }}// 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/:// a phone shows 20 thumbnails at a time, and the cached pictures are up to hundreds of kilobytes. { bytes, mime } or nullstatic thumbFile = (id) => {row = coverByHash(id)if (row == null) { return null }src = coverDir + '/' + row.hash + '.' + row.extif (!exists(src)) { return null }small = coverDir + '/thumbs/' + row.hash + '.jpg'if (!exists(small)) {mkDir(coverDir + '/thumbs')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 })// a picture ffmpeg cannot read: the big one is served insteadif (r == null || r.exit != 0 || !exists(small)) { return { bytes = readBytes(src) mime = row.mime } }}return { bytes = readBytes(small) mime = 'image/jpeg' }}
Branches
Latest commits
- 14abde3bjukebox: records: Atlas's records moved to worldapi-conductor/archivemre
- 0433e9a8jukebox: records: gitoria remote added and pushedmre
- 1722d707jukebox: records: repo creation handed to Gitoriamre
- b42e7dfbjukebox: conductor records (CONDUCTOR.md, STATUS.md), old STATUS archivedmre
- 1b0d0bc1jukebox: a startup error prints its message before exit 1 (hybriel#247)mre
- b19c478bjukebox JB18 (jukebox#17): onto Hybriel set-day31 (b287cd82, contains the SIGPIPE fix 6d2ca454): plugins/core and plugins/web re-vendored, bin/ from the set; a client that closes /stream while it is written no longer kills the server (90 aborted streams survived, the old bin died with exit 141 on the first)mre
- 73867a2ejukebox#2: Style blocks in the language's nested form (rules named after the View's own elements, pseudo-classes nested, merged duplicates); the same CSS rules, the pages look the samemre
- 06c548d6jukebox JB15: report, log linemre
- 980859cbjukebox JB15 (jukebox#15): the now-playing overlay on a short screen (tablet or phone on its side) gave the queue 12 to 100 px because the big cover took the room; the cover now shrinks with the screen height and goes away under 41rem, the queue keeps 3+ rows; gate tests/jb15.mjs at 8 sizesmre
- 9efc40ffjukebox#12 #14: Grok session 1793246574527-181160mre
- 7d58b36ejukebox JB13 (jukebox#13): Next and Previous ask for the songs after / before the current song's id in the current order, not for place + 1; gate tests/jb13.mjs checks Next with the queue down to one song after the order changedmre
- 51358794jukebox JB14 (jukebox#14): the Songs screen opens sorted the way it was left (kept on the server in storage/ui.json), the sort control shows the sort the rows really have (selected options, autocomplete off); gate tests/jb14.mjsmre
- cc68a063jukebox JB13 (jukebox#13): a tapped song plays the song that was tapped, found by its id in the list as it is now (not by its row number, which counted plays move); the queue goes on from its place there, the rows refresh on the tap; gate tests/jb13.mjs (songs by plays, favourites)mre
- 88d3d5c1jukebox JB12b (jukebox#12): the scroll handle is big (2.75rem x 4rem, accent colour, grip) and drawn on every device, no media query; native bar hidden; gate tests/touch.mjs checks the drawn width with and without touchmre
- b58e4495jukebox JB11 (jukebox#11): the next song starts in the ended event on the same audio element, so it also starts with the screen locked; gate tests/jb11.mjs (hidden tab, no frames)mre
- 12a26233jukebox JB12 (jukebox#12): the long lists get a scroll handle a finger can use on touch screens (44 px tap area, slim look, drag through all rows), gate tests/touch.mjsmre
- 0f2157a9jukebox JV1: onto Hybriel set-day27b: readBytes and stat for /stream and the mp3 tags (no dd, head or tail), startup fault exits 1, fs and mpackdb plugins re-vendored; songs with a comma in the title are indexed againmre
- 63d0f2eejukebox ST1 (jukebox#2): styles of main, songs and search moved into their own Style blocks (styles.hl 81 to 8 rules, pixel-identical at 390 and 1280); artist and album wait for an hl:web fixmre
- c5b8fde9jukebox JB3b: keyboard gap repro as a reportmre
- 9daebb4bjukebox JB3b: shuffle, now-playing overlay, next and previous, play counting, media session, gates; keyboard left open (Hybriel gap)mre