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
- 7b048c1bRecords: jb19 done, L2 live stepmre
- 665185aeMission 009: jb19 livemre
- 47b2d3beRecords: P1 resumedmre
- 8e326e94Records: resetKey blockedmre
- f72dd7edRecords: icons comingmre
- b264e5deRecords: P1 icons stopmre
- 514d7cbeRecords: mission 008 startedmre
- 61c40127Mission 008: #19 + #21mre
- 6dcaeb46Records: resetKey planmre
- c5dbf2ecRecords: R2 resultmre
- 956069b8Records: R1 result, mission 007mre
- 3c8a2e12Mission 007: #20 sort scrollmre
- deb8cef5Records: mission 006 startedmre
- 4f0ad42fMission 006: jukebox#20mre
- 800cae06Records: #21 planmre
- 64bc7823Records: #21 causemre
- 76639655Records: B1 questionmre
- d7906b36Records: mission 005 startedmre
- d3df773cMission 005: doc linemre
- 6a5d840aMission 005: jukebox#22mre