« get me outta code hell

thumbs, content: integrate cached thumb sizes into content - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/gen-thumbs.js
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-05-30 09:51:26 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-09-01 14:06:07 -0300
commit029210cc329a015a939472a688209d3f3423242b (patch)
treed0b3d682b699b0ba9e21642f4884c937d7f9f97c /src/gen-thumbs.js
parent641d821d03ad96bca28b8b09d2408457443a9f7f (diff)
thumbs, content: integrate cached thumb sizes into content
Diffstat (limited to 'src/gen-thumbs.js')
-rw-r--r--src/gen-thumbs.js31
1 files changed, 29 insertions, 2 deletions
diff --git a/src/gen-thumbs.js b/src/gen-thumbs.js
index bf6de286..f6a8eaaf 100644
--- a/src/gen-thumbs.js
+++ b/src/gen-thumbs.js
@@ -74,7 +74,7 @@
 
 'use strict';
 
-const CACHE_FILE = 'thumbnail-cache.json';
+export const CACHE_FILE = 'thumbnail-cache.json';
 const WARNING_DELAY_TIME = 10000;
 
 const thumbnailSpec = {
@@ -168,6 +168,30 @@ getThumbnailsAvailableForDimensions.all =
     .map(([name, {size}]) => [name, size])
     .sort((a, b) => b[1] - a[1]);
 
+export function getDimensionsOfImagePath(imagePath, cache) {
+  // This function is really generic. It takes the gen-thumbs image cache and
+  // returns the dimensions in that cache, so that other functions don't need
+  // to hard-code the cache format.
+
+  const [width, height] = cache[imagePath].slice(1);
+  return [width, height];
+}
+
+export function getThumbnailEqualOrSmaller(preferred, imagePath, cache) {
+  // This function is totally exclusive to page generation. It's a shorthand
+  // for accessing dimensions from the thumbnail cache, calculating all the
+  // thumbnails available, and selecting the one which is equal to or smaller
+  // than the provided size. Since the path provided might not be the actual
+  // one which is being thumbnail-ified, this just returns the name of the
+  // selected thumbnail size.
+
+  const {size: preferredSize} = thumbnailSpec[preferred];
+  const [width, height] = getDimensionsOfImagePath(imagePath, cache);
+  const available = getThumbnailsAvailableForDimensions([width, height]);
+  const [selected] = available.find(([name, size]) => size <= preferredSize);
+  return selected;
+}
+
 function readFileMD5(filePath) {
   return new Promise((resolve, reject) => {
     const md5 = createHash('md5');
@@ -302,7 +326,7 @@ export async function clearThumbs(mediaPath, {
         console.error(file);
       }
       fileIssue();
-      return;
+      return {success: false};
     }
 
     logInfo`Clearing out ${thumbFiles.length} thumbs.`;
@@ -327,6 +351,7 @@ export async function clearThumbs(mediaPath, {
         console.error(file);
       }
       logError`Check for permission errors?`;
+      return {success: false};
     } else {
       logInfo`Successfully deleted all ${thumbFiles.length} thumbnail files!`;
     }
@@ -355,6 +380,8 @@ export async function clearThumbs(mediaPath, {
       logWarn`Failed to remove cache file. Check its permissions?`;
     }
   }
+
+  return {success: true};
 }
 
 export default async function genThumbs(mediaPath, {