« get me outta code hell

search, client: refactor & simplify search result image processing - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-05-13 09:02:54 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-05-31 12:11:52 -0300
commit1b399c2451b222597bf734dbf8a211e8b421fe07 (patch)
tree8e5d3fcbf5c3a517c53bd82f5ef7af6f339409d1
parent5038549631b0413552fe6589e7c77f66e53e7bcd (diff)
search, client: refactor & simplify search result image processing
-rw-r--r--src/static/js/client.js32
-rw-r--r--src/util/search-spec.js75
2 files changed, 65 insertions, 42 deletions
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: [