Added bills upload & list
This commit is contained in:
78
bills/billsUploadMedia.ts
Normal file
78
bills/billsUploadMedia.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import dotenv from "dotenv";
|
||||
import { Request, Response } from "express";
|
||||
import fs from "fs-extra";
|
||||
import multer from "multer";
|
||||
import path, { resolve } from "path";
|
||||
import { logger } from "../server";
|
||||
import GenerateThumbnail from "../util/generateThumbnail";
|
||||
import generateUniqueFilename from "../util/generateUniqueFilename";
|
||||
import {
|
||||
PathToRoBillsFolder,
|
||||
PathToVendorBillsFile,
|
||||
} from "../util/pathGenerators";
|
||||
import { BillsListMedia } from "./billsListMedia";
|
||||
|
||||
dotenv.config({
|
||||
path: resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`),
|
||||
});
|
||||
|
||||
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);
|
||||
fs.ensureDirSync(DestinationFolder);
|
||||
cb(null, DestinationFolder);
|
||||
},
|
||||
filename: function (req, file, cb) {
|
||||
logger.info("Uploading file: ", path.basename(file.originalname));
|
||||
cb(null, generateUniqueFilename(file));
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
export async function BillsUploadMedia(req: Request, res: Response) {
|
||||
try {
|
||||
if (!req.files) {
|
||||
res.send({
|
||||
status: false,
|
||||
message: "No file uploaded",
|
||||
});
|
||||
} else {
|
||||
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);
|
||||
//Check to see if it should be duplicated to the vendor.
|
||||
if (process.env.DUPLICATE_BILL_TO_VENDOR) {
|
||||
const copyQueue: Promise<void>[] = [];
|
||||
|
||||
//for each file.path, generate the thumbnail.
|
||||
(req.files as Express.Multer.File[]).forEach((file) => {
|
||||
const vendor: string = (req.body.vendor || "").trim();
|
||||
const invoice_number: string = (req.body.invoice_number || "").trim();
|
||||
|
||||
copyQueue.push(
|
||||
(async () => {
|
||||
const target: string = path.join(
|
||||
PathToVendorBillsFile(vendor),
|
||||
invoice_number + path.extname(file.filename)
|
||||
);
|
||||
|
||||
await fs.ensureDir(path.dirname(target));
|
||||
await fs.copyFile(file.path, target);
|
||||
})()
|
||||
);
|
||||
|
||||
//Copy Queue is not awaited - we don't care if it finishes before we serve up the thumbnails.
|
||||
});
|
||||
}
|
||||
BillsListMedia(req, res);
|
||||
}
|
||||
} catch (err) {
|
||||
res.status(500).send(err);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user