This commit is contained in:
Allan Carr
2025-07-13 00:17:08 -07:00
parent 231130267f
commit 7f782d5a64
19 changed files with 2564 additions and 1416 deletions

View File

@@ -1,11 +1,16 @@
import { NextFunction, Request, Response } from "express";
const validateJobRequest: (req: Request, res: Response, next: NextFunction) => void = (req, res, next) => {
const jobId: string = (req.body.jobid || "").trim();
if (jobId === "") {
return res.status(400).json({ error: "No RO Number has been specified." });
const validateJobRequest = (req: Request, res: Response, next: NextFunction) => {
try {
const jobid: string = (req.body.jobid || "").trim();
if (!jobid) {
res.status(400).json({ error: "No RO Number has been specified." });
return;
}
next();
} catch (error) {
res.status(500).json({ error: "Error validating job request.", details: (error as Error).message });
}
next();
};
export default validateJobRequest;

View File

@@ -1,64 +1,351 @@
import { Job, Queue, QueueEvents, Worker } from "bullmq";
import { Request, Response } from "express";
import fs from "fs-extra";
import path from "path";
import { logger } from "../server.js";
import MediaFile from "../util/interfaces/MediaFile.js";
import ListableChecker from "../util/listableChecker.js";
import { PathToRoBillsFolder, PathToRoFolder } from "../util/pathGenerators.js";
import { PathToRoBillsFolder, PathToRoFolder, PathToVendorBillsFile } from "../util/pathGenerators.js";
import { BillsRelativeFilePath, FolderPaths, JobRelativeFilePath } from "../util/serverInit.js";
const DELETE_QUEUE_NAME = "deleteQueue";
const connectionOpts = {
host: "localhost",
port: 6379,
maxRetriesPerRequest: 3,
enableReadyCheck: true,
reconnectOnError: (err: Error) => err.message.includes("READONLY")
};
const deleteQueue = new Queue(DELETE_QUEUE_NAME, {
connection: connectionOpts,
defaultJobOptions: {
removeOnComplete: 10,
removeOnFail: 5,
attempts: 3,
backoff: { type: "exponential", delay: 2000 }
}
});
const deleteQueueEvents = new QueueEvents(DELETE_QUEUE_NAME, {
connection: connectionOpts
});
const deleteWorker = new Worker(
DELETE_QUEUE_NAME,
async (job: Job<{ jobid: string; files: string[] }>) => {
const { jobid, files } = job.data;
logger.debug(`[DeleteWorker] Starting delete operation for job ${jobid} with ${files.length} files`);
try {
await job.updateProgress(5);
const result = await processDeleteOperation(jobid, files, job);
await job.updateProgress(100);
logger.debug(`[DeleteWorker] Completed delete operation for job ${jobid}`);
return result;
} catch (error) {
logger.error(`[DeleteWorker] Error deleting files for job ${jobid}:`, error);
throw error;
}
},
{
connection: connectionOpts,
concurrency: 2 // Limit concurrent delete operations
}
);
// Worker event listeners for logging
deleteWorker.on("ready", () => {
logger.debug("[DeleteWorker] Worker is ready");
});
deleteWorker.on("active", (job, prev) => {
logger.debug(`[DeleteWorker] Job ${job.id} active (previous: ${prev})`);
});
deleteWorker.on("completed", async (job) => {
logger.debug(`[DeleteWorker] Job ${job.id} completed`);
});
deleteWorker.on("failed", (job, err) => {
logger.error(`[DeleteWorker] Job ${job?.id} failed:`, err);
});
deleteWorker.on("stalled", (jobId) => {
logger.error(`[DeleteWorker] Job stalled: ${jobId}`);
});
deleteWorker.on("error", (err) => {
logger.error("[DeleteWorker] Worker error:", err);
});
// Queue event listeners
deleteQueue.on("waiting", (job) => {
logger.debug(`[DeleteQueue] Job waiting in queue: job ${job.data.jobid} - ${job.data.files.length} files`);
});
deleteQueue.on("error", (err) => {
logger.error("[DeleteQueue] Queue error:", err);
});
async function processDeleteOperation(
jobid: string,
files: string[],
job?: Job
): Promise<{ deleted: number; failed: number }> {
await fs.ensureDir(PathToRoFolder(jobid));
logger.debug("Deleting media for job: " + PathToRoFolder(jobid));
try {
// Setup lists for both file locations
async function readFilteredDir(dirPath: string): Promise<fs.Dirent[]> {
const filtered: fs.Dirent[] = [];
try {
const dir = await fs.opendir(dirPath);
for await (const dirent of dir) {
if (dirent.isFile() && ListableChecker(dirent)) {
filtered.push(dirent);
}
}
} catch (err) {
logger.error(`Failed to read directory: ${dirPath}`, err);
}
return filtered;
}
const jobFileList = await readFilteredDir(PathToRoFolder(jobid));
const billFileList = await readFilteredDir(PathToRoBillsFolder(jobid));
if (job) await job.updateProgress(15);
// Helper function for safe file deletion
const safeUnlink = async (filePath: string, logPrefix: string = "[DeleteWorker] ") => {
try {
// lstat first to ensure it's a file and the handle is safe to unlink
if (await fs.pathExists(filePath)) {
const stats = await fs.lstat(filePath);
if (stats.isFile()) {
await fs.unlink(filePath);
logger.debug(`${logPrefix}Deleted: ${filePath}`);
}
}
} catch (err) {
logger.warn(`${logPrefix}Failed to delete ${filePath}: ${err}`);
}
};
// Helper to delete a file and its thumbnails
const deleteFileWithThumbs = async (mediaFile: MediaFile, logPrefix: string) => {
try {
await safeUnlink(mediaFile.path, logPrefix);
// Delete thumbnails
const thumbDir = path.dirname(mediaFile.thumbnailPath);
const baseThumb = path.basename(mediaFile.thumbnailPath, path.extname(mediaFile.thumbnailPath));
logger.debug(`${logPrefix}Deleting thumbnails from: ${thumbDir}, baseThumb: ${baseThumb}`);
for (const ext of [".jpg", ".png"]) {
const thumbPath = path.join(thumbDir, `${baseThumb}${ext}`);
await safeUnlink(thumbPath, logPrefix);
}
// Delete ConvertedOriginal file if it exists
// The ConvertedOriginal folder contains the original files with the same filename but original extension
const convertedOriginalDir = path.join(path.dirname(mediaFile.path), FolderPaths.ConvertedOriginalSubDir);
try {
if (await fs.pathExists(convertedOriginalDir)) {
const convertedOriginalFiles = await fs.readdir(convertedOriginalDir);
const currentFileName = path.basename(mediaFile.path, path.extname(mediaFile.path));
logger.debug(`Looking for ConvertedOriginal files with base name: ${currentFileName}`, {
convertedOriginalDir,
currentFileName,
availableFiles: convertedOriginalFiles
});
for (const file of convertedOriginalFiles) {
const fileBaseName = path.basename(file, path.extname(file));
// Match files that have the same base name (same filename, potentially different extension)
if (fileBaseName === currentFileName) {
const convertedOriginalPath = path.join(convertedOriginalDir, file);
await safeUnlink(convertedOriginalPath, logPrefix);
logger.debug(`Found and deleted ConvertedOriginal file: ${convertedOriginalPath}`);
}
}
}
} catch (error) {
logger.warn(`Error checking/deleting ConvertedOriginal files for ${mediaFile.path}:`, error);
}
} catch (error) {
logger.error(`${logPrefix}Error in deleteFileWithThumbs for ${mediaFile.path}:`, error);
throw error;
}
};
// Convert to MediaFile objects for better type safety
const jobMediaFiles: MediaFile[] = jobFileList.map((file) => {
const thumbName = file.name.replace(/\.[^/.]+$/, ".jpg");
const thumbPath = path.join(FolderPaths.Jobs, jobid, FolderPaths.ThumbsSubDir, thumbName);
const filePath = JobRelativeFilePath(jobid, file.name);
return {
name: file.name,
path: filePath,
thumbnailPath: thumbPath,
src: filePath,
thumbnail: thumbPath,
thumbnailHeight: 0,
thumbnailWidth: 0,
filename: file.name
};
});
// Delete job files with proper error handling
const jobDeletions = jobMediaFiles
.filter((mediaFile) => files.includes(path.basename(mediaFile.filename)))
.map((mediaFile) => deleteFileWithThumbs(mediaFile, "[DeleteWorker] "));
// Prepare bill media files
const billMediaFiles: MediaFile[] = billFileList.map((file) => {
const thumbName = file.name.replace(/\.[^/.]+$/, ".jpg");
const thumbPath = path.join(FolderPaths.Jobs, jobid, FolderPaths.BillsSubDir, FolderPaths.ThumbsSubDir, thumbName);
const filePath = BillsRelativeFilePath(jobid, file.name);
return {
name: file.name,
path: filePath,
thumbnailPath: thumbPath,
src: filePath,
thumbnail: thumbPath,
thumbnailHeight: 0,
thumbnailWidth: 0,
filename: file.name
};
});
// Delete bill files using the helper function
const billDeletions = billMediaFiles
.filter((mediaFile) => files.includes(path.basename(mediaFile.filename)))
.map((mediaFile) => deleteFileWithThumbs(mediaFile, "[DeleteWorker] Bill: "));
// Delete vendor duplicates if DUPLICATE_BILL_TO_VENDOR is enabled
const vendorDeletions: Promise<any>[] = [];
const duplicateToVendor = process.env.DUPLICATE_BILL_TO_VENDOR === "true";
if (duplicateToVendor) {
const billFilesToDelete = billMediaFiles
.filter((mediaFile) => files.includes(path.basename(mediaFile.filename)))
.map(mediaFile => path.basename(mediaFile.filename));
for (const billFile of billFilesToDelete) {
vendorDeletions.push(
(async () => {
try {
// Search for this file in all vendor directories
const vendorsDir = FolderPaths.Vendors;
if (await fs.pathExists(vendorsDir)) {
const vendors = await fs.readdir(vendorsDir, { withFileTypes: true });
for (const vendor of vendors) {
if (vendor.isDirectory()) {
const vendorFilePath = path.join(vendorsDir, vendor.name, billFile);
if (await fs.pathExists(vendorFilePath)) {
await safeUnlink(vendorFilePath, "[DeleteWorker] Vendor: ");
logger.debug(`[DeleteWorker] Deleted vendor file: ${vendorFilePath}`);
}
}
}
}
} catch (error) {
logger.warn(`[DeleteWorker] Failed to delete vendor copies for ${billFile}:`, error);
}
})()
);
}
}
if (job) await job.updateProgress(80);
// Wait for all deletions to complete
const results = await Promise.allSettled([...jobDeletions, ...billDeletions, ...vendorDeletions]);
const failed = results.filter(r => r.status === 'rejected').length;
const deleted = results.filter(r => r.status === 'fulfilled').length;
logger.debug(`[DeleteWorker] Delete operation completed: ${deleted} successful, ${failed} failed`);
return { deleted, failed };
} catch (error) {
logger.error("[DeleteWorker] Error in processDeleteOperation:", error);
throw error;
}
}
export async function JobsDeleteMedia(req: Request, res: Response) {
const jobid: string = (req.body.jobid || "").trim();
const files: string[] = req.body.files || [];
await fs.ensureDir(PathToRoFolder(jobid));
logger.debug("Deleteing media for job: " + PathToRoFolder(jobid));
let ret: MediaFile[];
try {
// 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));
if (!files.length) {
res.status(400).json({ error: "files must be specified." });
return;
}
// 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 fs.remove(
path.join(FolderPaths.Jobs, jobid, FolderPaths.ThumbsSubDir, file.name.replace(/\.[^/.]+$/, ".png"))
);
}
})
);
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));
await fs.remove(
path.join(
FolderPaths.Jobs,
jobid,
FolderPaths.BillsSubDir,
FolderPaths.ThumbsSubDir,
file.name.replace(/\.[^/.]+$/, ".png")
)
);
}
})
);
// For small operations (1-5 files), process synchronously for immediate feedback
if (files.length <= 5) {
logger.debug("Processing small delete operation synchronously");
await processDeleteOperation(jobid, files);
res.sendStatus(200);
return;
}
// For larger operations, use BullMQ but still return success immediately
logger.debug(`[JobsDeleteMedia] Queuing delete operation for ${files.length} files`);
const job = await deleteQueue.add("deleteMedia", { jobid, files });
// Return success immediately (optimistic response)
res.sendStatus(200);
// Process in background - if it fails, files will still be there on next refresh
job.waitUntilFinished(deleteQueueEvents)
.then(() => {
logger.debug(`[JobsDeleteMedia] Background delete completed for job ${job.id}`);
})
.catch((error) => {
logger.error(`[JobsDeleteMedia] Background delete failed for job ${job.id}:`, error);
});
if (!res.headersSent) res.sendStatus(200);
} catch (error) {
logger.error("Error deleting job media.", { jobid, error });
if (!res.headersSent) res.status(500).json(error);
if (!res.headersSent) res.status(500).json({ error: "Failed to delete media", details: error });
}
}
/**
* Get the status of a delete operation job
*/
export async function JobsDeleteStatus(req: Request, res: Response) {
const { jobId } = req.params;
try {
const job = await Job.fromId(deleteQueue, jobId);
if (!job) {
res.status(404).json({ error: "Job not found" });
return;
}
const state = await job.getState();
const progress = job.progress;
res.json({
jobId,
state,
progress,
data: job.data,
finishedOn: job.finishedOn,
processedOn: job.processedOn,
failedReason: job.failedReason
});
} catch (error) {
logger.error("Error getting delete job status:", error);
res.status(500).json({ error: "Failed to get job status" });
}
}

