Revert "IO-2086 Create optimized images."
This reverts commit 32beabccb5.
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
import ft from "file-type";
|
||||
import core from "file-type/core";
|
||||
import fs from "fs-extra";
|
||||
import { access } from "fs/promises";
|
||||
import gm from "gm";
|
||||
import imageThumbnail from "image-thumbnail";
|
||||
import path from "path";
|
||||
import { logger } from "../server";
|
||||
import gm from "gm";
|
||||
import ft from "file-type";
|
||||
import core from "file-type/core";
|
||||
import GenerateUrl from "./MediaUrlGen";
|
||||
import { AssetPaths, FolderPaths } from "./serverInit";
|
||||
import { logger } from "../server";
|
||||
const simpleThumb = require("simple-thumbnail");
|
||||
//const ffmpeg = require("ffmpeg-static");
|
||||
|
||||
@@ -66,67 +67,6 @@ export default async function GenerateThumbnail(
|
||||
}
|
||||
}
|
||||
|
||||
/** @returns {string} Returns the relative path from the file to the thumbnail on the server. This must be converted to a URL. */
|
||||
export async function GenerateOptimized(
|
||||
file: string
|
||||
//thumbPath: string
|
||||
) {
|
||||
const type: core.FileTypeResult | undefined = await ft.fromFile(file);
|
||||
let thumbnailExtension: string = GetThumbnailExtension(type, true);
|
||||
let thumbPath: string = path.join(
|
||||
path.dirname(file),
|
||||
FolderPaths.OptimizedSubDir,
|
||||
path.parse(path.basename(file)).name + thumbnailExtension
|
||||
);
|
||||
|
||||
try {
|
||||
//Ensure the thumbs directory exists.
|
||||
await fs.ensureDir(path.dirname(thumbPath));
|
||||
|
||||
try {
|
||||
await access(thumbPath);
|
||||
logger.debug("Optimized image already exists for : " + thumbPath);
|
||||
|
||||
return path.relative(path.dirname(file), thumbPath);
|
||||
} catch {}
|
||||
|
||||
//Check to see if the file is an image, PDF, or video.
|
||||
|
||||
if (type?.mime === "application/pdf") {
|
||||
const fileOnDisk: Buffer = await fs.readFile(file);
|
||||
await GeneratePdfThumbnail(file, thumbPath);
|
||||
} else if (type?.mime.startsWith("video")) {
|
||||
await simpleThumb(file, thumbPath, "250x?", {
|
||||
// path: ffmpeg,
|
||||
});
|
||||
} else {
|
||||
logger.debug("Optimized image being created for : " + thumbPath);
|
||||
await OptimizeImage(file, thumbPath);
|
||||
}
|
||||
return path.relative(path.dirname(file), thumbPath);
|
||||
} catch (err) {
|
||||
logger.error("Error when genenerating optimized image:", {
|
||||
thumbPath,
|
||||
err,
|
||||
message: (err as Error).message,
|
||||
});
|
||||
return path.relative(path.dirname(file), AssetPaths.File);
|
||||
}
|
||||
}
|
||||
|
||||
async function OptimizeImage(file: string, optimizedPath: string) {
|
||||
const fileOnDisk: Buffer = await fs.readFile(file);
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
const result = gm(fileOnDisk)
|
||||
.resize(1500)
|
||||
.quality(75)
|
||||
.write(optimizedPath, (error) => {
|
||||
if (error) reject(error.message);
|
||||
resolve(optimizedPath);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function GeneratePdfThumbnail(file: string, thumbPath: string) {
|
||||
const fileOnDisk: Buffer = await fs.readFile(file);
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
@@ -142,11 +82,7 @@ async function GeneratePdfThumbnail(file: string, thumbPath: string) {
|
||||
});
|
||||
}
|
||||
|
||||
function GetThumbnailExtension(
|
||||
file: core.FileTypeResult | undefined,
|
||||
useJpeg?: boolean
|
||||
) {
|
||||
if (useJpeg) return ".jpg";
|
||||
function GetThumbnailExtension(file: core.FileTypeResult | undefined) {
|
||||
if (file === undefined) return ".png";
|
||||
return ".png";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user