IO-3150 Thumbnail Generation Correction

This commit is contained in:
Allan Carr
2025-03-06 08:49:11 -08:00
parent c3f408f206
commit 231130267f

View File

@@ -12,39 +12,35 @@ import simpleThumb from "simple-thumbnail";
//const ffmpeg = require("ffmpeg-static"); //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. */ /** @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( export default async function GenerateThumbnail(file: string) {
file: string
//thumbPath: string
) {
// const type: core.FileTypeResult | undefined = await ft.fileTypeFromFile(file); // const type: core.FileTypeResult | undefined = await ft.fileTypeFromFile(file);
const type: FileTypeResult | undefined = await fileTypeFromFile(file); const type: FileTypeResult | undefined = await fileTypeFromFile(file);
let thumbnailExtension: string = GetThumbnailExtension(type);
let thumbPath: string = path.join( let thumbPath: string = path.join(
path.dirname(file), path.dirname(file),
FolderPaths.ThumbsSubDir, FolderPaths.ThumbsSubDir,
path.parse(path.basename(file)).name + thumbnailExtension `${path.parse(path.basename(file)).name}.jpg`
); );
try { try {
//Ensure the thumbs directory exists. //Ensure the thumbs directory exists.
await fs.ensureDir(path.dirname(thumbPath)); await fs.ensureDir(path.dirname(thumbPath));
try { //Check for existing thumbnail
await access(thumbPath); if (await fs.pathExists(thumbPath)) {
logger.debug("Thumbnail already exists for : " + thumbPath); logger.debug("Using existing thumbnail:", thumbPath);
return path.relative(path.dirname(file), thumbPath); return path.relative(path.dirname(file), thumbPath);
} catch {} }
//Check to see if the file is an image, PDF, or video. //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") { if (type?.mime === "application/pdf" || type?.mime === "image/heic" || type?.mime === "image/heif") {
const fileOnDisk: Buffer = await fs.readFile(file);
await GeneratePdfThumbnail(file, thumbPath); await GeneratePdfThumbnail(file, thumbPath);
} else if (type?.mime.startsWith("video")) { } else if (type?.mime.startsWith("video")) {
await simpleThumb(file, thumbPath, "250x?", { await simpleThumb(file, thumbPath, "250x?", {
// path: ffmpeg, // path: ffmpeg,
}); });
} else if (type?.mime === "image/heic" || type?.mime === "image/heif") {
await GeneratePdfThumbnail(file, thumbPath);
} else { } else {
logger.debug("Thumbnail being created for : " + thumbPath); logger.debug("Thumbnail being created for : " + thumbPath);
//Ignoring typescript as the interface is lacking a parameter. //Ignoring typescript as the interface is lacking a parameter.
@@ -55,7 +51,6 @@ export default async function GenerateThumbnail(
width: 250, width: 250,
failOnError: false failOnError: false
}); });
console.log("Image success.");
await fs.writeFile(thumbPath, thumbnail); await fs.writeFile(thumbPath, thumbnail);
} }
return path.relative(path.dirname(file), thumbPath); return path.relative(path.dirname(file), thumbPath);
@@ -74,8 +69,8 @@ async function GeneratePdfThumbnail(file: string, thumbPath: string) {
return new Promise<string>((resolve, reject) => { return new Promise<string>((resolve, reject) => {
gm(fileOnDisk) gm(fileOnDisk)
.selectFrame(0) .selectFrame(0)
.setFormat("png") .setFormat("jpg")
.resize(200, 200, "!") .resize(250, 250, "!")
.quality(75) .quality(75)
.write(thumbPath, (error) => { .write(thumbPath, (error) => {
if (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";
}