Added media move.
This commit is contained in:
55
jobs/jobsMoveMedia.ts
Normal file
55
jobs/jobsMoveMedia.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
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";
|
||||
|
||||
export async function JobsMoveMedia(req: Request, res: Response) {
|
||||
const ro_number: string = (req.body.ro_number || "").trim();
|
||||
const from_ro: string = (req.body.from_ro || "").trim();
|
||||
const files: string[] = req.body.files; //Just file names.
|
||||
|
||||
//Validate the request is valid and contains everything that it needs.
|
||||
|
||||
if (from_ro === "") {
|
||||
res.status(400).json({ error: "from_ro must be specified. " });
|
||||
return;
|
||||
}
|
||||
if (files.length === 0) {
|
||||
res.status(400).json({ error: "files must be specified. " });
|
||||
return;
|
||||
}
|
||||
//Make sure the destination RO directory exists.
|
||||
await fs.ensureDir(path.join(FolderPaths.Jobs, ro_number));
|
||||
|
||||
const movingQueue: Promise<void>[] = [];
|
||||
|
||||
files.forEach((file) => {
|
||||
movingQueue.push(
|
||||
fs.move(
|
||||
path.join(FolderPaths.Jobs, from_ro, file),
|
||||
path.join(FolderPaths.Jobs, ro_number, file)
|
||||
)
|
||||
);
|
||||
|
||||
movingQueue.push(
|
||||
fs.move(
|
||||
path.join(FolderPaths.Jobs, from_ro, FolderPaths.ThumbsSubDir, file),
|
||||
path.join(FolderPaths.Jobs, ro_number, FolderPaths.ThumbsSubDir, file)
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
//Use AllSettled as it allows for individual moves to fail.
|
||||
//e.g. if the thumbnail does not exist.
|
||||
await Promise.allSettled(movingQueue);
|
||||
|
||||
try {
|
||||
JobsListMedia(req, res);
|
||||
} catch (err) {
|
||||
res.status(500).send(err);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
import { Request, Response } from "express";
|
||||
import fs from "fs-extra";
|
||||
import multer from "multer";
|
||||
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";
|
||||
|
||||
export const JobMediaUploadMulter = multer({
|
||||
storage: multer.diskStorage({
|
||||
@@ -14,7 +13,13 @@ export const JobMediaUploadMulter = multer({
|
||||
cb(null, DestinationFolder);
|
||||
},
|
||||
filename: function (req, file, cb) {
|
||||
cb(null, file.originalname);
|
||||
console.log(path.basename(file.originalname));
|
||||
cb(
|
||||
null,
|
||||
`${file.originalname}-${Math.floor(Date.now() / 1000)}.${path.extname(
|
||||
file.originalname
|
||||
)}`
|
||||
);
|
||||
},
|
||||
}),
|
||||
});
|
||||
@@ -46,10 +51,10 @@ export async function jobsUploadMedia(req: Request, res: Response) {
|
||||
);
|
||||
});
|
||||
await Promise.all(thumbnailGenerationQueue);
|
||||
res.json(thumbnailGenerationQueue);
|
||||
|
||||
JobsListMedia(req, res);
|
||||
}
|
||||
} catch (err) {
|
||||
res.status(500).send(err);
|
||||
}
|
||||
res.json();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user