Added debug statements.

This commit is contained in:
Patrick Fic
2022-05-10 14:53:58 -07:00
parent 270be1d360
commit 16e2852994
3 changed files with 111 additions and 96 deletions

View File

@@ -1,6 +1,7 @@
import { Request, Response } from "express"; import { Request, Response } from "express";
import fs from "fs-extra"; import fs from "fs-extra";
import path from "path"; import path from "path";
import { logger } from "../server";
import GenerateThumbnail from "../util/generateThumbnail"; import GenerateThumbnail from "../util/generateThumbnail";
import MediaFile from "../util/interfaces/MediaFile"; import MediaFile from "../util/interfaces/MediaFile";
import GenerateUrl from "../util/MediaUrlGen"; import GenerateUrl from "../util/MediaUrlGen";
@@ -10,65 +11,70 @@ import { FolderPaths } from "../util/serverInit";
export async function JobsListMedia(req: Request, res: Response) { export async function JobsListMedia(req: Request, res: Response) {
const jobid: string = (req.body.jobid || "").trim(); const jobid: string = (req.body.jobid || "").trim();
await fs.ensureDir(PathToRoFolder(jobid)); await fs.ensureDir(PathToRoFolder(jobid));
logger.debug("Listing media for job.", PathToRoFolder(jobid));
let ret: MediaFile[]; let ret: MediaFile[];
if (req.files) { try {
//We just uploaded files, we're going to send only those back. if (req.files) {
ret = await Promise.all( //We just uploaded files, we're going to send only those back.
(req.files as Express.Multer.File[]).map(async (file) => { ret = await Promise.all(
const relativeThumbPath: string = await GenerateThumbnail( (req.files as Express.Multer.File[]).map(async (file) => {
path.join(FolderPaths.Jobs, jobid, file.filename) const relativeThumbPath: string = await GenerateThumbnail(
); path.join(FolderPaths.Jobs, jobid, file.filename)
return { );
src: GenerateUrl([ return {
FolderPaths.StaticPath, src: GenerateUrl([
FolderPaths.JobsFolder, FolderPaths.StaticPath,
jobid, FolderPaths.JobsFolder,
file.filename, jobid,
]), file.filename,
thumbnail: GenerateUrl([ ]),
FolderPaths.StaticPath, thumbnail: GenerateUrl([
FolderPaths.JobsFolder, FolderPaths.StaticPath,
jobid, FolderPaths.JobsFolder,
relativeThumbPath, jobid,
]), relativeThumbPath,
thumbnailHeight: 250, ]),
thumbnailWidth: 250, thumbnailHeight: 250,
filename: file.filename, thumbnailWidth: 250,
}; filename: file.filename,
}) };
); })
} else { );
const filesList: fs.Dirent[] = ( } else {
await fs.readdir(PathToRoFolder(jobid), { const filesList: fs.Dirent[] = (
withFileTypes: true, await fs.readdir(PathToRoFolder(jobid), {
}) withFileTypes: true,
).filter((f) => f.isFile() && !/(^|\/)\.[^\/\.]/g.test(f.name)); })
).filter((f) => f.isFile() && !/(^|\/)\.[^\/\.]/g.test(f.name));
ret = await Promise.all( ret = await Promise.all(
filesList.map(async (file) => { filesList.map(async (file) => {
const relativeThumbPath: string = await GenerateThumbnail( const relativeThumbPath: string = await GenerateThumbnail(
path.join(FolderPaths.Jobs, jobid, file.name) path.join(FolderPaths.Jobs, jobid, file.name)
); );
return { return {
src: GenerateUrl([ src: GenerateUrl([
FolderPaths.StaticPath, FolderPaths.StaticPath,
FolderPaths.JobsFolder, FolderPaths.JobsFolder,
jobid, jobid,
file.name, file.name,
]), ]),
thumbnail: GenerateUrl([ thumbnail: GenerateUrl([
FolderPaths.StaticPath, FolderPaths.StaticPath,
FolderPaths.JobsFolder, FolderPaths.JobsFolder,
jobid, jobid,
relativeThumbPath, relativeThumbPath,
]), ]),
thumbnailHeight: 250, thumbnailHeight: 250,
thumbnailWidth: 250, thumbnailWidth: 250,
filename: file.name, filename: file.name,
}; };
}) })
); );
}
res.json(ret);
} catch (error) {
res.status(500).json(error);
} }
res.json(ret);
} }

