From d68ead9ff27a166ccf90492cd900ef4d8d6b8e3e Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 27 May 2023 12:15:41 -0300 Subject: thumbs: use image-size module instead of magick identify --- package-lock.json | 39 +++++++++++++++++++++++++++++++++++++ package.json | 1 + src/gen-thumbs.js | 57 +++++++++++-------------------------------------------- 3 files changed, 51 insertions(+), 46 deletions(-) diff --git a/package-lock.json b/package-lock.json index d9ab5cd1..edd3ad32 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "command-exists": "^1.2.9", "eslint": "^8.37.0", "he": "^1.2.0", + "image-size": "^1.0.2", "js-yaml": "^4.1.0", "marked": "^5.0.2", "striptags": "^4.0.0-alpha.4", @@ -1632,6 +1633,20 @@ "node": ">= 4" } }, + "node_modules/image-size": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/image-size/-/image-size-1.0.2.tgz", + "integrity": "sha512-xfOoWjceHntRb3qFCrh5ZFORYH8XCdYpASltMhZ/Q0KZiOwjdE/Yl2QCiWdwD+lygV5bMCvauzgu5PxBX/Yerg==", + "dependencies": { + "queue": "6.0.2" + }, + "bin": { + "image-size": "bin/image-size.js" + }, + "engines": { + "node": ">=14.0.0" + } + }, "node_modules/import-fresh": { "version": "3.3.0", "resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.0.tgz", @@ -2374,6 +2389,14 @@ "node": ">=6" } }, + "node_modules/queue": { + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/queue/-/queue-6.0.2.tgz", + "integrity": "sha512-iHZWu+q3IdFZFX36ro/lKBkSvfkztY5Y7HMiPlOUjhupPcG2JMfst2KKEpu5XndviX/3UhFbRngUPNKtgvtZiA==", + "dependencies": { + "inherits": "~2.0.3" + } + }, "node_modules/queue-microtask": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz", @@ -6136,6 +6159,14 @@ "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.2.4.tgz", "integrity": "sha512-MAb38BcSbH0eHNBxn7ql2NH/kX33OkB3lZ1BNdh7ENeRChHTYsTvWrMubiIAMNS2llXEEgZ1MUOBtXChP3kaFQ==" }, + "image-size": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/image-size/-/image-size-1.0.2.tgz", + "integrity": "sha512-xfOoWjceHntRb3qFCrh5ZFORYH8XCdYpASltMhZ/Q0KZiOwjdE/Yl2QCiWdwD+lygV5bMCvauzgu5PxBX/Yerg==", + "requires": { + "queue": "6.0.2" + } + }, "import-fresh": { "version": "3.3.0", "resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.0.tgz", @@ -6694,6 +6725,14 @@ "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==" }, + "queue": { + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/queue/-/queue-6.0.2.tgz", + "integrity": "sha512-iHZWu+q3IdFZFX36ro/lKBkSvfkztY5Y7HMiPlOUjhupPcG2JMfst2KKEpu5XndviX/3UhFbRngUPNKtgvtZiA==", + "requires": { + "inherits": "~2.0.3" + } + }, "queue-microtask": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz", diff --git a/package.json b/package.json index 02789a76..3b8e1771 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "command-exists": "^1.2.9", "eslint": "^8.37.0", "he": "^1.2.0", + "image-size": "^1.0.2", "js-yaml": "^4.1.0", "marked": "^5.0.2", "striptags": "^4.0.0-alpha.4", diff --git a/src/gen-thumbs.js b/src/gen-thumbs.js index 2f5304ea..4f5c0fec 100644 --- a/src/gen-thumbs.js +++ b/src/gen-thumbs.js @@ -91,6 +91,8 @@ import {createReadStream} from 'node:fs'; import {readFile, stat, unlink, writeFile} from 'node:fs/promises'; import * as path from 'node:path'; +import dimensionsOf from 'image-size'; + import { color, fileIssue, @@ -122,46 +124,11 @@ function readFileMD5(filePath) { }); } -async function identifyImageDimensions(filePath, {spawnIdentify}) { - const maxTries = 5; - - const recursive = async n => { - if (n > maxTries) { - throw new Error(`Didn't get any output after ${maxTries} tries`); - } - - if (n > 1) { - logInfo`Attempt #${n} for ${filePath}`; - } - - const stdoutText = await new Promise((resolve, reject) => { - let stdout = ''; - let stderr = ''; - - const proc = spawnIdentify(['-format', '%w %h', filePath]); - proc.stdout.on('data', data => stdout += data); - proc.stderr.on('data', data => stderr += data); - - proc.on('exit', code => { - if (code === 0) { - resolve(stdout); - } else { - reject(stderr); - } - }); - }); - - if (stdoutText === '') { - return recursive(n + 1); - } - - const words = stdoutText.split(' '); - const width = parseInt(words[0]); - const height = parseInt(words[1]); - return [width, height]; - }; - - return recursive(1); +async function identifyImageDimensions(filePath) { + // See: https://github.com/image-size/image-size/issues/96 + const buffer = await readFile(filePath); + const dimensions = dimensionsOf(buffer); + return [dimensions.width, dimensions.height]; } async function getImageMagickVersion(binary) { @@ -343,12 +310,11 @@ export default async function genThumbs(mediaPath, { const quietInfo = quiet ? () => null : logInfo; const [convertInfo, spawnConvert] = await getSpawnMagick('convert'); - const [identifyInfo, spawnIdentify] = await getSpawnMagick('identify'); - if (!spawnConvert || !spawnIdentify) { + if (!spawnConvert) { logError`${`It looks like you don't have ImageMagick installed.`}`; logError`ImageMagick is required to generate thumbnails for display on the wiki.`; - for (const error of [convertInfo, identifyInfo].filter(Boolean)) { + for (const error of [convertInfo].filter(Boolean)) { logError`(Error message: ${error})`; } logInfo`You can find info to help install ImageMagick on Linux, Windows, or macOS`; @@ -357,8 +323,7 @@ export default async function genThumbs(mediaPath, { logInfo`to drop a message in the HSMusic Discord server! ${'https://hsmusic.wiki/discord/'}`; return {success: false}; } else { - logInfo`Found ImageMagick convert binary: ${convertInfo}`; - logInfo`Found ImageMagick identify binary: ${identifyInfo}`; + logInfo`Found ImageMagick binary: ${convertInfo}`; } quietInfo`Running up to ${magickThreads + ' magick threads'} simultaneously.`; @@ -432,7 +397,7 @@ export default async function genThumbs(mediaPath, { `Identifying dimensions of image files`, queue( imagePaths.map(imagePath => () => - identifyImageDimensions(path.join(mediaPath, imagePath), {spawnIdentify}) + identifyImageDimensions(path.join(mediaPath, imagePath)) .then( dimensions => [imagePath, dimensions], error => [imagePath, {error}])), -- cgit 1.3.0-6-gf8a5