« get me outta code hell

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:
Diffstat (limited to 'src/gen-thumbs.js')
-rw-r--r--src/gen-thumbs.js88
1 files changed, 41 insertions, 47 deletions
diff --git a/src/gen-thumbs.js b/src/gen-thumbs.js
index c5c5ee4f..d08726c7 100644
--- a/src/gen-thumbs.js
+++ b/src/gen-thumbs.js
@@ -91,7 +91,8 @@ const WARNING_DELAY_TIME = 10000;
 //   this particular thumbtack will be regenerated, but any others (whose
 //   `tackbust` listed below is equal or below the cache-recorded bust) will be
 //   reused. (Zero is a special value that means this tack's spec is still the
-//   same as it would've been generated prior to thumbtack versioning.)
+//   same as it would've been generated prior to thumbtack versioning; any new
+//   kinds of thumbnails should start counting up from one.)
 //
 // * `size` is the maximum length of the image. It will be scaled down,
 //   keeping aspect ratio, to fit in this dimension.
@@ -132,6 +133,12 @@ const thumbnailSpec = {
     quality: 85,
   },
 
+  'adorb': {
+    tackbust: 1,
+    size: 64,
+    quality: 90,
+  },
+
   'mini': {
     tackbust: 2,
     size: 8,
@@ -630,6 +637,13 @@ export async function determineMediaCachePath({
     };
   }
 
+  if (!wikiCachePath) {
+    return {
+      annotation: 'wiki cache path not provided',
+      mediaCachePath: null,
+    };
+  }
+
   let mediaIncludesThumbnailCache;
 
   try {
@@ -648,24 +662,33 @@ export async function determineMediaCachePath({
 
   // Two inferred paths are possible - "adjacent" and "contained".
   // "Contained" is the preferred format and we'll create it if
-  // wikiCachePath is provided, but if it *isn't* we won't know
-  // where to create it. Since "adjacent" isn't preferred we don't
-  // ever generate it, and we'd prefer not to *newly* generate
-  // thumbs in-place with mediaPath, so give up - we've already
-  // determined mediaPath doesn't include in-place thumbs.
-
-  const adjacentInferredPath =
-    path.join(
-      path.dirname(mediaPath),
-      path.basename(mediaPath) + '-cache');
+  // neither of the inferred paths exists. (Of course, by this
+  // point we've already determined that the media path itself
+  // isn't doubling as the thumbnail cache.)
 
   const containedInferredPath =
     (wikiCachePath
       ? path.join(wikiCachePath, 'media-cache')
       : null);
 
-  let adjacentIncludesThumbnailCache;
+  const adjacentInferredPath =
+    path.join(
+      path.dirname(mediaPath),
+      path.basename(mediaPath) + '-cache');
+
   let containedIncludesThumbnailCache;
+  let adjacentIncludesThumbnailCache;
+
+  try {
+    const files = await readdir(containedInferredPath);
+    containedIncludesThumbnailCache = files.includes(CACHE_FILE);
+  } catch (error) {
+    if (error.code === 'ENOENT') {
+      containedIncludesThumbnailCache = null;
+    } else {
+      containedIncludesThumbnailCache = undefined;
+    }
+  }
 
   try {
     const files = await readdir(adjacentInferredPath);
@@ -678,19 +701,6 @@ export async function determineMediaCachePath({
     }
   }
 
-  if (wikiCachePath) {
-    try {
-      const files = await readdir(containedInferredPath);
-      containedIncludesThumbnailCache = files.includes(CACHE_FILE);
-    } catch (error) {
-      if (error.code === 'ENOENT') {
-        containedIncludesThumbnailCache = null;
-      } else {
-        containedIncludesThumbnailCache = undefined;
-      }
-    }
-  }
-
   // Go ahead with the contained path if it exists and contains a cache -
   // no other conditions matter.
   if (containedIncludesThumbnailCache === true) {
@@ -712,7 +722,7 @@ export async function determineMediaCachePath({
   // Throw a very high-priority tantrum if the contained cache exists but
   // isn't readable. It's the preferred cache and we can't tell if it's
   // available for use or not!
-  if (wikiCachePath && containedIncludesThumbnailCache === undefined) {
+  if (containedIncludesThumbnailCache === undefined) {
     return {
       annotation: `contained path not readable`,
       mediaCachePath: null,
@@ -764,28 +774,12 @@ export async function determineMediaCachePath({
     }
   }
 
-  // If wikiCachePath was provided and the contained cache just doesn't
-  // exist yet, we'll create it during this run.
-  if (wikiCachePath && containedIncludesThumbnailCache === null) {
-    return {
-      annotation: `contained path will be created`,
-      mediaCachePath: containedInferredPath,
-    };
-  }
-
-  // If the adjacent cache doesn't exist, too dang bad!
-  // We aren't interested in newly creating it, so
-  // don't count it as an option.
-
-  // Similarly, we've already established mediaPath isn't
-  // currently doubling as the thumbnail cache, and we won't
-  // newly start generating thumbnails here either.
-
-  // All options aside struck out, there's no way to continue.
-
+  // If we haven't found any information about either inferred
+  // location (and so have fallen back to this base case), we'll
+  // create the contained cache during this run.
   return {
-    annotation: `missing wiki cache to create media cache inside`,
-    mediaCachePath: null,
+    annotation: `contained path will be created`,
+    mediaCachePath: containedInferredPath,
   };
 }