Files
bodyshop-media-server/bills/billsListMedia.ts
2022-05-04 18:13:21 -07:00

53 lines
1.7 KiB
TypeScript

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 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(jobid));
const filesList: fs.Dirent[] = (
await fs.readdir(PathToRoBillsFolder(jobid), {
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(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,
};
})
);
res.json(ret);
}