A music player that connects to your cloud/distributed storage.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore: improve artwork storage & lookup, store by cid

+32 -5
+32 -5
src/components/processor/artwork/worker.js
··· 1 1 import * as IDB from "idb-keyval"; 2 2 3 3 import { IDB_ARTWORK_PREFIX } from "./constants.js"; 4 + import { create as createCid } from "~/common/cid.js"; 4 5 import { musicMetadataTags } from "../metadata/common.js"; 5 6 import { ostiary, rpc } from "~/common/worker.js"; 7 + 8 + // multicodec raw bytes 9 + const RAW = 0x55; 6 10 7 11 /** 8 12 * @import {IPicture} from "music-metadata" ··· 205 209 */ 206 210 async function processRequest(req) { 207 211 // Check if already processed 208 - // TODO: Retry if none was found? 209 - const cache = await IDB.get(`${IDB_ARTWORK_PREFIX}/${req.cacheId}`); 210 - if (cache && Array.isArray(cache) && cache.length) return cache; 212 + 213 + /** @type {string[] | undefined} */ 214 + const cachedCids = await IDB.get( 215 + `${IDB_ARTWORK_PREFIX}/track/${req.cacheId}`, 216 + ); 217 + 218 + if (cachedCids?.length) { 219 + /** @type {Artwork[]} */ 220 + const art = await Promise.all( 221 + cachedCids.map((cid) => IDB.get(`${IDB_ARTWORK_PREFIX}/image/${cid}`)), 222 + ); 223 + 224 + const found = art.filter(Boolean); 225 + if (found.length) return found; 226 + } 211 227 212 228 // Request override 213 229 if (req.tags?.artist?.toUpperCase() === "VA") { ··· 254 270 art.push(...fromLastFm); 255 271 } 256 272 257 - // Save artwork to IDB 258 - await IDB.set(`${IDB_ARTWORK_PREFIX}/${req.cacheId}`, art); 273 + // Save artwork to IDB — store each image by its content CID, 274 + // then map the track to those CIDs 275 + const cids = await Promise.all( 276 + art.map(async (a) => { 277 + const cid = await createCid(RAW, a.bytes); 278 + const key = `${IDB_ARTWORK_PREFIX}/image/${cid}`; 279 + if (await IDB.get(key)) return cid; 280 + await IDB.set(key, a); 281 + return cid; 282 + }), 283 + ); 284 + 285 + await IDB.set(`${IDB_ARTWORK_PREFIX}/track/${req.cacheId}`, cids); 259 286 260 287 // Fin 261 288 return art;