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

@@ -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);