diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2025-06-17 13:35:26 -0300 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2025-06-17 13:35:26 -0300 |
commit | 9b70e690b49e44993e95cc701a69d5013baa73ea (patch) | |
tree | dc6cc0c03bf1d5f83c7e7dc3cf879b868ebd0ec7 | |
parent | 24d976d8510cbd4dbea3bbd0edd784f8f2ddf2c3 (diff) |
gen-thumbs: style cleanup
-rw-r--r-- | src/gen-thumbs.js | 62 |
1 files changed, 33 insertions, 29 deletions
diff --git a/src/gen-thumbs.js b/src/gen-thumbs.js index 4986c6f7..65f0793e 100644 --- a/src/gen-thumbs.js +++ b/src/gen-thumbs.js @@ -464,27 +464,32 @@ async function getImageMagickVersion(binary) { // Write all requested thumbtacks for a source image in one pass // This saves a lot of disk reads which are probably the main bottleneck -function buildMultiThumbArgs({src, dstDir, baseName, thumbtacks}) { - const args = [src, '-strip']; +function prepareConvertArgs(filePathInMedia, dirnameInCache, thumbtacks) { + const args = [filePathInMedia, '-strip']; + + const basename = + path.basename(filePathInMedia, path.extname(filePathInMedia)); // do larger sizes first - thumbtacks - .sort((a, b) => thumbnailSpec[b].size - thumbnailSpec[a].size) - .forEach(tack => { - const {size, quality} = thumbnailSpec[tack]; - const outFile = path.join(dstDir, `${baseName}.${tack}.jpg`); - args.push( - '(', '+clone', - '-resize', `${size}x${size}>`, - '-interlace', 'Plane', - '-quality', `${quality}%`, - '-write', outFile, - '+delete', ')', - ); - }); + thumbtacks.sort((a, b) => thumbnailSpec[b].size - thumbnailSpec[a].size); + + for (const tack of thumbtacks) { + const {size, quality} = thumbnailSpec[tack]; + const filename = `${basename}.${tack}.jpg`; + const filePathInCache = path.join(dirnameInCache, filename); + args.push( + '(', '+clone', + '-resize', `${size}x${size}>`, + '-interlace', 'Plane', + '-quality', `${quality}%`, + '-write', filePathInCache, + '+delete', ')', + ); + } // throw away the (already written) image stream args.push('null:'); + return args; } @@ -586,19 +591,18 @@ async function generateImageThumbnails(imagePath, thumbtacks, { mediaCachePath, spawnConvert, }) { - if (!thumbtacks.length) return; - - const filePathInMedia = path.join(mediaPath, imagePath); - const dstDir = path.join(mediaCachePath, path.dirname(imagePath)); - await mkdir(dstDir, {recursive: true}); - - const baseName = path.basename(imagePath, path.extname(imagePath)); - const convertArgs = buildMultiThumbArgs({ - src: filePathInMedia, - dstDir, - baseName, - thumbtacks, - }); + if (empty(thumbtacks)) return; + + const filePathInMedia = + path.join(mediaPath, imagePath); + + const dirnameInCache = + path.join(mediaCachePath, path.dirname(imagePath)); + + await mkdir(dirnameInCache, {recursive: true}); + + const convertArgs = + prepareConvertArgs(filePathInMedia, dirnameInCache, thumbtacks); await promisifyProcess(spawnConvert(convertArgs), false); } |