72 lines
2.4 KiB
TypeScript
72 lines
2.4 KiB
TypeScript
import { Request, Response } from "express";
|
|
import fs from "fs-extra";
|
|
import multer from "multer";
|
|
import path from "path";
|
|
import { logger } from "../server.js";
|
|
import GenerateThumbnail from "../util/generateThumbnail.js";
|
|
import generateUniqueFilename from "../util/generateUniqueFilename.js";
|
|
import { ConvertHeicFiles } from "../util/heicConverter.js";
|
|
import { PathToRoFolder } from "../util/pathGenerators.js";
|
|
import { JobsListMedia } from "./jobsListMedia.js";
|
|
|
|
export const JobMediaUploadMulter = multer({
|
|
storage: multer.diskStorage({
|
|
destination: function (req, file, cb) {
|
|
const jobid: string = (req.body.jobid || "").trim();
|
|
const DestinationFolder: string = PathToRoFolder(jobid);
|
|
fs.ensureDirSync(DestinationFolder);
|
|
cb(jobid === "" || jobid === null ? new Error("Job ID not specified.") : null, DestinationFolder);
|
|
},
|
|
filename: function (req, file, cb) {
|
|
logger.debug("Uploading file: ", {
|
|
file: path.basename(file.originalname)
|
|
});
|
|
cb(null, generateUniqueFilename(file));
|
|
}
|
|
})
|
|
});
|
|
|
|
export async function jobsUploadMedia(req: Request, res: Response) {
|
|
const jobid: string = (req.body.jobid || "").trim();
|
|
|
|
try {
|
|
if (!req.files || (req.files as Express.Multer.File[]).length === 0) {
|
|
logger.warning("Upload contained no files.");
|
|
res.status(400).send({
|
|
status: false,
|
|
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) {
|
|
req.headers.skipReponse === "true";
|
|
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)
|
|
);
|
|
const thumbnailGenerationQueue: Promise<string>[] = [];
|
|
|
|
//for each file.path, generate the thumbnail.
|
|
(req.files as Express.Multer.File[]).forEach((file) => {
|
|
thumbnailGenerationQueue.push(GenerateThumbnail(file.path));
|
|
});
|
|
|
|
await Promise.all(thumbnailGenerationQueue);
|
|
|
|
JobsListMedia(req, res);
|
|
}
|
|
} catch (error) {
|
|
logger.error("Error uploading job media.", {
|
|
jobid,
|
|
error: (error as Error).message
|
|
});
|
|
res.status(500).json((error as Error).message);
|
|
}
|
|
}
|