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
- 8ed6da45|||PR README: media-player pin df9e7dfmre
- d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre
- 89cdabb1Vendor Hybriel day41 (57054d9e): plugin dirs, set .somre
- 1fecd6a7Merge branch 'records-day39' into mainJukebox
- d6b3e068|||PR Records: day39 live (PR #2), mission 011 PR step, statusmre
- 5fa19e89Merge branch 'day39' into mainJukebox
- cfe95440|||PR day39 re-vendor: Hybriel 51e921d7 (bin 6b481aa7), ids kebab-case since hybriel#256: quoted state rule and tests use jb-shuffle / jb-repeatmre
- f3614c9bRe-vendor to Hybriel day39 (51e921d7, bin 6b481aa7)mre
- 9f5c6532Merge branch 'status-22-pr' into mainJukebox
- 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