Add in HEIC converter on uploaded images.
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
MEDIA_PATH=~/Desktop/IMS
|
MEDIA_PATH=~/Desktop/IMS
|
||||||
PORT=8000
|
PORT=8000
|
||||||
IMS_TOKEN=
|
IMS_TOKEN=
|
||||||
|
CONVERT_QUALITY=0.5
|
||||||
|
KEEP_CONVERTED_ORIGINALS=TRUE
|
||||||
@@ -8,6 +8,7 @@ import GenerateThumbnail from "../util/generateThumbnail";
|
|||||||
import generateUniqueFilename, {
|
import generateUniqueFilename, {
|
||||||
generateUniqueBillFilename,
|
generateUniqueBillFilename,
|
||||||
} from "../util/generateUniqueFilename";
|
} from "../util/generateUniqueFilename";
|
||||||
|
import { ConvertHeicFiles } from "../util/heicConverter";
|
||||||
import {
|
import {
|
||||||
PathToRoBillsFolder,
|
PathToRoBillsFolder,
|
||||||
PathToVendorBillsFile,
|
PathToVendorBillsFile,
|
||||||
@@ -55,6 +56,8 @@ export async function BillsUploadMedia(req: Request, res: Response) {
|
|||||||
message: "No file uploaded",
|
message: "No file uploaded",
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
await ConvertHeicFiles(req.files as Express.Multer.File[]);
|
||||||
|
|
||||||
const thumbnailGenerationQueue: Promise<string>[] = [];
|
const thumbnailGenerationQueue: Promise<string>[] = [];
|
||||||
|
|
||||||
//for each file.path, generate the thumbnail.
|
//for each file.path, generate the thumbnail.
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import { PathToRoFolder } from "../util/pathGenerators";
|
|||||||
import { FolderPaths } from "../util/serverInit";
|
import { FolderPaths } from "../util/serverInit";
|
||||||
|
|
||||||
export async function JobsListMedia(req: Request, res: Response) {
|
export async function JobsListMedia(req: Request, res: Response) {
|
||||||
console.time("JobsListMedia");
|
|
||||||
const jobid: string = (req.body.jobid || "").trim();
|
const jobid: string = (req.body.jobid || "").trim();
|
||||||
await fs.ensureDir(PathToRoFolder(jobid));
|
await fs.ensureDir(PathToRoFolder(jobid));
|
||||||
logger.debug("Listing media for job: " + 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);
|
res.json(ret);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error("Error listing job media.", { jobid, error });
|
logger.error("Error listing job media.", { jobid, error });
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import generateUniqueFilename from "../util/generateUniqueFilename";
|
|||||||
import { PathToRoFolder } from "../util/pathGenerators";
|
import { PathToRoFolder } from "../util/pathGenerators";
|
||||||
import { JobsListMedia } from "./jobsListMedia";
|
import { JobsListMedia } from "./jobsListMedia";
|
||||||
import fs from "fs-extra";
|
import fs from "fs-extra";
|
||||||
|
import { ConvertHeicFiles } from "../util/heicConverter";
|
||||||
|
|
||||||
export const JobMediaUploadMulter = multer({
|
export const JobMediaUploadMulter = multer({
|
||||||
storage: multer.diskStorage({
|
storage: multer.diskStorage({
|
||||||
@@ -31,7 +32,6 @@ export const JobMediaUploadMulter = multer({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export async function jobsUploadMedia(req: Request, res: Response) {
|
export async function jobsUploadMedia(req: Request, res: Response) {
|
||||||
console.time("jobsUploadMedia");
|
|
||||||
const jobid: string = (req.body.jobid || "").trim();
|
const jobid: string = (req.body.jobid || "").trim();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -42,6 +42,14 @@ export async function jobsUploadMedia(req: Request, res: Response) {
|
|||||||
message: "No file uploaded",
|
message: "No file uploaded",
|
||||||
});
|
});
|
||||||
} else {
|
} 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(
|
logger.debug(
|
||||||
"Creating thumbnails for newly uploaded media",
|
"Creating thumbnails for newly uploaded media",
|
||||||
(req.files as Express.Multer.File[]).map((f) => f.filename)
|
(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);
|
await Promise.all(thumbnailGenerationQueue);
|
||||||
|
|
||||||
console.timeEnd("jobsUploadMedia");
|
JobsListMedia(req, res);
|
||||||
if (req.body.skip_thumbnail) {
|
|
||||||
res.sendStatus(200);
|
|
||||||
} else {
|
|
||||||
JobsListMedia(req, res);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error("Error uploading job media.", { jobid, error: error });
|
logger.error("Error uploading job media.", {
|
||||||
res.status(500).send(error);
|
jobid,
|
||||||
|
error: (error as Error).message,
|
||||||
|
});
|
||||||
|
res.status(500).json((error as Error).message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
"file-type": "^16.5.3",
|
"file-type": "^16.5.3",
|
||||||
"fs-extra": "^10.1.0",
|
"fs-extra": "^10.1.0",
|
||||||
"gm": "^1.23.1",
|
"gm": "^1.23.1",
|
||||||
|
"heic-convert": "^1.2.4",
|
||||||
"helmet": "^5.0.2",
|
"helmet": "^5.0.2",
|
||||||
"image-thumbnail": "^1.0.14",
|
"image-thumbnail": "^1.0.14",
|
||||||
"morgan": "^1.10.0",
|
"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),
|
Vendors: path.join(RootDirectory, VendorsFolder),
|
||||||
ThumbsSubDir: "/thumbs",
|
ThumbsSubDir: "/thumbs",
|
||||||
BillsSubDir: "/bills",
|
BillsSubDir: "/bills",
|
||||||
|
ConvertedOriginalSubDir: "/ConvertedOriginal",
|
||||||
StaticPath: "/static",
|
StaticPath: "/static",
|
||||||
JobsFolder,
|
JobsFolder,
|
||||||
VendorsFolder,
|
VendorsFolder,
|
||||||
|
|||||||
Reference in New Issue
Block a user