Add logging, pdf thumbs

This commit is contained in:
Patrick Fic
2022-05-03 15:26:14 -07:00
parent cb046f813b
commit 8d851c52b2
10 changed files with 3141 additions and 1568 deletions

View File

@@ -2,29 +2,73 @@ import fs from "fs-extra";
import { access } from "fs/promises";
import imageThumbnail from "image-thumbnail";
import path from "path";
import gm from "gm";
import ft from "file-type";
import core from "file-type/core";
import GenerateUrl from "./MediaUrlGen";
import { FolderPaths } from "./serverInit";
var Bluebird = require("bluebird");
Bluebird.promisifyAll(gm.prototype);
/** @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
file: string
//thumbPath: string
) {
const type: core.FileTypeResult | undefined = await ft.fromFile(file);
let thumbnailExtension: string = GetThumbnailExtension(type);
let thumbPath: string = path.join(
path.dirname(file),
FolderPaths.ThumbsSubDir,
path.parse(path.basename(file)).name + thumbnailExtension
);
try {
//Ensure the thumbs directory exists.
await fs.ensureDir(path.dirname(thumbPath));
try {
await access(thumbPath);
return;
return;
return path.relative(path.dirname(file), thumbPath);
} catch {}
const thumbnail = await imageThumbnail(file, {
responseType: "buffer",
height: 250,
width: 250,
});
//Check to see if the file is an image, PDF, or video.
await fs.writeFile(thumbPath, thumbnail);
if (type?.mime === "application/pdf") {
const fileOnDisk: Buffer = await fs.readFile(file);
await GeneratePdfThumbnail(file, thumbPath);
} else {
const thumbnail = await imageThumbnail(file, {
responseType: "buffer",
height: 250,
width: 250,
});
await fs.writeFile(thumbPath, thumbnail);
}
return path.relative(path.dirname(file), thumbPath);
} catch (err) {
console.error("Error when genenerating thumbnail:", thumbPath);
return path.relative(path.dirname(file), thumbPath);
}
}
async function GeneratePdfThumbnail(file: string, thumbPath: string) {
const fileOnDisk: Buffer = await fs.readFile(file);
return new Promise<string>((resolve, reject) => {
const result = gm(fileOnDisk)
.setFormat("png")
.resize(200) // Resize to fixed 200px width, maintaining aspect ratio
.quality(75)
.write(thumbPath, (error) => {
if (error) reject(error.message);
resolve(thumbPath);
});
});
}
function GetThumbnailExtension(file: core.FileTypeResult | undefined) {
if (file === undefined) return ".png";
return ".png";
}

View File

@@ -2,6 +2,7 @@ import fs = require("fs-extra");
import path, { resolve } from "path";
import dotenv from "dotenv";
import os from "os";
import { logger } from "../server";
dotenv.config({
path: resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`),
@@ -21,10 +22,11 @@ export const FolderPaths = {
};
export default function InitServer() {
console.log(`Ensuring Root media path exists: ${FolderPaths.Root}`);
logger.info(`Ensuring Root media path exists: ${FolderPaths.Root}`);
fs.ensureDirSync(FolderPaths.Root);
console.log(`Ensuring Jobs media path exists: ${FolderPaths.Jobs}`);
logger.info(`Ensuring Jobs media path exists: ${FolderPaths.Jobs}`);
fs.ensureDirSync(FolderPaths.Jobs);
console.log(`Ensuring Vendors media path exists: ${FolderPaths.Vendors}`);
logger.info(`Ensuring Vendors media path exists: ${FolderPaths.Vendors}`);
fs.ensureDirSync(FolderPaths.Vendors);
logger.info("Folder Paths", FolderPaths);
}