Added bills upload & list
This commit is contained in:
50
bills/billsListMedia.ts
Normal file
50
bills/billsListMedia.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { Request, Response } from "express";
|
||||
import fs from "fs-extra";
|
||||
import path from "path";
|
||||
import GenerateThumbnail from "../util/generateThumbnail";
|
||||
import MediaFile from "../util/interfaces/MediaFile";
|
||||
import GenerateUrl from "../util/MediaUrlGen";
|
||||
import { PathToRoBillsFolder, PathToRoFolder } from "../util/pathGenerators";
|
||||
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 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));
|
||||
|
||||
const filesList: fs.Dirent[] = (
|
||||
await fs.readdir(PathToRoBillsFolder(ro_number), {
|
||||
withFileTypes: true,
|
||||
})
|
||||
).filter((f) => f.isFile() && !/(^|\/)\.[^\/\.]/g.test(f.name));
|
||||
|
||||
const ret: MediaFile[] = await Promise.all(
|
||||
filesList.map(async (file) => {
|
||||
const relativeThumbPath: string = await GenerateThumbnail(
|
||||
path.join(PathToRoBillsFolder(ro_number), file.name)
|
||||
);
|
||||
return {
|
||||
src: GenerateUrl([
|
||||
FolderPaths.StaticPath,
|
||||
FolderPaths.JobsFolder,
|
||||
ro_number,
|
||||
FolderPaths.BillsSubDir,
|
||||
file.name,
|
||||
]),
|
||||
thumb: GenerateUrl([
|
||||
FolderPaths.StaticPath,
|
||||
FolderPaths.JobsFolder,
|
||||
ro_number,
|
||||
FolderPaths.BillsSubDir,
|
||||
relativeThumbPath,
|
||||
]),
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
res.json(ret);
|
||||
}
|
||||
Reference in New Issue
Block a user