View File

@@ -12,77 +12,60 @@ 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 jobFileList: fs.Dirent[] = (
await fs.readdir(PathToRoFolder(jobid), {
withFileTypes: true
})
await fs.readdir(PathToRoFolder(jobid), { withFileTypes: true })
).filter((f) => f.isFile() && ListableChecker(f));
const billFileList: fs.Dirent[] = (
await fs.readdir(PathToRoBillsFolder(jobid), {
withFileTypes: true
})
await fs.readdir(PathToRoBillsFolder(jobid), { withFileTypes: true })
).filter((f) => f.isFile() && ListableChecker(f));
if (files.length === 0) {
//Get everything.
// Helper to add files to the zip
const addFilesToZip = async (
fileList: fs.Dirent[],
relativePathFn: (jobid: string, filename: string) => string
) => {
await Promise.all(
jobFileList.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);
})
);
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(
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(JobRelativeFilePath(jobid, file.name));
zip.file(path.parse(path.basename(file.name)).base, fileOnDisk);
fileList.map(async (file) => {
const baseName = path.basename(file.name);
if (files.length === 0 || files.includes(baseName)) {
try {
const fileOnDisk: Buffer = await fs.readFile(relativePathFn(jobid, file.name));
zip.file(baseName, fileOnDisk);
} catch (err) {
logger.warn(`Could not add file to zip: ${file.name}`, err);
}
}
})
);
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.
// res.setHeader("Content-disposition", "attachment; filename=" + filename);
};
await addFilesToZip(jobFileList, JobRelativeFilePath);
await addFilesToZip(billFileList, BillsRelativeFilePath);
// Set headers for download
res.setHeader("Content-Disposition", `attachment; filename="${jobid}.zip"`);
res.setHeader("Content-Type", "application/zip");
zip
.generateNodeStream({
type: "nodebuffer",
streamFiles: true
//encodeFileName: (filename) => `${jobid}.zip`,
})
.pipe(res);
.pipe(res)
.on("finish", () => {
logger.debug(`Zip stream finished for Job ID ${jobid}`);
});
} catch (error) {
logger.error("Error downloading job media.", {
jobid,
error: (error as Error).message
});
res.status(500).json((error as Error).message);
if (!res.headersSent) res.status(500).json((error as Error).message);
}
}

