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");
/** @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<string>((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";
}