Add in HEIC converter on uploaded images.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
MEDIA_PATH=~/Desktop/IMS
|
||||
PORT=8000
|
||||
IMS_TOKEN=
|
||||
CONVERT_QUALITY=0.5
|
||||
KEEP_CONVERTED_ORIGINALS=TRUE
|
||||
@@ -8,6 +8,7 @@ import GenerateThumbnail from "../util/generateThumbnail";
|
||||
import generateUniqueFilename, {
|
||||
generateUniqueBillFilename,
|
||||
} from "../util/generateUniqueFilename";
|
||||
import { ConvertHeicFiles } from "../util/heicConverter";
|
||||
import {
|
||||
PathToRoBillsFolder,
|
||||
PathToVendorBillsFile,
|
||||
@@ -55,6 +56,8 @@ export async function BillsUploadMedia(req: Request, res: Response) {
|
||||
message: "No file uploaded",
|
||||
});
|
||||
} else {
|
||||
await ConvertHeicFiles(req.files as Express.Multer.File[]);
|
||||
|
||||
const thumbnailGenerationQueue: Promise<string>[] = [];
|
||||
|
||||
//for each file.path, generate the thumbnail.
|
||||
|
||||
@@ -9,7 +9,6 @@ import { PathToRoFolder } from "../util/pathGenerators";
|
||||
import { FolderPaths } from "../util/serverInit";
|
||||
|
||||
export async function JobsListMedia(req: Request, res: Response) {
|
||||
console.time("JobsListMedia");
|
||||
const jobid: string = (req.body.jobid || "").trim();
|
||||
await fs.ensureDir(PathToRoFolder(jobid));
|
||||
logger.debug("Listing media for job: " + PathToRoFolder(jobid));
|
||||
@@ -73,7 +72,7 @@ export async function JobsListMedia(req: Request, res: Response) {
|
||||
})
|
||||
);
|
||||
}
|
||||
console.timeEnd("JobsListMedia");
|
||||
|
||||
res.json(ret);
|
||||
} catch (error) {
|
||||
logger.error("Error listing job media.", { jobid, error });
|
||||
|
||||
@@ -7,6 +7,7 @@ import generateUniqueFilename from "../util/generateUniqueFilename";
|
||||
import { PathToRoFolder } from "../util/pathGenerators";
|
||||
import { JobsListMedia } from "./jobsListMedia";
|
||||
import fs from "fs-extra";
|
||||
import { ConvertHeicFiles } from "../util/heicConverter";
|
||||
|
||||
export const JobMediaUploadMulter = multer({
|
||||
storage: multer.diskStorage({
|
||||
@@ -31,7 +32,6 @@ export const JobMediaUploadMulter = multer({
|
||||
});
|
||||
|
||||
export async function jobsUploadMedia(req: Request, res: Response) {
|
||||
console.time("jobsUploadMedia");
|
||||
const jobid: string = (req.body.jobid || "").trim();
|
||||
|
||||
try {
|
||||
@@ -42,6 +42,14 @@ export async function jobsUploadMedia(req: Request, res: Response) {
|
||||
message: "No file uploaded",
|
||||
});
|
||||
} else {
|
||||
//If we want to skip waiting for everything, just send it back that we're good.
|
||||
if (req.body.skip_thumbnail) {
|
||||
res.sendStatus(200);
|
||||
}
|
||||
|
||||
//Check if there's a heic in the file set. If so, modify the file set.
|
||||
await ConvertHeicFiles(req.files as Express.Multer.File[]);
|
||||
|
||||
logger.debug(
|
||||
"Creating thumbnails for newly uploaded media",
|
||||
(req.files as Express.Multer.File[]).map((f) => f.filename)
|
||||
@@ -55,15 +63,13 @@ export async function jobsUploadMedia(req: Request, res: Response) {
|
||||
|
||||
await Promise.all(thumbnailGenerationQueue);
|
||||
|
||||
console.timeEnd("jobsUploadMedia");
|
||||
if (req.body.skip_thumbnail) {
|
||||
res.sendStatus(200);
|
||||
} else {
|
||||
JobsListMedia(req, res);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error("Error uploading job media.", { jobid, error: error });
|
||||
res.status(500).send(error);
|
||||
logger.error("Error uploading job media.", {
|
||||
jobid,
|
||||
error: (error as Error).message,
|
||||
});
|
||||
res.status(500).json((error as Error).message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
"file-type": "^16.5.3",
|
||||
"fs-extra": "^10.1.0",
|
||||
"gm": "^1.23.1",
|
||||
"heic-convert": "^1.2.4",
|
||||
"helmet": "^5.0.2",
|
||||
"image-thumbnail": "^1.0.14",
|
||||
"morgan": "^1.10.0",
|
||||
|
||||
77
util/heicConverter.ts
Normal file
77
util/heicConverter.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { Request, Response } from "express";
|
||||
import multer from "multer";
|
||||
|
||||
import GenerateThumbnail from "../util/generateThumbnail";
|
||||
import generateUniqueFilename from "../util/generateUniqueFilename";
|
||||
import { PathToRoFolder } from "../util/pathGenerators";
|
||||
|
||||
import fs from "fs-extra";
|
||||
|
||||
import { access } from "fs/promises";
|
||||
import imageThumbnail from "image-thumbnail";
|
||||
import path, { resolve } 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";
|
||||
import { logger } from "../server";
|
||||
import dotenv from "dotenv";
|
||||
|
||||
const heicConverter = require("heic-convert");
|
||||
|
||||
//gm.subClass();
|
||||
dotenv.config({
|
||||
path: resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`),
|
||||
});
|
||||
|
||||
export async function ConvertHeicFiles(files: Express.Multer.File[]) {
|
||||
for (const file of files) {
|
||||
const type: core.FileTypeResult | undefined = await ft.fromFile(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`;
|
||||
|
||||
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}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function ConvertToJpeg(file: string, newPath: string) {
|
||||
const fileOnDisk: Buffer = await fs.readFile(file);
|
||||
|
||||
const outputBuffer = await heicConverter({
|
||||
buffer: fileOnDisk, // the HEIC file buffer
|
||||
format: "JPEG", // output format
|
||||
quality: process.env.CONVERT_QUALITY || 0.5, // the jpeg compression quality, between 0 and 1
|
||||
});
|
||||
return await fs.writeFile(newPath, outputBuffer);
|
||||
}
|
||||
@@ -18,6 +18,7 @@ export const FolderPaths = {
|
||||
Vendors: path.join(RootDirectory, VendorsFolder),
|
||||
ThumbsSubDir: "/thumbs",
|
||||
BillsSubDir: "/bills",
|
||||
ConvertedOriginalSubDir: "/ConvertedOriginal",
|
||||
StaticPath: "/static",
|
||||
JobsFolder,
|
||||
VendorsFolder,
|
||||
|
||||
Reference in New Issue
Block a user