View File

@@ -14,56 +14,14 @@ export async function JobsListMedia(req: Request, res: Response) {
const jobid: string = (req.body.jobid || "").trim();
await fs.ensureDir(PathToRoFolder(jobid));
logger.debug("Listing media for job: " + PathToRoFolder(jobid));
let ret: MediaFile[];
try {
let ret: MediaFile[];
if (req.files) {
//We just uploaded files, we're going to send only those back.
ret = await Promise.all(
(req.files as Express.Multer.File[]).map(async (file) => {
const relativeFilePath: string = JobRelativeFilePath(jobid, file.filename);
const relativeThumbPath: string = await GenerateThumbnail(relativeFilePath);
const type: FileTypeResult | undefined = await fileTypeFromFile(relativeFilePath);
return {
type,
size: file.size,
src: GenerateUrl([FolderPaths.StaticPath, FolderPaths.JobsFolder, jobid, file.filename]),
thumbnail: GenerateUrl([FolderPaths.StaticPath, FolderPaths.JobsFolder, jobid, relativeThumbPath]),
thumbnailHeight: 250,
thumbnailWidth: 250,
filename: file.filename,
relativeFilePath
};
})
);
ret = await processUploadedFiles(req.files as Express.Multer.File[], jobid);
} else {
const filesList: fs.Dirent[] = (
await fs.readdir(PathToRoFolder(jobid), {
withFileTypes: true
})
).filter((f) => f.isFile() && ListableChecker(f));
ret = await Promise.all(
filesList.map(async (file) => {
const relativeFilePath: string = JobRelativeFilePath(jobid, file.name);
const relativeThumbPath: string = await GenerateThumbnail(relativeFilePath);
const type: FileTypeResult | undefined = await fileTypeFromFile(relativeFilePath);
const fileSize = await fs.stat(relativeFilePath);
return {
type,
size: fileSize.size,
src: GenerateUrl([FolderPaths.StaticPath, FolderPaths.JobsFolder, jobid, file.name]),
thumbnail: GenerateUrl([FolderPaths.StaticPath, FolderPaths.JobsFolder, jobid, relativeThumbPath]),
thumbnailHeight: 250,
thumbnailWidth: 250,
filename: file.name,
relativeFilePath
};
})
);
ret = await processExistingFiles(jobid);
}
if (!res.headersSent) res.json(ret);
@@ -72,3 +30,114 @@ export async function JobsListMedia(req: Request, res: Response) {
if (!res.headersSent) res.status(500).json(error);
}
}
async function processUploadedFiles(files: Express.Multer.File[], jobid: string): Promise<MediaFile[]> {
const processFile = async (file: Express.Multer.File): Promise<MediaFile> => {
const relativeFilePath: string = JobRelativeFilePath(jobid, file.filename);
try {
const relativeThumbPath: string = await GenerateThumbnail(relativeFilePath);
const type: FileTypeResult | undefined = await Promise.race([
fileTypeFromFile(relativeFilePath),
new Promise<undefined>((resolve) => setTimeout(() => resolve(undefined), 5000))
]);
return {
type,
size: file.size,
src: GenerateUrl([FolderPaths.StaticPath, FolderPaths.JobsFolder, jobid, file.filename]),
thumbnail: GenerateUrl([FolderPaths.StaticPath, FolderPaths.JobsFolder, jobid, relativeThumbPath]),
thumbnailHeight: 250,
thumbnailWidth: 250,
filename: file.filename,
name: file.filename,
path: relativeFilePath,
thumbnailPath: relativeThumbPath
};
} catch (error) {
logger.error(`Error processing uploaded file ${file.filename}:`, error);
return {
type: undefined,
size: file.size,
src: GenerateUrl([FolderPaths.StaticPath, FolderPaths.JobsFolder, jobid, file.filename]),
thumbnail: GenerateUrl([FolderPaths.StaticPath, "assets", "file.svg"]),
thumbnailHeight: 250,
thumbnailWidth: 250,
filename: file.filename,
name: file.filename,
path: relativeFilePath,
thumbnailPath: ""
};
}
};
return (await Promise.all(files.map(processFile))).filter((r): r is MediaFile => r !== null);
}
async function processExistingFiles(jobid: string): Promise<MediaFile[]> {
const dirPath = PathToRoFolder(jobid);
const mediaFiles: MediaFile[] = [];
const dir = await fs.opendir(dirPath);
for await (const dirent of dir) {
if (!dirent.isFile() || !ListableChecker(dirent)) continue;
const file = dirent.name;
const relativeFilePath: string = JobRelativeFilePath(jobid, file);
try {
if (!(await fs.pathExists(relativeFilePath))) {
logger.warn(`File no longer exists: ${relativeFilePath}`);
continue;
}
const fileStats = await Promise.race([
fs.stat(relativeFilePath),
new Promise<never>((_, reject) => setTimeout(() => reject(new Error("File stat timeout")), 5000))
]);
const relativeThumbPath: string = await GenerateThumbnail(relativeFilePath);
const type: FileTypeResult | undefined = await Promise.race([
fileTypeFromFile(relativeFilePath),
new Promise<undefined>((resolve) => setTimeout(() => resolve(undefined), 5000))
]);
mediaFiles.push({
type,
size: fileStats.size,
src: GenerateUrl([FolderPaths.StaticPath, FolderPaths.JobsFolder, jobid, file]),
thumbnail: GenerateUrl([FolderPaths.StaticPath, FolderPaths.JobsFolder, jobid, relativeThumbPath]),
thumbnailHeight: 250,
thumbnailWidth: 250,
filename: file,
name: file,
path: relativeFilePath,
thumbnailPath: relativeThumbPath
});
} catch (error) {
logger.error(`Error processing existing file ${file}:`, error);
try {
const fileStats = await fs.stat(relativeFilePath);
mediaFiles.push({
type: undefined,
size: fileStats.size,
src: GenerateUrl([FolderPaths.StaticPath, FolderPaths.JobsFolder, jobid, file]),
thumbnail: GenerateUrl([FolderPaths.StaticPath, "assets", "file.svg"]),
thumbnailHeight: 250,
thumbnailWidth: 250,
filename: file,
name: file,
path: relativeFilePath,
thumbnailPath: ""
});
} catch (statError) {
logger.error(`Could not get stats for ${file}:`, statError);
}
}
}
return mediaFiles;
}

View File

@@ -1,3 +1,4 @@
import { Job, Queue, QueueEvents, Worker } from "bullmq";
import { Request, Response } from "express";
import fs from "fs-extra";
import path from "path";
@@ -7,90 +8,299 @@ import { PathToRoBillsFolder, PathToRoFolder } from "../util/pathGenerators.js";
import { FolderPaths } from "../util/serverInit.js";
import { JobsListMedia } from "./jobsListMedia.js";
const MOVE_QUEUE_NAME = "moveQueue";
const connectionOpts = {
host: "localhost",
port: 6379,
maxRetriesPerRequest: 3,
enableReadyCheck: true,
reconnectOnError: (err: Error) => err.message.includes("READONLY")
};
const moveQueue = new Queue(MOVE_QUEUE_NAME, {
connection: connectionOpts,
defaultJobOptions: {
removeOnComplete: 10,
removeOnFail: 5,
attempts: 3,
backoff: { type: "exponential", delay: 2000 }
}
});
const moveQueueEvents = new QueueEvents(MOVE_QUEUE_NAME, {
connection: connectionOpts
});
const moveWorker = new Worker(
MOVE_QUEUE_NAME,
async (job: Job<{ jobid: string; from_jobid: string; files: string[] }>) => {
const { jobid, from_jobid, files } = job.data;
logger.debug(`[MoveWorker] Starting move operation from ${from_jobid} to ${jobid} for ${files.length} files`);
try {
await job.updateProgress(5);
const result = await processMoveOperation(jobid, from_jobid, files, job);
await job.updateProgress(100);
logger.debug(`[MoveWorker] Completed move operation from ${from_jobid} to ${jobid}`);
return result;
} catch (error) {
logger.error(`[MoveWorker] Error moving files from ${from_jobid} to ${jobid}:`, error);
throw error;
}
},
{
connection: connectionOpts,
concurrency: 2 // Limit concurrent move operations to avoid I/O overwhelm
}
);
// Worker event listeners for logging
moveWorker.on("ready", () => {
logger.debug("[MoveWorker] Worker is ready");
});
moveWorker.on("active", (job, prev) => {
logger.debug(`[MoveWorker] Job ${job.id} active (previous: ${prev})`);
});
moveWorker.on("completed", async (job) => {
logger.debug(`[MoveWorker] Job ${job.id} completed`);
});
moveWorker.on("failed", (job, err) => {
logger.error(`[MoveWorker] Job ${job?.id} failed:`, err);
});
moveWorker.on("stalled", (jobId) => {
logger.error(`[MoveWorker] Job stalled: ${jobId}`);
});
moveWorker.on("error", (err) => {
logger.error("[MoveWorker] Worker error:", err);
});
// Queue event listeners
moveQueue.on("waiting", (job) => {
logger.debug(`[MoveQueue] Job waiting in queue: ${job.data.from_jobid} -> ${job.data.jobid}`);
});
moveQueue.on("error", (err) => {
logger.error("[MoveQueue] Queue error:", err);
});
async function processMoveOperation(
jobid: string,
from_jobid: string,
files: string[],
job?: Job
): Promise<{ moved: number; failed: number }> {
try {
const jobFileList: string[] = [];
const jobDir = await fs.opendir(PathToRoFolder(from_jobid));
for await (const dirent of jobDir) {
if (dirent.isFile() && ListableChecker(dirent)) jobFileList.push(dirent.name);
}
const billFileList: string[] = [];
const billDir = await fs.opendir(PathToRoBillsFolder(from_jobid));
for await (const dirent of billDir) {
if (dirent.isFile() && ListableChecker(dirent)) billFileList.push(dirent.name);
}
await fs.ensureDir(PathToRoFolder(jobid));
logger.debug("Moving job based media.", { jobid, from_jobid, files });
if (job) await job.updateProgress(15);
const moveOps: Promise<any>[] = [];
let processedFiles = 0;
const totalFiles = files.length;
for (const file of files) {
if (jobFileList.includes(file)) {
// Move main file
moveOps.push(
fs
.move(path.join(FolderPaths.Jobs, from_jobid, file), path.join(FolderPaths.Jobs, jobid, file), {
overwrite: true
})
.then(() => logger.debug(`[MoveWorker] Moved main file: ${file}`))
.catch((err) => logger.warn(`[MoveWorker] Failed to move main file ${file}:`, err))
);
// Move thumbnails
const baseThumb = file.replace(/\.[^/.]+$/, "");
for (const ext of [".jpg", ".png"]) {
moveOps.push(
fs
.move(
path.join(FolderPaths.Jobs, from_jobid, FolderPaths.ThumbsSubDir, `${baseThumb}${ext}`),
path.join(FolderPaths.Jobs, jobid, FolderPaths.ThumbsSubDir, `${baseThumb}${ext}`),
{ overwrite: true }
)
.then(() => logger.debug(`[MoveWorker] Moved thumbnail: ${baseThumb}${ext}`))
.catch(() => {}) // Thumbnails might not exist
);
}
// Move ConvertedOriginal file if it exists
const baseFileName = file.replace(/\.[^/.]+$/, "");
const sourceConvertedDir = path.join(FolderPaths.Jobs, from_jobid, FolderPaths.ConvertedOriginalSubDir);
const targetConvertedDir = path.join(FolderPaths.Jobs, jobid, FolderPaths.ConvertedOriginalSubDir);
moveOps.push(
(async () => {
try {
if (await fs.pathExists(sourceConvertedDir)) {
const convertedOriginalFiles = await fs.readdir(sourceConvertedDir);
for (const convertedFile of convertedOriginalFiles) {
const convertedFileBaseName = path.basename(convertedFile, path.extname(convertedFile));
if (convertedFileBaseName === baseFileName) {
await fs.ensureDir(targetConvertedDir);
await fs.move(
path.join(sourceConvertedDir, convertedFile),
path.join(targetConvertedDir, convertedFile),
{ overwrite: true }
);
logger.debug(`[MoveWorker] Moved ConvertedOriginal: ${convertedFile}`);
}
}
}
} catch (error) {
logger.warn(`[MoveWorker] Failed to move ConvertedOriginal for ${file}:`, error);
}
})()
);
}
if (billFileList.includes(file)) {
// Move bill file
moveOps.push(
fs
.move(
path.join(FolderPaths.Jobs, from_jobid, FolderPaths.BillsSubDir, file),
path.join(FolderPaths.Jobs, jobid, FolderPaths.BillsSubDir, file),
{ overwrite: true }
)
.then(() => logger.debug(`[MoveWorker] Moved bill file: ${file}`))
.catch((err) => logger.warn(`[MoveWorker] Failed to move bill file ${file}:`, err))
);
// Move bill thumbnails
const baseThumb = file.replace(/\.[^/.]+$/, "");
for (const ext of [".jpg", ".png"]) {
moveOps.push(
fs
.move(
path.join(
FolderPaths.Jobs,
from_jobid,
FolderPaths.BillsSubDir,
FolderPaths.ThumbsSubDir,
`${baseThumb}${ext}`
),
path.join(
FolderPaths.Jobs,
jobid,
FolderPaths.BillsSubDir,
FolderPaths.ThumbsSubDir,
`${baseThumb}${ext}`
),
{ overwrite: true }
)
.then(() => logger.debug(`[MoveWorker] Moved bill thumbnail: ${baseThumb}${ext}`))
.catch(() => {}) // Thumbnails might not exist
);
}
// Move bill ConvertedOriginal file if it exists
const billBaseFileName = file.replace(/\.[^/.]+$/, "");
const sourceBillConvertedDir = path.join(FolderPaths.Jobs, from_jobid, FolderPaths.BillsSubDir, FolderPaths.ConvertedOriginalSubDir);
const targetBillConvertedDir = path.join(FolderPaths.Jobs, jobid, FolderPaths.BillsSubDir, FolderPaths.ConvertedOriginalSubDir);
moveOps.push(
(async () => {
try {
if (await fs.pathExists(sourceBillConvertedDir)) {
const convertedOriginalFiles = await fs.readdir(sourceBillConvertedDir);
for (const convertedFile of convertedOriginalFiles) {
const convertedFileBaseName = path.basename(convertedFile, path.extname(convertedFile));
if (convertedFileBaseName === billBaseFileName) {
await fs.ensureDir(targetBillConvertedDir);
await fs.move(
path.join(sourceBillConvertedDir, convertedFile),
path.join(targetBillConvertedDir, convertedFile),
{ overwrite: true }
);
logger.debug(`[MoveWorker] Moved bill ConvertedOriginal: ${convertedFile}`);
}
}
}
} catch (error) {
logger.warn(`[MoveWorker] Failed to move bill ConvertedOriginal for ${file}:`, error);
}
})()
);
}
processedFiles++;
if (job) {
const progress = 15 + Math.round((processedFiles / totalFiles) * 70);
await job.updateProgress(progress);
}
}
if (job) await job.updateProgress(90);
const results = await Promise.allSettled(moveOps);
const failed = results.filter(r => r.status === 'rejected').length;
const moved = results.filter(r => r.status === 'fulfilled').length;
logger.debug(`[MoveWorker] Move operation completed: ${moved} successful, ${failed} failed`);
return { moved, failed };
} catch (error) {
logger.error("[MoveWorker] Error in processMoveOperation:", error);
throw error;
}
}
export async function JobsMoveMedia(req: Request, res: Response) {
const jobid: string = (req.body.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 || [];
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. " });
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. " });
if (!files.length) {
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) => {
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.replace(/\.[^/.]+$/, ".png")),
path.join(FolderPaths.Jobs, jobid, FolderPaths.ThumbsSubDir, file.replace(/\.[^/.]+$/, ".png"))
)
);
}
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.replace(/\.[^/.]+$/, ".png")
),
path.join(
FolderPaths.Jobs,
jobid,
FolderPaths.BillsSubDir,
FolderPaths.ThumbsSubDir,
file.replace(/\.[^/.]+$/, ".png")
)
)
);
}
});
//Use AllSettled as it allows for individual moves to fail.
//e.g. if the thumbnail does not exist.
await Promise.allSettled(movingQueue);
// For small operations (1-3 files), process synchronously for immediate feedback
if (files.length <= 3) {
logger.debug("Processing small move operation synchronously");
await processMoveOperation(jobid, from_jobid, files);
JobsListMedia(req, res);
return;
}
// For larger operations, use BullMQ but still return updated file list
logger.debug(`[JobsMoveMedia] Queuing move operation for ${files.length} files`);
const job = await moveQueue.add("moveMedia", { jobid, from_jobid, files });
// Return the updated file list immediately (optimistic update)
JobsListMedia(req, res);
// Process in background - if it fails, files will be back on next refresh
job.waitUntilFinished(moveQueueEvents)
.then(() => {
logger.debug(`[JobsMoveMedia] Background move completed for job ${job.id}`);
})
.catch((error) => {
logger.error(`[JobsMoveMedia] Background move failed for job ${job.id}:`, error);
});
} catch (err) {
logger.error("Error moving job media", {
from_jobid,
@@ -98,6 +308,38 @@ export async function JobsMoveMedia(req: Request, res: Response) {
files,
err
});
res.status(500).send(err);
res.status(500).json({ error: "Failed to queue move operation", details: err });
}
}
/**
* Get the status of a move operation job
*/
export async function JobsMoveStatus(req: Request, res: Response) {
const { jobId } = req.params;
try {
const job = await Job.fromId(moveQueue, jobId);
if (!job) {
res.status(404).json({ error: "Job not found" });
return;
}
const state = await job.getState();
const progress = job.progress;
res.json({
jobId,
state,
progress,
data: job.data,
finishedOn: job.finishedOn,
processedOn: job.processedOn,
failedReason: job.failedReason
});
} catch (error) {
logger.error("Error getting move job status:", error);
res.status(500).json({ error: "Failed to get job status" });
}
}

