Package Updates

This commit is contained in:
Allan Carr
2024-07-23 09:49:47 -07:00
parent f295488195
commit 58055fd1b3
22 changed files with 1686 additions and 3807 deletions

View File

@@ -1,10 +1,6 @@
import { Request, Response, NextFunction } from "express";
export default function BillRequestValidator(
req: Request,
res: Response,
next: NextFunction
) {
export default function BillRequestValidator(req: Request, res: Response, next: NextFunction) {
const vendorid: string = (req.body.vendorid || "").trim();
const invoice_number: string = (req.body.invoice_number || "").trim();
const jobid: string = (req.body.jobid || "").trim();

View File

@@ -22,17 +22,10 @@ export async function BillsListMedia(req: Request, res: Response) {
if (req.files) {
ret = await Promise.all(
(req.files as Express.Multer.File[]).map(async (file) => {
const relativeFilePath: string = path.join(
PathToRoBillsFolder(jobid),
file.filename
);
const relativeFilePath: string = path.join(PathToRoBillsFolder(jobid), file.filename);
const relativeThumbPath: string = await GenerateThumbnail(
relativeFilePath
);
const type: core.FileTypeResult | undefined = await ft.fromFile(
relativeFilePath
);
const relativeThumbPath: string = await GenerateThumbnail(relativeFilePath);
const type: core.FileTypeResult | undefined = await ft.fileTypeFromFile(relativeFilePath);
return {
type,
size: file.size,
@@ -41,72 +34,57 @@ export async function BillsListMedia(req: Request, res: Response) {
FolderPaths.JobsFolder,
jobid,
FolderPaths.BillsSubDir,
file.filename,
file.filename
]),
thumbnail: GenerateUrl([
FolderPaths.StaticPath,
FolderPaths.JobsFolder,
jobid,
FolderPaths.BillsSubDir,
relativeThumbPath,
relativeThumbPath
]),
thumbnailHeight: 250,
thumbnailWidth: 250,
filename: file.filename,
relativeFilePath,
relativeFilePath
};
})
);
} else {
let filesList: fs.Dirent[] = (
await fs.readdir(PathToRoBillsFolder(jobid), {
withFileTypes: true,
withFileTypes: true
})
).filter(
(f) =>
f.isFile() &&
!/(^|\/)\.[^\/\.]/g.test(f.name) &&
(invoice_number !== ""
? f.name.toLowerCase().includes(invoice_number.toLowerCase())
: true) &&
(invoice_number !== "" ? f.name.toLowerCase().includes(invoice_number.toLowerCase()) : true) &&
ListableChecker(f)
);
ret = await Promise.all(
filesList.map(async (file) => {
const relativeFilePath: string = path.join(
PathToRoBillsFolder(jobid),
file.name
);
const relativeFilePath: string = path.join(PathToRoBillsFolder(jobid), file.name);
const relativeThumbPath: string = await GenerateThumbnail(
relativeFilePath
);
const type: core.FileTypeResult | undefined = await ft.fromFile(
relativeFilePath
);
const relativeThumbPath: string = await GenerateThumbnail(relativeFilePath);
const type: core.FileTypeResult | undefined = await ft.fileTypeFromFile(relativeFilePath);
const fileSize = await fs.stat(relativeFilePath);
return {
type,
size: fileSize.size,
src: GenerateUrl([
FolderPaths.StaticPath,
FolderPaths.JobsFolder,
jobid,
FolderPaths.BillsSubDir,
file.name,
]),
src: GenerateUrl([FolderPaths.StaticPath, FolderPaths.JobsFolder, jobid, FolderPaths.BillsSubDir, file.name]),
thumbnail: GenerateUrl([
FolderPaths.StaticPath,
FolderPaths.JobsFolder,
jobid,
FolderPaths.BillsSubDir,
relativeThumbPath,
relativeThumbPath
]),
thumbnailHeight: 250,
thumbnailWidth: 250,
filename: file.name,
relativeFilePath,
relativeFilePath
};
})
);

View File

@@ -5,18 +5,13 @@ import multer from "multer";
import path, { resolve } from "path";
import { logger } from "../server";
import GenerateThumbnail from "../util/generateThumbnail";
import generateUniqueFilename, {
generateUniqueBillFilename,
} from "../util/generateUniqueFilename";
import generateUniqueFilename, { generateUniqueBillFilename } from "../util/generateUniqueFilename";
import { ConvertHeicFiles } from "../util/heicConverter";
import {
PathToRoBillsFolder,
PathToVendorBillsFile,
} from "../util/pathGenerators";
import { PathToRoBillsFolder, PathToVendorBillsFile } from "../util/pathGenerators";
import { BillsListMedia } from "./billsListMedia";
dotenv.config({
path: resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`),
path: resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`)
});
export const BillsMediaUploadMulter = multer({
@@ -25,27 +20,20 @@ export const BillsMediaUploadMulter = multer({
const jobid: string = (req.body.jobid || "").trim();
const DestinationFolder: string = PathToRoBillsFolder(jobid);
fs.ensureDirSync(DestinationFolder);
cb(
jobid === "" || jobid === null
? new Error("Job ID not specified.")
: null,
DestinationFolder
);
cb(jobid === "" || jobid === null ? new Error("Job ID not specified.") : null, DestinationFolder);
},
filename: function (req, file, cb) {
logger.info("Uploading file: ", {
file: path.basename(file.originalname),
file: path.basename(file.originalname)
});
const invoice_number: string = (req.body.invoice_number || "").trim();
cb(
invoice_number === "" || invoice_number === null
? new Error("Invoice number not specified.")
: null,
invoice_number === "" || invoice_number === null ? new Error("Invoice number not specified.") : null,
generateUniqueBillFilename(file, invoice_number)
);
},
}),
}
})
});
export async function BillsUploadMedia(req: Request, res: Response) {
@@ -53,7 +41,7 @@ export async function BillsUploadMedia(req: Request, res: Response) {
if (!req.files) {
res.send({
status: false,
message: "No file uploaded",
message: "No file uploaded"
});
} else {
await ConvertHeicFiles(req.files as Express.Multer.File[]);
@@ -76,10 +64,7 @@ export async function BillsUploadMedia(req: Request, res: Response) {
copyQueue.push(
(async () => {
const target: string = path.join(
PathToVendorBillsFile(vendorid),
file.filename
);
const target: string = path.join(PathToVendorBillsFile(vendorid), file.filename);
await fs.ensureDir(path.dirname(target));
await fs.copyFile(file.path, target);
})()
@@ -93,7 +78,7 @@ export async function BillsUploadMedia(req: Request, res: Response) {
} catch (error) {
logger.error("Error while uploading Bill Media", {
files: req.files,
error,
error
});
res.status(500).send(error);
}