IO-2221 IO-2323 Correct Download/Delete/Move functions
This commit is contained in:
@@ -2,14 +2,10 @@ import { Request, Response } from "express";
|
||||
import fs from "fs-extra";
|
||||
import path from "path";
|
||||
import { logger } from "../server";
|
||||
import GenerateThumbnail from "../util/generateThumbnail";
|
||||
import MediaFile from "../util/interfaces/MediaFile";
|
||||
import ListableChecker from "../util/listableChecker";
|
||||
import GenerateUrl from "../util/MediaUrlGen";
|
||||
import { PathToRoFolder } from "../util/pathGenerators";
|
||||
import { FolderPaths, JobRelativeFilePath } from "../util/serverInit";
|
||||
import ft from "file-type";
|
||||
import core from "file-type/core";
|
||||
import { PathToRoBillsFolder, PathToRoFolder } from "../util/pathGenerators";
|
||||
import { BillsRelativeFilePath, JobRelativeFilePath } from "../util/serverInit";
|
||||
|
||||
export async function JobsDeleteMedia(req: Request, res: Response) {
|
||||
const jobid: string = (req.body.jobid || "").trim();
|
||||
@@ -18,12 +14,33 @@ export async function JobsDeleteMedia(req: Request, res: Response) {
|
||||
logger.debug("Deleteing media for job: " + PathToRoFolder(jobid));
|
||||
let ret: MediaFile[];
|
||||
try {
|
||||
//We just uploaded files, we're going to send only those back.
|
||||
await Promise.all(
|
||||
files.map(async (filename) => {
|
||||
const relativeFilePath: string = JobRelativeFilePath(jobid, filename);
|
||||
// Setup lists for both file locations
|
||||
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));
|
||||
|
||||
await fs.remove(relativeFilePath);
|
||||
// Check both list for the files that need to be deleted
|
||||
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.
|
||||
await fs.remove(JobRelativeFilePath(jobid, file.name));
|
||||
}
|
||||
})
|
||||
);
|
||||
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.
|
||||
await fs.remove(BillsRelativeFilePath(jobid, file.name));
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
import { Request, Response } from "express";
|
||||
import ft from "file-type";
|
||||
import core from "file-type/core";
|
||||
import fs from "fs-extra";
|
||||
import path from "path";
|
||||
import { logger } from "../server";
|
||||
import GenerateUrl from "../util/MediaUrlGen";
|
||||
import GenerateThumbnail from "../util/generateThumbnail";
|
||||
import MediaFile from "../util/interfaces/MediaFile";
|
||||
import ListableChecker from "../util/listableChecker";
|
||||
import GenerateUrl from "../util/MediaUrlGen";
|
||||
import { PathToRoFolder } from "../util/pathGenerators";
|
||||
import { FolderPaths, JobRelativeFilePath } from "../util/serverInit";
|
||||
import ft from "file-type";
|
||||
import core from "file-type/core";
|
||||
|
||||
export async function JobsListMedia(req: Request, res: Response) {
|
||||
const jobid: string = (req.body.jobid || "").trim();
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
import { Request, Response } from "express";
|
||||
import fs from "fs-extra";
|
||||
import path from "path";
|
||||
import GenerateThumbnail from "../util/generateThumbnail";
|
||||
import GenerateUrl from "../util/MediaUrlGen";
|
||||
import { FolderPaths } from "../util/serverInit";
|
||||
import multer from "multer";
|
||||
import { JobsListMedia } from "./jobsListMedia";
|
||||
import { PathToRoFolder } from "../util/pathGenerators";
|
||||
import { logger } from "../server";
|
||||
import ListableChecker from "../util/listableChecker";
|
||||
import { PathToRoBillsFolder, PathToRoFolder } from "../util/pathGenerators";
|
||||
import { FolderPaths } from "../util/serverInit";
|
||||
import { JobsListMedia } from "./jobsListMedia";
|
||||
|
||||
export async function JobsMoveMedia(req: Request, res: Response) {
|
||||
const jobid: string = (req.body.jobid || "").trim();
|
||||
@@ -16,7 +14,6 @@ export async function JobsMoveMedia(req: Request, res: Response) {
|
||||
|
||||
try {
|
||||
//Validate the request is valid and contains everything that it needs.
|
||||
|
||||
if (from_jobid === "") {
|
||||
res.status(400).json({ error: "from_jobid must be specified. " });
|
||||
return;
|
||||
@@ -25,30 +22,81 @@ export async function JobsMoveMedia(req: Request, res: Response) {
|
||||
res.status(400).json({ error: "files must be specified. " });
|
||||
return;
|
||||
}
|
||||
|
||||
// Setup lists for both file locations
|
||||
const jobFileList: string[] = (
|
||||
await fs.readdir(PathToRoFolder(from_jobid), {
|
||||
withFileTypes: true,
|
||||
})
|
||||
)
|
||||
.filter((f) => f.isFile() && ListableChecker(f))
|
||||
.map((dirent) => dirent.name);
|
||||
const billFileList: string[] = (
|
||||
await fs.readdir(PathToRoBillsFolder(from_jobid), {
|
||||
withFileTypes: true,
|
||||
})
|
||||
)
|
||||
.filter((f) => f.isFile() && ListableChecker(f))
|
||||
.map((dirent) => dirent.name);
|
||||
|
||||
//Make sure the destination RO directory exists.
|
||||
await fs.ensureDir(PathToRoFolder(jobid));
|
||||
logger.debug("Moving job based media.", { jobid, from_jobid, files });
|
||||
const movingQueue: Promise<void>[] = [];
|
||||
|
||||
files.forEach((file) => {
|
||||
movingQueue.push(
|
||||
fs.move(
|
||||
path.join(FolderPaths.Jobs, from_jobid, file),
|
||||
path.join(FolderPaths.Jobs, jobid, file)
|
||||
)
|
||||
);
|
||||
if (jobFileList.includes(file)) {
|
||||
movingQueue.push(
|
||||
fs.move(
|
||||
path.join(FolderPaths.Jobs, from_jobid, file),
|
||||
path.join(FolderPaths.Jobs, jobid, file)
|
||||
)
|
||||
);
|
||||
|
||||
movingQueue.push(
|
||||
fs.move(
|
||||
path.join(
|
||||
FolderPaths.Jobs,
|
||||
from_jobid,
|
||||
FolderPaths.ThumbsSubDir,
|
||||
file
|
||||
),
|
||||
path.join(FolderPaths.Jobs, jobid, FolderPaths.ThumbsSubDir, file)
|
||||
)
|
||||
);
|
||||
movingQueue.push(
|
||||
fs.move(
|
||||
path.join(
|
||||
FolderPaths.Jobs,
|
||||
from_jobid,
|
||||
FolderPaths.ThumbsSubDir,
|
||||
file
|
||||
),
|
||||
path.join(FolderPaths.Jobs, jobid, FolderPaths.ThumbsSubDir, file)
|
||||
)
|
||||
);
|
||||
}
|
||||
if (billFileList.includes(file)) {
|
||||
movingQueue.push(
|
||||
fs.move(
|
||||
path.join(
|
||||
FolderPaths.Jobs,
|
||||
from_jobid,
|
||||
FolderPaths.BillsSubDir,
|
||||
file
|
||||
),
|
||||
path.join(FolderPaths.Jobs, jobid, FolderPaths.BillsSubDir, file)
|
||||
)
|
||||
);
|
||||
|
||||
movingQueue.push(
|
||||
fs.move(
|
||||
path.join(
|
||||
FolderPaths.Jobs,
|
||||
from_jobid,
|
||||
FolderPaths.BillsSubDir,
|
||||
FolderPaths.ThumbsSubDir,
|
||||
file
|
||||
),
|
||||
path.join(
|
||||
FolderPaths.Jobs,
|
||||
jobid,
|
||||
FolderPaths.BillsSubDir,
|
||||
FolderPaths.ThumbsSubDir,
|
||||
file
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
//Use AllSettled as it allows for individual moves to fail.
|
||||
|
||||
Reference in New Issue
Block a user