View File

@@ -7,48 +7,54 @@ import { FolderPaths } from "../util/serverInit";
import multer from "multer"; import multer from "multer";
import { JobsListMedia } from "./jobsListMedia"; import { JobsListMedia } from "./jobsListMedia";
import { PathToRoFolder } from "../util/pathGenerators"; import { PathToRoFolder } from "../util/pathGenerators";
import { logger } from "../server";
export async function JobsMoveMedia(req: Request, res: Response) { export async function JobsMoveMedia(req: Request, res: Response) {
const jobid: string = (req.body.jobid || "").trim(); const jobid: string = (req.body.jobid || "").trim();
const from_jobid: string = (req.body.from_jobid || "").trim(); const from_jobid: string = (req.body.from_jobid || "").trim();
const files: string[] = req.body.files; //Just file names. const files: string[] = req.body.files; //Just file names.
//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;
}
if (files.length === 0) {
res.status(400).json({ error: "files must be specified. " });
return;
}
//Make sure the destination RO directory exists.
await fs.ensureDir(PathToRoFolder(jobid));
const movingQueue: Promise<void>[] = [];
files.forEach((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)
)
);
});
//Use AllSettled as it allows for individual moves to fail.
//e.g. if the thumbnail does not exist.
await Promise.allSettled(movingQueue);
try { 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;
}
if (files.length === 0) {
res.status(400).json({ error: "files must be specified. " });
return;
}
//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)
)
);
movingQueue.push(
fs.move(
path.join(
FolderPaths.Jobs,
from_jobid,
FolderPaths.ThumbsSubDir,
file
),
path.join(FolderPaths.Jobs, jobid, 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);
JobsListMedia(req, res); JobsListMedia(req, res);
} catch (err) { } catch (err) {
res.status(500).send(err); res.status(500).send(err);

View File

@@ -17,7 +17,7 @@ export const JobMediaUploadMulter = multer({
cb(null, DestinationFolder); cb(null, DestinationFolder);
}, },
filename: function (req, file, cb) { filename: function (req, file, cb) {
logger.info("Uploading file: ", path.basename(file.originalname)); logger.debug("Uploading file: ", path.basename(file.originalname));
cb(null, generateUniqueFilename(file)); cb(null, generateUniqueFilename(file));
}, },
}), }),
@@ -33,6 +33,10 @@ export async function jobsUploadMedia(req: Request, res: Response) {
message: "No file uploaded", message: "No file uploaded",
}); });
} else { } else {
logger.log(
"Creating thumbnails for newly uploaded media",
(req.files as Express.Multer.File[]).map((f) => f.filename)
);
const thumbnailGenerationQueue: Promise<string>[] = []; const thumbnailGenerationQueue: Promise<string>[] = [];
//for each file.path, generate the thumbnail. //for each file.path, generate the thumbnail.
@@ -40,9 +44,8 @@ export async function jobsUploadMedia(req: Request, res: Response) {
thumbnailGenerationQueue.push(GenerateThumbnail(file.path)); thumbnailGenerationQueue.push(GenerateThumbnail(file.path));
}); });
logger.debug("Pre await", thumbnailGenerationQueue.toString());
await Promise.all(thumbnailGenerationQueue); await Promise.all(thumbnailGenerationQueue);
logger.debug("Post Await", thumbnailGenerationQueue.toString());
JobsListMedia(req, res); JobsListMedia(req, res);
} }
} catch (err) { } catch (err) {