Update media file interface and how bills are returned.
This commit is contained in:
@@ -5,7 +5,7 @@ export default function BillRequestValidator(
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) {
|
||||
const vendor: string = (req.body.vendor || "").trim();
|
||||
const vendorid: string = (req.body.vendorid || "").trim();
|
||||
const invoice_number: string = (req.body.invoice_number || "").trim();
|
||||
const jobid: string = (req.body.jobid || "").trim();
|
||||
|
||||
|
||||
@@ -10,43 +10,80 @@ 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 jobid: string = (req.body.jobid || "").trim();
|
||||
const vendor: string = (req.body.vendor || "").trim();
|
||||
//const vendorid: string = (req.body.vendorid || "").trim();
|
||||
const invoice_number: string = (req.body.invoice_number || "").trim();
|
||||
|
||||
let ret: MediaFile[];
|
||||
//Ensure all directories exist.
|
||||
await fs.ensureDir(PathToRoBillsFolder(jobid));
|
||||
|
||||
const filesList: fs.Dirent[] = (
|
||||
await fs.readdir(PathToRoBillsFolder(jobid), {
|
||||
withFileTypes: true,
|
||||
})
|
||||
).filter((f) => f.isFile() && !/(^|\/)\.[^\/\.]/g.test(f.name));
|
||||
if (req.files) {
|
||||
ret = await Promise.all(
|
||||
(req.files as Express.Multer.File[]).map(async (file) => {
|
||||
const relativeThumbPath: string = await GenerateThumbnail(
|
||||
path.join(PathToRoBillsFolder(jobid), file.filename)
|
||||
);
|
||||
return {
|
||||
src: GenerateUrl([
|
||||
FolderPaths.StaticPath,
|
||||
FolderPaths.JobsFolder,
|
||||
jobid,
|
||||
FolderPaths.BillsSubDir,
|
||||
file.filename,
|
||||
]),
|
||||
thumbnail: GenerateUrl([
|
||||
FolderPaths.StaticPath,
|
||||
FolderPaths.JobsFolder,
|
||||
jobid,
|
||||
FolderPaths.BillsSubDir,
|
||||
relativeThumbPath,
|
||||
]),
|
||||
thumbnailHeight: 250,
|
||||
thumbnailWidth: 250,
|
||||
filename: file.filename,
|
||||
};
|
||||
})
|
||||
);
|
||||
} else {
|
||||
let filesList: fs.Dirent[] = (
|
||||
await fs.readdir(PathToRoBillsFolder(jobid), {
|
||||
withFileTypes: true,
|
||||
})
|
||||
).filter(
|
||||
(f) =>
|
||||
f.isFile() &&
|
||||
!/(^|\/)\.[^\/\.]/g.test(f.name) &&
|
||||
(invoice_number !== ""
|
||||
? f.name.toLowerCase().includes(invoice_number.toLowerCase())
|
||||
: true)
|
||||
);
|
||||
|
||||
const ret: MediaFile[] = await Promise.all(
|
||||
filesList.map(async (file) => {
|
||||
const relativeThumbPath: string = await GenerateThumbnail(
|
||||
path.join(PathToRoBillsFolder(jobid), file.name)
|
||||
);
|
||||
return {
|
||||
src: GenerateUrl([
|
||||
FolderPaths.StaticPath,
|
||||
FolderPaths.JobsFolder,
|
||||
jobid,
|
||||
FolderPaths.BillsSubDir,
|
||||
file.name,
|
||||
]),
|
||||
thumbnail: GenerateUrl([
|
||||
FolderPaths.StaticPath,
|
||||
FolderPaths.JobsFolder,
|
||||
jobid,
|
||||
FolderPaths.BillsSubDir,
|
||||
relativeThumbPath,
|
||||
]),
|
||||
thumbnailHeight: 250,
|
||||
thumbnailWidth: 250,
|
||||
};
|
||||
})
|
||||
);
|
||||
ret = await Promise.all(
|
||||
filesList.map(async (file) => {
|
||||
const relativeThumbPath: string = await GenerateThumbnail(
|
||||
path.join(PathToRoBillsFolder(jobid), file.name)
|
||||
);
|
||||
return {
|
||||
src: GenerateUrl([
|
||||
FolderPaths.StaticPath,
|
||||
FolderPaths.JobsFolder,
|
||||
jobid,
|
||||
FolderPaths.BillsSubDir,
|
||||
file.name,
|
||||
]),
|
||||
thumbnail: GenerateUrl([
|
||||
FolderPaths.StaticPath,
|
||||
FolderPaths.JobsFolder,
|
||||
jobid,
|
||||
FolderPaths.BillsSubDir,
|
||||
relativeThumbPath,
|
||||
]),
|
||||
thumbnailHeight: 250,
|
||||
thumbnailWidth: 250,
|
||||
filename: file.name,
|
||||
};
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
res.json(ret);
|
||||
}
|
||||
|
||||
@@ -56,13 +56,13 @@ export async function BillsUploadMedia(req: Request, res: Response) {
|
||||
|
||||
//for each file.path, generate the thumbnail.
|
||||
(req.files as Express.Multer.File[]).forEach((file) => {
|
||||
const vendor: string = (req.body.vendor || "").trim();
|
||||
const vendorid: string = (req.body.vendorid || "").trim();
|
||||
const invoice_number: string = (req.body.invoice_number || "").trim();
|
||||
|
||||
copyQueue.push(
|
||||
(async () => {
|
||||
const target: string = path.join(
|
||||
PathToVendorBillsFile(vendor),
|
||||
PathToVendorBillsFile(vendorid),
|
||||
file.filename
|
||||
);
|
||||
await fs.ensureDir(path.dirname(target));
|
||||
@@ -72,9 +72,8 @@ export async function BillsUploadMedia(req: Request, res: Response) {
|
||||
//Copy Queue is not awaited - we don't care if it finishes before we serve up the thumbnails from the jobs directory.
|
||||
});
|
||||
}
|
||||
res.sendStatus(200);
|
||||
|
||||
// BillsListMedia(req, res);
|
||||
BillsListMedia(req, res);
|
||||
}
|
||||
} catch (err) {
|
||||
res.status(500).send(err);
|
||||
|
||||
Reference in New Issue
Block a user