From 231130267fc4243dce93e63194063b600b321b81 Mon Sep 17 00:00:00 2001 From: Allan Carr Date: Thu, 6 Mar 2025 08:49:11 -0800 Subject: [PATCH] IO-3150 Thumbnail Generation Correction --- util/generateThumbnail.ts | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/util/generateThumbnail.ts b/util/generateThumbnail.ts index 5964543..6486769 100644 --- a/util/generateThumbnail.ts +++ b/util/generateThumbnail.ts @@ -12,39 +12,35 @@ import simpleThumb from "simple-thumbnail"; //const ffmpeg = require("ffmpeg-static"); /** @returns {string} Returns the relative path from the file to the thumbnail on the server. This must be converted to a URL. */ -export default async function GenerateThumbnail( - file: string - //thumbPath: string -) { +export default async function GenerateThumbnail(file: string) { // const type: core.FileTypeResult | undefined = await ft.fileTypeFromFile(file); const type: FileTypeResult | undefined = await fileTypeFromFile(file); - let thumbnailExtension: string = GetThumbnailExtension(type); let thumbPath: string = path.join( path.dirname(file), FolderPaths.ThumbsSubDir, - path.parse(path.basename(file)).name + thumbnailExtension + `${path.parse(path.basename(file)).name}.jpg` ); try { //Ensure the thumbs directory exists. await fs.ensureDir(path.dirname(thumbPath)); - try { - await access(thumbPath); - logger.debug("Thumbnail already exists for : " + thumbPath); + //Check for existing thumbnail + if (await fs.pathExists(thumbPath)) { + logger.debug("Using existing thumbnail:", thumbPath); return path.relative(path.dirname(file), thumbPath); - } catch {} + } //Check to see if the file is an image, PDF, or video. + if (!type?.mime) { + throw new Error("Unknown file type"); + } - if (type?.mime === "application/pdf") { - const fileOnDisk: Buffer = await fs.readFile(file); + if (type?.mime === "application/pdf" || type?.mime === "image/heic" || type?.mime === "image/heif") { await GeneratePdfThumbnail(file, thumbPath); } else if (type?.mime.startsWith("video")) { await simpleThumb(file, thumbPath, "250x?", { // path: ffmpeg, }); - } else if (type?.mime === "image/heic" || type?.mime === "image/heif") { - await GeneratePdfThumbnail(file, thumbPath); } else { logger.debug("Thumbnail being created for : " + thumbPath); //Ignoring typescript as the interface is lacking a parameter. @@ -55,7 +51,6 @@ export default async function GenerateThumbnail( width: 250, failOnError: false }); - console.log("Image success."); await fs.writeFile(thumbPath, thumbnail); } return path.relative(path.dirname(file), thumbPath); @@ -74,8 +69,8 @@ async function GeneratePdfThumbnail(file: string, thumbPath: string) { return new Promise((resolve, reject) => { gm(fileOnDisk) .selectFrame(0) - .setFormat("png") - .resize(200, 200, "!") + .setFormat("jpg") + .resize(250, 250, "!") .quality(75) .write(thumbPath, (error) => { if (error) { @@ -86,8 +81,3 @@ async function GeneratePdfThumbnail(file: string, thumbPath: string) { }); }); } - -function GetThumbnailExtension(file: FileTypeResult | undefined) { - if (file === undefined) return ".png"; - return ".png"; -}