1.0.14
This commit is contained in:
@@ -12,77 +12,60 @@ export async function jobsDownloadMedia(req: Request, res: Response) {
|
||||
const jobid: string = (req.body.jobid || "").trim();
|
||||
|
||||
try {
|
||||
//Do we need all files or just some files?
|
||||
const files: string[] = req.body.files || [];
|
||||
const zip: JSZip = new JSZip();
|
||||
await fs.ensureDir(PathToRoFolder(jobid));
|
||||
|
||||
logger.debug(`Generating batch download for Job ID ${jobid}`, files);
|
||||
//Prepare the zip file.
|
||||
|
||||
const jobFileList: fs.Dirent[] = (
|
||||
await fs.readdir(PathToRoFolder(jobid), {
|
||||
withFileTypes: true
|
||||
})
|
||||
await fs.readdir(PathToRoFolder(jobid), { withFileTypes: true })
|
||||
).filter((f) => f.isFile() && ListableChecker(f));
|
||||
const billFileList: fs.Dirent[] = (
|
||||
await fs.readdir(PathToRoBillsFolder(jobid), {
|
||||
withFileTypes: true
|
||||
})
|
||||
await fs.readdir(PathToRoBillsFolder(jobid), { withFileTypes: true })
|
||||
).filter((f) => f.isFile() && ListableChecker(f));
|
||||
|
||||
if (files.length === 0) {
|
||||
//Get everything.
|
||||
// Helper to add files to the zip
|
||||
const addFilesToZip = async (
|
||||
fileList: fs.Dirent[],
|
||||
relativePathFn: (jobid: string, filename: string) => string
|
||||
) => {
|
||||
await Promise.all(
|
||||
jobFileList.map(async (file) => {
|
||||
//Do something async
|
||||
const fileOnDisk: Buffer = await fs.readFile(JobRelativeFilePath(jobid, file.name));
|
||||
zip.file(path.parse(path.basename(file.name)).base, fileOnDisk);
|
||||
})
|
||||
);
|
||||
await Promise.all(
|
||||
billFileList.map(async (file) => {
|
||||
//Do something async
|
||||
const fileOnDisk: Buffer = await fs.readFile(BillsRelativeFilePath(jobid, file.name));
|
||||
zip.file(path.parse(path.basename(file.name)).base, fileOnDisk);
|
||||
})
|
||||
);
|
||||
} else {
|
||||
//Get the files that are in the list and see which are requested.
|
||||
await Promise.all(
|
||||
jobFileList.map(async (file) => {
|
||||
if (files.includes(path.parse(path.basename(file.name)).base)) {
|
||||
// File is in the set of requested files.
|
||||
const fileOnDisk: Buffer = await fs.readFile(JobRelativeFilePath(jobid, file.name));
|
||||
zip.file(path.parse(path.basename(file.name)).base, fileOnDisk);
|
||||
fileList.map(async (file) => {
|
||||
const baseName = path.basename(file.name);
|
||||
if (files.length === 0 || files.includes(baseName)) {
|
||||
try {
|
||||
const fileOnDisk: Buffer = await fs.readFile(relativePathFn(jobid, file.name));
|
||||
zip.file(baseName, fileOnDisk);
|
||||
} catch (err) {
|
||||
logger.warn(`Could not add file to zip: ${file.name}`, err);
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
await Promise.all(
|
||||
billFileList.map(async (file) => {
|
||||
if (files.includes(path.parse(path.basename(file.name)).base)) {
|
||||
// File is in the set of requested files.
|
||||
const fileOnDisk: Buffer = await fs.readFile(BillsRelativeFilePath(jobid, file.name));
|
||||
zip.file(path.parse(path.basename(file.name)).base, fileOnDisk);
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
//Send it as a response to download it automatically.
|
||||
// res.setHeader("Content-disposition", "attachment; filename=" + filename);
|
||||
};
|
||||
|
||||
await addFilesToZip(jobFileList, JobRelativeFilePath);
|
||||
await addFilesToZip(billFileList, BillsRelativeFilePath);
|
||||
|
||||
// Set headers for download
|
||||
res.setHeader("Content-Disposition", `attachment; filename="${jobid}.zip"`);
|
||||
res.setHeader("Content-Type", "application/zip");
|
||||
|
||||
zip
|
||||
.generateNodeStream({
|
||||
type: "nodebuffer",
|
||||
streamFiles: true
|
||||
//encodeFileName: (filename) => `${jobid}.zip`,
|
||||
})
|
||||
.pipe(res);
|
||||
.pipe(res)
|
||||
.on("finish", () => {
|
||||
logger.debug(`Zip stream finished for Job ID ${jobid}`);
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error("Error downloading job media.", {
|
||||
jobid,
|
||||
error: (error as Error).message
|
||||
});
|
||||
res.status(500).json((error as Error).message);
|
||||
if (!res.headersSent) res.status(500).json((error as Error).message);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user