Covert to ESM and get it functional locally

This commit is contained in:
Allan Carr
2024-08-27 13:45:12 -07:00
parent e793402a79
commit 36ada6fd1f
19 changed files with 980 additions and 1046 deletions

View File

@@ -5,15 +5,12 @@ import { access } from "fs/promises";
import gm from "gm";
import imageThumbnail from "image-thumbnail";
import path from "path";
import { logger } from "../server";
import { AssetPaths, FolderPaths } from "./serverInit";
const simpleThumb = require("simple-thumbnail");
import { logger } from "../server.js";
import { AssetPaths, FolderPaths } from "./serverInit.js";
//@ts-ignore
import simpleThumb from "simple-thumbnail";
//const ffmpeg = require("ffmpeg-static");
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
@@ -73,14 +70,17 @@ export default async function GenerateThumbnail(
async function GeneratePdfThumbnail(file: string, thumbPath: string) {
const fileOnDisk: Buffer = await fs.readFile(file);
return new Promise<string>((resolve, reject) => {
const result = gm(fileOnDisk)
gm(fileOnDisk)
.selectFrame(0)
.setFormat("png")
.resize(200, 200, "!") // Resize to fixed 200px width, maintaining aspect ratio
.resize(200, 200, "!")
.quality(75)
.write(thumbPath, (error) => {
if (error) reject(error.message);
resolve(thumbPath);
if (error) {
reject(error);
} else {
resolve(thumbPath);
}
});
});
}

View File

@@ -1,58 +1,60 @@
import fs from "fs-extra";
import dotenv from "dotenv";
import { fileTypeFromFile } from "file-type";
import { FileTypeResult } from "file-type/core";
import fs from "fs-extra";
import gm from "gm";
import path, { resolve } from "path";
import { logger } from "../server";
import { FolderPaths } from "./serverInit";
import path from "path";
import { logger } from "../server.js";
import { FolderPaths } from "./serverInit.js";
//const heicConverter = require("heic-convert");
var imageMagick = gm.subClass({ imageMagick: true });
//gm.subClass();
dotenv.config({
path: resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`)
path: path.resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`)
});
const imageMagick = gm.subClass({ imageMagick: true });
export async function ConvertHeicFiles(files: Express.Multer.File[]) {
const validFiles = await filterValidHeicFiles(files);
await Promise.all(validFiles.map(async (file) => {
const convertedFileName = `${path.parse(path.basename(file.originalname)).name}-${Math.floor(Date.now() / 1000)}.jpeg`;
try {
await ConvertToJpeg(file.path, `${file.destination}/${convertedFileName}`);
logger.log("debug", `Converted ${file.filename} image to JPEG from HEIC.`);
await handleOriginalFile(file, convertedFileName);
file.filename = convertedFileName;
file.mimetype = "image/jpeg";
file.path = `${file.destination}/${convertedFileName}`;
} catch (error) {
logger.log("error", `Error converting ${file.filename} image to JPEG from HEIC. ${JSON.stringify(error)}`);
}
}));
}
async function filterValidHeicFiles(files: Express.Multer.File[]) {
const validFiles = [];
for (const file of files) {
const type: FileTypeResult | undefined = await fileTypeFromFile(file.path);
if (type?.mime === "image/heic") {
logger.log("debug", `Converting ${file.filename} image to JPEG from HEIC.`);
const convertedFileName = `${
path.parse(path.basename(file.originalname)).name
}-${Math.floor(Date.now() / 1000)}.jpeg`;
try {
await ConvertToJpeg(file.path, `${file.destination}/${convertedFileName}`);
//Move the HEIC.
if (process.env.KEEP_CONVERTED_ORIGINALS) {
await fs.ensureDir(path.join(file.destination, FolderPaths.ConvertedOriginalSubDir));
await fs.move(
file.path,
`${path.join(file.destination, FolderPaths.ConvertedOriginalSubDir)}/${file.filename}`
);
} else {
await fs.unlink(file.destination);
}
//Update the multer file entry.
file.filename = convertedFileName;
file.mimetype = "image/jpeg";
file.path = `${file.destination}/${convertedFileName}`;
} catch (error) {
logger.log("error", `Error converting ${file.filename} image to JPEG from HEIC. ${JSON.stringify(error)}`);
}
validFiles.push(file);
}
}
return validFiles;
}
async function handleOriginalFile(file: Express.Multer.File, convertedFileName: string) {
if (process.env.KEEP_CONVERTED_ORIGINALS) {
await fs.ensureDir(path.join(file.destination, FolderPaths.ConvertedOriginalSubDir));
await fs.move(file.path, `${path.join(file.destination, FolderPaths.ConvertedOriginalSubDir)}/${file.filename}`);
} else {
await fs.unlink(file.path);
}
}
async function ConvertToJpeg(file: string, newPath: string) {
const fileOnDisk: Buffer = await fs.readFile(file);
return new Promise<string>((resolve, reject) => {
const result = imageMagick(fileOnDisk)
imageMagick(fileOnDisk)
.setFormat("jpg")
.write(newPath, (error) => {
if (error) reject(error.message);

View File

@@ -1,5 +1,5 @@
import path from "path";
import { FolderPaths } from "./serverInit";
import { FolderPaths } from "./serverInit.js";
export function PathToRoFolder(jobid: string) {
return path.join(FolderPaths.Jobs, jobid);

View File

@@ -2,7 +2,7 @@ import dotenv from "dotenv";
import { ensureDirSync } from "fs-extra";
import os from "os";
import path, { resolve } from "path";
import { logger } from "../server";
import { logger } from "../server.js";
dotenv.config({
path: resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`)