From 1b399c2451b222597bf734dbf8a211e8b421fe07 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Mon, 13 May 2024 09:02:54 -0300 Subject: search, client: refactor & simplify search result image processing --- src/static/js/client.js | 32 +++------------------ src/util/search-spec.js | 75 ++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 65 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/static/js/client.js b/src/static/js/client.js index 771d2011..bee82938 100644 --- a/src/static/js/client.js +++ b/src/static/js/client.js @@ -3862,36 +3862,12 @@ function generateSidebarSearchResult(result) { function getSearchResultImageSource(result) { const {artwork} = result.data; - if (!artwork) return null; - const [kind, ...opts] = artwork; - - switch (kind) { - case 'flash': - return rebase( - ('flash-art' - + `/${result.directory}.small.jpg`), - 'rebaseThumb'); - - case 'track': - return rebase( - (`album-art` - + `/${opts[0]}` - + `/${result.directory}` - + `.small.jpg`), - 'rebaseThumb'); - - case 'track-album': - return rebase( - (`album-art` - + `/${opts[0]}` - + `/cover.small.jpg`), - 'rebaseThumb'); - - default: - return null; - } + return ( + rebase( + artwork.replace('<>', result.directory), + 'rebaseThumb')); } function generateSidebarSearchResultTemplate(slots) { diff --git a/src/util/search-spec.js b/src/util/search-spec.js index 92ed4dec..6c8eb1ee 100644 --- a/src/util/search-spec.js +++ b/src/util/search-spec.js @@ -1,30 +1,77 @@ // Index structures shared by client and server, and relevant interfaces. -function prepareArtwork(thing) { +function getArtworkPath(thing) { switch (thing.constructor[Symbol.for('Thing.referenceType')]) { case 'flash': { - return ['flash', thing.coverArtFileExtension]; + return [ + 'media.flashArt', + thing.directory, + thing.coverArtFileExtension, + ]; } case 'track': { if (thing.hasUniqueCoverArt) { - if (thing.coverArtFileExtension === 'gif') - return undefined; - return ['track', thing.album.directory]; + return [ + 'media.trackCover', + thing.album.directory, + thing.directory, + thing.coverArtFileExtension, + ]; } else if (thing.album.hasCoverArt) { - if (thing.album.coverArtFileExtension === 'gif') - return undefined; - return ['track-album', thing.album.directory]; + return [ + 'media.albumCover', + thing.album.directory, + thing.album.coverArtFileExtension, + ]; } else { - return undefined; + return null; } } default: - return undefined; + return null; } } +function prepareArtwork(thing, { + checkIfImagePathHasCachedThumbnails, + getThumbnailEqualOrSmaller, + urls, +}) { + const artworkPath = + getArtworkPath(thing); + + if (!artworkPath) { + return undefined; + } + + const mediaSrc = + urls + .from('media.root') + .to(...artworkPath); + + if (!checkIfImagePathHasCachedThumbnails(mediaSrc)) { + return undefined; + } + + const selectedSize = + getThumbnailEqualOrSmaller('small', mediaSrc); + + const mediaSrcJpeg = + mediaSrc.replace(/\.(png|jpg)$/, `.${selectedSize}.jpg`); + + const displaySrc = + urls + .from('thumb.root') + .to('thumb.path', mediaSrcJpeg); + + const serializeSrc = + displaySrc.replace(thing.directory, '<>'); + + return serializeSrc; +} + export const searchSpec = { generic: { query: ({ @@ -46,7 +93,7 @@ export const searchSpec = { .filter(track => !track.originalReleaseTrack), ].flat(), - process: (thing) => ({ + process: (thing, opts) => ({ primaryName: thing.name, @@ -80,7 +127,7 @@ export const searchSpec = { : []), artwork: - prepareArtwork(thing), + prepareArtwork(thing, opts), }), index: [ @@ -117,7 +164,7 @@ export const searchSpec = { tracks: { query: ({trackData}) => trackData, - process: (track) => ({ + process: (track, opts) => ({ name: track.name, @@ -140,7 +187,7 @@ export const searchSpec = { .map(entry => entry.name), artwork: - prepareArtwork(track), + prepareArtwork(track, opts), }), index: [ -- cgit 1.3.0-6-gf8a5