View File

@@ -1,3 +1,5 @@
import { Job, Queue, QueueEvents, Worker } from "bullmq";
import dotenv from "dotenv";
import { Request, Response } from "express";
import fs from "fs-extra";
import multer from "multer";
@@ -5,10 +7,151 @@ import path from "path";
import { logger } from "../server.js";
import GenerateThumbnail from "../util/generateThumbnail.js";
import generateUniqueFilename from "../util/generateUniqueFilename.js";
import { ConvertHeicFiles } from "../util/heicConverter.js";
import { convertHeicFiles } from "../util/heicConverter.js";
import { PathToRoFolder } from "../util/pathGenerators.js";
import { JobsListMedia } from "./jobsListMedia.js";
dotenv.config({
path: path.resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`)
});
const UPLOAD_QUEUE_NAME = "uploadProcessingQueue";
const connectionOpts = {
host: "localhost",
port: 6379,
maxRetriesPerRequest: 3,
enableReadyCheck: true,
reconnectOnError: (err: Error) => err.message.includes("READONLY")
};
const uploadProcessingQueue = new Queue(UPLOAD_QUEUE_NAME, {
connection: connectionOpts,
defaultJobOptions: {
removeOnComplete: true,
removeOnFail: true,
attempts: 3,
backoff: { type: "exponential", delay: 1000 }
}
});
const uploadQueueEvents = new QueueEvents(UPLOAD_QUEUE_NAME, {
connection: connectionOpts
});
// Upload processing worker
const uploadWorker = new Worker(
UPLOAD_QUEUE_NAME,
async (
job: Job<{
files: Express.Multer.File[];
jobid: string;
skipThumbnails: boolean;
}>
) => {
const { files, jobid, skipThumbnails } = job.data;
try {
logger.debug(`Processing upload for job ${jobid} with ${files.length} files, skipThumbnails: ${skipThumbnails}`);
await job.updateProgress(10);
// Convert HEIC files if any
await convertHeicFiles(files);
await job.updateProgress(40);
if (!skipThumbnails) {
// Log file states after conversion for debugging
logger.debug("File states after HEIC conversion:", files.map(f => ({
filename: f.filename,
mimetype: f.mimetype,
path: f.path
})));
// Check if converted files actually exist on disk
for (const file of files) {
const exists = await fs.pathExists(file.path);
logger.debug(`File existence check: ${file.filename} at ${file.path} - exists: ${exists}`);
}
// After HEIC conversion, all files should be ready for thumbnail generation
// Only exclude files that are still HEIC (which shouldn't happen after conversion)
const filesToThumbnail = [];
for (const file of files) {
const isStillHeic =
file.mimetype === "image/heic" ||
file.mimetype === "image/heif" ||
/\.heic$/i.test(file.filename) ||
/\.heif$/i.test(file.filename);
logger.debug(`File ${file.filename} - mimetype: ${file.mimetype}, isStillHeic: ${isStillHeic}, including: ${!isStillHeic}`);
if (!isStillHeic) {
filesToThumbnail.push(file);
}
}
logger.debug(
`Generating thumbnails for ${filesToThumbnail.length} files (excluding HEIC)`,
filesToThumbnail.map((f) => f.filename)
);
// Generate thumbnails
logger.debug(`About to generate thumbnails for ${filesToThumbnail.length} files`);
const thumbnailPromises = filesToThumbnail.map((file, index) => {
logger.debug(`Starting thumbnail generation for file ${index + 1}/${filesToThumbnail.length}: ${file.filename} at ${file.path}`);
return GenerateThumbnail(file.path);
});
const thumbnailResults = await Promise.all(thumbnailPromises);
logger.debug(`Thumbnail generation completed. Results:`, thumbnailResults);
await job.updateProgress(90);
}
logger.debug(`Upload processing completed for job ${jobid}`);
await job.updateProgress(100);
return { success: true, filesProcessed: files.length };
} catch (error) {
logger.error(`Error processing upload for job ${jobid}:`, error);
throw error;
}
},
{
connection: connectionOpts,
concurrency: 2 // Allow 2 upload processing jobs to run concurrently
}
);
// Event listeners for upload queue and worker
uploadProcessingQueue.on("waiting", (job) => {
logger.debug(`[UploadQueue] Job waiting: ${job.data.jobid}`);
});
uploadProcessingQueue.on("error", (error) => {
logger.error(`[UploadQueue] Queue error:`, error);
});
uploadWorker.on("ready", () => {
logger.debug("[UploadWorker] Worker ready");
});
uploadWorker.on("active", (job, prev) => {
logger.debug(`[UploadWorker] Job ${job.id} active (previous: ${prev})`);
});
uploadWorker.on("completed", async (job) => {
logger.debug(`[UploadWorker] Job ${job.id} completed`);
await job.remove();
logger.debug(`[UploadWorker] Job ${job.id} removed from Redis`);
});
uploadWorker.on("failed", (jobId, reason) => {
logger.error(`[UploadWorker] Job ${jobId} failed: ${reason}`);
});
uploadWorker.on("error", (error) => {
logger.error(`[UploadWorker] Worker error:`, error);
});
uploadWorker.on("stalled", (job) => {
logger.error(`[UploadWorker] Worker stalled: ${job}`);
});
uploadWorker.on("ioredis:close", () => {
logger.error("[UploadWorker] Redis connection closed");
});
export const JobMediaUploadMulter = multer({
storage: multer.diskStorage({
destination: function (req, file, cb) {
@@ -30,37 +173,46 @@ export async function jobsUploadMedia(req: Request, res: Response) {
const jobid: string = (req.body.jobid || "").trim();
try {
if (!req.files || (req.files as Express.Multer.File[]).length === 0) {
const files = req.files as Express.Multer.File[] | undefined;
if (!files || files.length === 0) {
logger.warning("Upload contained no files.");
res.status(400).send({
status: false,
message: "No file uploaded"
});
} else {
//If we want to skip waiting for everything, just send it back that we're good.
if (req.body.skip_thumbnail) {
req.headers.skipReponse === "true";
res.sendStatus(200);
}
//Check if there's a heic in the file set. If so, modify the file set.
await ConvertHeicFiles(req.files as Express.Multer.File[]);
logger.debug(
"Creating thumbnails for newly uploaded media",
(req.files as Express.Multer.File[]).map((f) => f.filename)
);
const thumbnailGenerationQueue: Promise<string>[] = [];
//for each file.path, generate the thumbnail.
(req.files as Express.Multer.File[]).forEach((file) => {
thumbnailGenerationQueue.push(GenerateThumbnail(file.path));
});
await Promise.all(thumbnailGenerationQueue);
JobsListMedia(req, res);
return;
}
// Check if client wants to skip waiting for response but still do processing
if (req.body.skip_thumbnail) {
logger.debug("Client requested skip_thumbnail - responding immediately but processing in background");
// Set header to indicate we're skipping the response wait
req.headers.skipResponse = "true";
res.sendStatus(200);
// Continue with background processing (conversion + thumbnails)
const job = await uploadProcessingQueue.add("processUpload", {
files,
jobid,
skipThumbnails: false // Always do thumbnails, just don't wait for completion
});
return;
}
// Add upload processing job to queue and wait for completion
logger.debug(`Adding upload processing job for ${files.length} files with thumbnails enabled`);
const job = await uploadProcessingQueue.add("processUpload", {
files,
jobid,
skipThumbnails: false
});
// Wait for the job to complete
await job.waitUntilFinished(uploadQueueEvents);
logger.debug(`Upload processing job completed for job ${jobid}`);
// Return the updated media list
JobsListMedia(req, res);
} catch (error) {
logger.error("Error uploading job media.", {
jobid,