IO-2221 IO-2323 Correct Download/Delete/Move functions
This commit is contained in:
@@ -1,16 +1,11 @@
|
||||
import { Request, Response } from "express";
|
||||
import fs from "fs-extra";
|
||||
import multer from "multer";
|
||||
import JSZip from "jszip";
|
||||
import path from "path";
|
||||
import { logger } from "../server";
|
||||
import GenerateThumbnail from "../util/generateThumbnail";
|
||||
import generateUniqueFilename from "../util/generateUniqueFilename";
|
||||
import { ConvertHeicFiles } from "../util/heicConverter";
|
||||
import { PathToRoFolder } from "../util/pathGenerators";
|
||||
import { JobsListMedia } from "./jobsListMedia";
|
||||
import JSZip from "jszip";
|
||||
import ListableChecker from "../util/listableChecker";
|
||||
import { JobRelativeFilePath } from "../util/serverInit";
|
||||
import { PathToRoBillsFolder, PathToRoFolder } from "../util/pathGenerators";
|
||||
import { BillsRelativeFilePath, JobRelativeFilePath } from "../util/serverInit";
|
||||
|
||||
//param: files: string[] | array of filenames.
|
||||
export async function jobsDownloadMedia(req: Request, res: Response) {
|
||||
@@ -25,17 +20,21 @@ export async function jobsDownloadMedia(req: Request, res: Response) {
|
||||
logger.debug(`Generating batch download for Job ID ${jobid}`, files);
|
||||
//Prepare the zip file.
|
||||
|
||||
const filesList: fs.Dirent[] = (
|
||||
const jobFileList: fs.Dirent[] = (
|
||||
await fs.readdir(PathToRoFolder(jobid), {
|
||||
withFileTypes: true,
|
||||
})
|
||||
).filter((f) => f.isFile() && ListableChecker(f));
|
||||
const billFileList: fs.Dirent[] = (
|
||||
await fs.readdir(PathToRoBillsFolder(jobid), {
|
||||
withFileTypes: true,
|
||||
})
|
||||
).filter((f) => f.isFile() && ListableChecker(f));
|
||||
|
||||
if (files.length === 0) {
|
||||
//Get everything.
|
||||
|
||||
await Promise.all(
|
||||
filesList.map(async (file) => {
|
||||
jobFileList.map(async (file) => {
|
||||
//Do something async
|
||||
const fileOnDisk: Buffer = await fs.readFile(
|
||||
JobRelativeFilePath(jobid, file.name)
|
||||
@@ -43,10 +42,19 @@ export async function jobsDownloadMedia(req: Request, res: Response) {
|
||||
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(
|
||||
filesList.map(async (file) => {
|
||||
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(
|
||||
@@ -54,7 +62,18 @@ export async function jobsDownloadMedia(req: Request, res: Response) {
|
||||
);
|
||||
zip.file(path.parse(path.basename(file.name)).base, fileOnDisk);
|
||||
}
|
||||
})
|
||||
}),
|
||||
);
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user