From 7754dc46538694e051a699f194977a2338bac0f6 Mon Sep 17 00:00:00 2001 From: Patrick Fic <> Date: Wed, 4 May 2022 18:13:21 -0700 Subject: [PATCH] Change RO number to jobid. --- bills/billRequestValidator.ts | 4 ++-- bills/billsListMedia.ts | 12 ++++++------ bills/billsUploadMedia.ts | 4 ++-- jobs/jobRequestValidator.ts | 4 ++-- jobs/jobsListMedia.ts | 18 +++++++++--------- jobs/jobsMoveMedia.ts | 8 ++++---- jobs/jobsUploadMedia.ts | 10 ++++++---- server.ts | 4 ++-- util/generateThumbnail.ts | 4 +++- util/pathGenerators.ts | 8 ++++---- 10 files changed, 40 insertions(+), 36 deletions(-) diff --git a/bills/billRequestValidator.ts b/bills/billRequestValidator.ts index d5c669f..665e314 100644 --- a/bills/billRequestValidator.ts +++ b/bills/billRequestValidator.ts @@ -7,7 +7,7 @@ export default function BillRequestValidator( ) { const vendor: string = (req.body.vendor || "").trim(); const invoice_number: string = (req.body.invoice_number || "").trim(); - const ro_number: string = (req.body.ro_number || "").trim(); + const jobid: string = (req.body.jobid || "").trim(); // if (vendor === "") { // res.status(400).json({ error: "No vendor name has been specified." }); @@ -17,7 +17,7 @@ export default function BillRequestValidator( // res.status(400).json({ error: "No invoice number has been specified." }); // return; // } - if (ro_number === "") { + if (jobid === "") { res.status(400).json({ error: "No RO Number has been specified." }); return; } else { diff --git a/bills/billsListMedia.ts b/bills/billsListMedia.ts index e3335d1..6a9fcce 100644 --- a/bills/billsListMedia.ts +++ b/bills/billsListMedia.ts @@ -9,15 +9,15 @@ import { FolderPaths } from "../util/serverInit"; /** @description Bills will use the hierarchy of PDFs stored under the Job first, and then the Bills folder. */ export async function BillsListMedia(req: Request, res: Response) { - const ro_number: string = (req.body.ro_number || "").trim(); + const jobid: string = (req.body.jobid || "").trim(); const vendor: string = (req.body.vendor || "").trim(); const invoice_number: string = (req.body.invoice_number || "").trim(); //Ensure all directories exist. - await fs.ensureDir(PathToRoBillsFolder(ro_number)); + await fs.ensureDir(PathToRoBillsFolder(jobid)); const filesList: fs.Dirent[] = ( - await fs.readdir(PathToRoBillsFolder(ro_number), { + await fs.readdir(PathToRoBillsFolder(jobid), { withFileTypes: true, }) ).filter((f) => f.isFile() && !/(^|\/)\.[^\/\.]/g.test(f.name)); @@ -25,20 +25,20 @@ export async function BillsListMedia(req: Request, res: Response) { const ret: MediaFile[] = await Promise.all( filesList.map(async (file) => { const relativeThumbPath: string = await GenerateThumbnail( - path.join(PathToRoBillsFolder(ro_number), file.name) + path.join(PathToRoBillsFolder(jobid), file.name) ); return { src: GenerateUrl([ FolderPaths.StaticPath, FolderPaths.JobsFolder, - ro_number, + jobid, FolderPaths.BillsSubDir, file.name, ]), thumbnail: GenerateUrl([ FolderPaths.StaticPath, FolderPaths.JobsFolder, - ro_number, + jobid, FolderPaths.BillsSubDir, relativeThumbPath, ]), diff --git a/bills/billsUploadMedia.ts b/bills/billsUploadMedia.ts index 0ec40b7..3a06a11 100644 --- a/bills/billsUploadMedia.ts +++ b/bills/billsUploadMedia.ts @@ -21,8 +21,8 @@ dotenv.config({ export const BillsMediaUploadMulter = multer({ storage: multer.diskStorage({ destination: function (req, file, cb) { - const ro_number: string = (req.body.ro_number || "").trim(); - const DestinationFolder: string = PathToRoBillsFolder(ro_number); + const jobid: string = (req.body.jobid || "").trim(); + const DestinationFolder: string = PathToRoBillsFolder(jobid); fs.ensureDirSync(DestinationFolder); cb(null, DestinationFolder); }, diff --git a/jobs/jobRequestValidator.ts b/jobs/jobRequestValidator.ts index aa7e5b7..dae930b 100644 --- a/jobs/jobRequestValidator.ts +++ b/jobs/jobRequestValidator.ts @@ -5,8 +5,8 @@ export default function ValidateJobBasedRequest( res: Response, next: NextFunction ) { - const ro_number: string = (req.body.ro_number || "").trim(); - if (ro_number === "") { + const jobid: string = (req.body.jobid || "").trim(); + if (jobid === "") { res.status(400).json({ error: "No RO Number has been specified." }); return; } else { diff --git a/jobs/jobsListMedia.ts b/jobs/jobsListMedia.ts index ad9c66a..62a9899 100644 --- a/jobs/jobsListMedia.ts +++ b/jobs/jobsListMedia.ts @@ -8,27 +8,27 @@ import { PathToRoFolder } from "../util/pathGenerators"; import { FolderPaths } from "../util/serverInit"; export async function JobsListMedia(req: Request, res: Response) { - const ro_number: string = (req.body.ro_number || "").trim(); - await fs.ensureDir(PathToRoFolder(ro_number)); + const jobid: string = (req.body.jobid || "").trim(); + await fs.ensureDir(PathToRoFolder(jobid)); let ret: MediaFile[]; if (req.files) { //We just uploaded files, we're going to send only those back. ret = await Promise.all( (req.files as Express.Multer.File[]).map(async (file) => { const relativeThumbPath: string = await GenerateThumbnail( - path.join(FolderPaths.Jobs, ro_number, file.filename) + path.join(FolderPaths.Jobs, jobid, file.filename) ); return { src: GenerateUrl([ FolderPaths.StaticPath, FolderPaths.JobsFolder, - ro_number, + jobid, file.filename, ]), thumbnail: GenerateUrl([ FolderPaths.StaticPath, FolderPaths.JobsFolder, - ro_number, + jobid, relativeThumbPath, ]), thumbnailHeight: 250, @@ -38,7 +38,7 @@ export async function JobsListMedia(req: Request, res: Response) { ); } else { const filesList: fs.Dirent[] = ( - await fs.readdir(PathToRoFolder(ro_number), { + await fs.readdir(PathToRoFolder(jobid), { withFileTypes: true, }) ).filter((f) => f.isFile() && !/(^|\/)\.[^\/\.]/g.test(f.name)); @@ -46,19 +46,19 @@ export async function JobsListMedia(req: Request, res: Response) { ret = await Promise.all( filesList.map(async (file) => { const relativeThumbPath: string = await GenerateThumbnail( - path.join(FolderPaths.Jobs, ro_number, file.name) + path.join(FolderPaths.Jobs, jobid, file.name) ); return { src: GenerateUrl([ FolderPaths.StaticPath, FolderPaths.JobsFolder, - ro_number, + jobid, file.name, ]), thumbnail: GenerateUrl([ FolderPaths.StaticPath, FolderPaths.JobsFolder, - ro_number, + jobid, relativeThumbPath, ]), thumbnailHeight: 250, diff --git a/jobs/jobsMoveMedia.ts b/jobs/jobsMoveMedia.ts index cad0c84..9d1b01f 100644 --- a/jobs/jobsMoveMedia.ts +++ b/jobs/jobsMoveMedia.ts @@ -9,7 +9,7 @@ import { JobsListMedia } from "./jobsListMedia"; import { PathToRoFolder } from "../util/pathGenerators"; export async function JobsMoveMedia(req: Request, res: Response) { - const ro_number: string = (req.body.ro_number || "").trim(); + const jobid: string = (req.body.jobid || "").trim(); const from_ro: string = (req.body.from_ro || "").trim(); const files: string[] = req.body.files; //Just file names. @@ -24,7 +24,7 @@ export async function JobsMoveMedia(req: Request, res: Response) { return; } //Make sure the destination RO directory exists. - await fs.ensureDir(PathToRoFolder(ro_number)); + await fs.ensureDir(PathToRoFolder(jobid)); const movingQueue: Promise[] = []; @@ -32,14 +32,14 @@ export async function JobsMoveMedia(req: Request, res: Response) { movingQueue.push( fs.move( path.join(FolderPaths.Jobs, from_ro, file), - path.join(FolderPaths.Jobs, ro_number, file) + path.join(FolderPaths.Jobs, jobid, file) ) ); movingQueue.push( fs.move( path.join(FolderPaths.Jobs, from_ro, FolderPaths.ThumbsSubDir, file), - path.join(FolderPaths.Jobs, ro_number, FolderPaths.ThumbsSubDir, file) + path.join(FolderPaths.Jobs, jobid, FolderPaths.ThumbsSubDir, file) ) ); }); diff --git a/jobs/jobsUploadMedia.ts b/jobs/jobsUploadMedia.ts index 2ec09be..a112358 100644 --- a/jobs/jobsUploadMedia.ts +++ b/jobs/jobsUploadMedia.ts @@ -11,8 +11,8 @@ import fs from "fs-extra"; export const JobMediaUploadMulter = multer({ storage: multer.diskStorage({ destination: function (req, file, cb) { - const ro_number: string = (req.body.ro_number || "").trim(); - const DestinationFolder: string = PathToRoFolder(ro_number); + const jobid: string = (req.body.jobid || "").trim(); + const DestinationFolder: string = PathToRoFolder(jobid); fs.ensureDirSync(DestinationFolder); cb(null, DestinationFolder); }, @@ -24,7 +24,7 @@ export const JobMediaUploadMulter = multer({ }); export async function jobsUploadMedia(req: Request, res: Response) { - const ro_number: string = (req.body.ro_number || "").trim(); + const jobid: string = (req.body.jobid || "").trim(); try { if (!req.files) { @@ -39,8 +39,10 @@ export async function jobsUploadMedia(req: Request, res: Response) { (req.files as Express.Multer.File[]).forEach((file) => { thumbnailGenerationQueue.push(GenerateThumbnail(file.path)); }); - await Promise.all(thumbnailGenerationQueue); + logger.debug("Pre await", thumbnailGenerationQueue.toString()); + await Promise.all(thumbnailGenerationQueue); + logger.debug("Post Await", thumbnailGenerationQueue.toString()); JobsListMedia(req, res); } } catch (err) { diff --git a/server.ts b/server.ts index 95203b2..0052d56 100644 --- a/server.ts +++ b/server.ts @@ -87,7 +87,7 @@ const app: Express = express(); const port = process.env.PORT; app.use(bodyParser.json({ limit: "50mb" })); app.use(bodyParser.urlencoded({ limit: "50mb", extended: true })); -app.use(FolderPaths.StaticPath, express.static(FolderPaths.Root)); + app.use(cors()); const morganMiddleware = morgan( "combined", //":method :url :status :res[content-length] - :response-time ms" @@ -121,7 +121,7 @@ app.post( ); InitServer(); - +app.use(FolderPaths.StaticPath, express.static(FolderPaths.Root, {})); app.listen(port, () => { logger.info(`ImEX Media Server is running at http://localhost:${port}`); }); diff --git a/util/generateThumbnail.ts b/util/generateThumbnail.ts index 215d593..0e237ab 100644 --- a/util/generateThumbnail.ts +++ b/util/generateThumbnail.ts @@ -7,6 +7,7 @@ import ft from "file-type"; import core from "file-type/core"; import GenerateUrl from "./MediaUrlGen"; import { FolderPaths } from "./serverInit"; +import { logger } from "../server"; var Bluebird = require("bluebird"); Bluebird.promisifyAll(gm.prototype); @@ -23,13 +24,13 @@ export default async function GenerateThumbnail( FolderPaths.ThumbsSubDir, path.parse(path.basename(file)).name + thumbnailExtension ); - try { //Ensure the thumbs directory exists. await fs.ensureDir(path.dirname(thumbPath)); try { await access(thumbPath); + logger.debug("Thumbnail already exists for : " + thumbPath); return path.relative(path.dirname(file), thumbPath); } catch {} @@ -39,6 +40,7 @@ export default async function GenerateThumbnail( const fileOnDisk: Buffer = await fs.readFile(file); await GeneratePdfThumbnail(file, thumbPath); } else { + logger.debug("Thumbnail being created for : " + thumbPath); const thumbnail = await imageThumbnail(file, { responseType: "buffer", height: 250, diff --git a/util/pathGenerators.ts b/util/pathGenerators.ts index 53f59a1..f02ade4 100644 --- a/util/pathGenerators.ts +++ b/util/pathGenerators.ts @@ -1,12 +1,12 @@ import path from "path"; import { FolderPaths } from "./serverInit"; -export function PathToRoFolder(ro_number: string) { - return path.join(FolderPaths.Jobs, ro_number); +export function PathToRoFolder(jobid: string) { + return path.join(FolderPaths.Jobs, jobid); } -export function PathToRoBillsFolder(ro_number: string) { - return path.join(FolderPaths.Jobs, ro_number, FolderPaths.BillsSubDir); +export function PathToRoBillsFolder(jobid: string) { + return path.join(FolderPaths.Jobs, jobid, FolderPaths.BillsSubDir); } export function PathToVendorBillsFile(vendor: string) { return path.join(FolderPaths.Vendors, vendor);