IO-1998 LMS Zip Download.
This commit is contained in:
77
jobs/jobsDownloadMedia.ts
Normal file
77
jobs/jobsDownloadMedia.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { Request, Response } from "express";
|
||||
import fs from "fs-extra";
|
||||
import multer from "multer";
|
||||
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";
|
||||
|
||||
//param: files: string[] | array of filenames.
|
||||
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 filesList: fs.Dirent[] = (
|
||||
await fs.readdir(PathToRoFolder(jobid), {
|
||||
withFileTypes: true,
|
||||
})
|
||||
).filter((f) => f.isFile() && ListableChecker(f));
|
||||
|
||||
if (files.length === 0) {
|
||||
//Get everything.
|
||||
|
||||
await Promise.all(
|
||||
filesList.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);
|
||||
})
|
||||
);
|
||||
} else {
|
||||
//Get the files that are in the list and see which are requested.
|
||||
await Promise.all(
|
||||
filesList.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);
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
//Send it as a response to download it automatically.
|
||||
// res.setHeader("Content-disposition", "attachment; filename=" + filename);
|
||||
|
||||
zip
|
||||
.generateNodeStream({
|
||||
type: "nodebuffer",
|
||||
streamFiles: true,
|
||||
//encodeFileName: (filename) => `${jobid}.zip`,
|
||||
})
|
||||
.pipe(res);
|
||||
} catch (error) {
|
||||
logger.error("Error downloading job media.", {
|
||||
jobid,
|
||||
error: (error as Error).message,
|
||||
});
|
||||
res.status(500).json((error as Error).message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user