Initial Queue Changes.
This commit is contained in:
@@ -1,13 +1,19 @@
|
||||
import { Job, Queue, QueueEvents, Worker } from "bullmq";
|
||||
import dotenv from "dotenv";
|
||||
import { fileTypeFromFile } from "file-type";
|
||||
import { FileTypeResult } from "file-type/core";
|
||||
import fs from "fs-extra";
|
||||
import gm from "gm";
|
||||
import path from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
import { logger } from "../server.js";
|
||||
import { generateUniqueHeicFilename } from "./generateUniqueFilename.js";
|
||||
import { FolderPaths } from "./serverInit.js";
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
const HeicQueue = new Queue("HEIC Queue", { connection: { host: "localhost", port: 6379 } });
|
||||
|
||||
dotenv.config({
|
||||
path: path.resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`)
|
||||
});
|
||||
@@ -16,19 +22,22 @@ const imageMagick = gm.subClass({ imageMagick: true });
|
||||
|
||||
export async function ConvertHeicFiles(files: Express.Multer.File[]) {
|
||||
const validFiles = await filterValidHeicFiles(files);
|
||||
|
||||
await Promise.all(
|
||||
validFiles.map(async (file) => {
|
||||
const convertedFileName = generateUniqueHeicFilename(file);
|
||||
try {
|
||||
await ConvertToJpeg(file.path, `${file.destination}/${convertedFileName}`);
|
||||
logger.log("debug", `Converted ${file.filename} image to JPEG from HEIC.`);
|
||||
await handleOriginalFile(file, convertedFileName);
|
||||
file.filename = convertedFileName;
|
||||
file.mimetype = "image/jpeg";
|
||||
file.path = `${file.destination}/${convertedFileName}`;
|
||||
} catch (error) {
|
||||
logger.log("error", `Error converting ${file.filename} image to JPEG from HEIC. ${JSON.stringify(error)}`);
|
||||
}
|
||||
await HeicQueue.add(convertedFileName, { convertedFileName, file });
|
||||
|
||||
// try {
|
||||
// await ConvertToJpeg(file.path, `${file.destination}/${convertedFileName}`);
|
||||
// logger.log("debug", `Converted ${file.filename} image to JPEG from HEIC.`);
|
||||
// await handleOriginalFile(file, convertedFileName);
|
||||
// file.filename = convertedFileName;
|
||||
// file.mimetype = "image/jpeg";
|
||||
// file.path = `${file.destination}/${convertedFileName}`;
|
||||
// } catch (error) {
|
||||
// logger.log("error", `Error converting ${file.filename} image to JPEG from HEIC. ${JSON.stringify(error)}`);
|
||||
// }
|
||||
})
|
||||
);
|
||||
}
|
||||
@@ -65,3 +74,41 @@ async function ConvertToJpeg(file: string, newPath: string) {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
//Previos implementation using sandboxing. Cannot set up because the imports try to launch the server again.
|
||||
// const processorUrl = pathToFileURL(__dirname + "/heicQueueProcessor.ts");
|
||||
// const HeicWorker = new Worker("HEIC Queue", processorUrl, {
|
||||
// connection: { host: "localhost", port: 6379 }
|
||||
// });
|
||||
const HeicWorker = new Worker(
|
||||
"HEIC Queue",
|
||||
async (job: Job) => {
|
||||
const { file, convertedFileName } = job.data;
|
||||
try {
|
||||
await ConvertToJpeg(file.path, `${file.destination}/${convertedFileName}`);
|
||||
logger.log("debug", `Converted ${file.filename} image to JPEG from HEIC.`);
|
||||
await handleOriginalFile(file, convertedFileName);
|
||||
file.filename = convertedFileName;
|
||||
file.mimetype = "image/jpeg";
|
||||
file.path = `${file.destination}/${convertedFileName}`;
|
||||
} catch (error) {
|
||||
logger.log("error", `Error converting ${file.filename} image to JPEG from HEIC. ${JSON.stringify(error)}`);
|
||||
}
|
||||
},
|
||||
{
|
||||
connection: { host: "localhost", port: 6379 }
|
||||
}
|
||||
);
|
||||
|
||||
HeicWorker.on("ready", () => {
|
||||
console.log(`Worker Ready`);
|
||||
});
|
||||
HeicWorker.on("active", (job, prev) => {
|
||||
console.log(`Job ${job} is now active; previous status was ${prev}`);
|
||||
});
|
||||
HeicWorker.on("completed", (jobId, returnvalue) => {
|
||||
console.log(`${jobId} has completed and returned ${returnvalue}`);
|
||||
});
|
||||
HeicWorker.on("failed", (jobId, failedReason) => {
|
||||
console.log(`${jobId} has failed with reason ${failedReason}`);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user