IO-2221 IO-2323 Correct Download/Delete/Move functions

This commit is contained in:
Allan Carr
2023-07-20 11:19:21 -07:00
parent a678383dee
commit aa3b567a5b
9 changed files with 415 additions and 334 deletions

View File

@@ -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.