diff --git a/index.ts b/index.ts index af4f133..00261c1 100644 --- a/index.ts +++ b/index.ts @@ -1,41 +1,13 @@ import amqp from "amqplib"; import { processImage } from "./process-image"; import dotenv from "dotenv"; +import { sleepWithCountdown } from "./util"; dotenv.config(); const queueName = process.env.QUEUE_NAME || "storage.file"; const rabbitUrl = process.env.RABBITMQ_URL || "amqp://localhost"; -function sleepWithCountdown(seconds: number): Promise { - return new Promise((resolve) => { - let remaining = seconds; - - const update = () => { - const msg = `⏳ Waiting ${remaining}s...`; - process.stdout.clearLine(0); // Clear current line - process.stdout.cursorTo(0); // Move cursor to start of line - process.stdout.write(msg); // Write message - }; - - update(); // Initial message - - const interval = setInterval(() => { - remaining--; - - if (remaining > 0) { - update(); - } else { - clearInterval(interval); - process.stdout.clearLine(0); - process.stdout.cursorTo(0); - process.stdout.write(`✅ Resuming now\n`); - resolve(); - } - }, 1000); - }); -} - async function start() { const conn = await amqp.connect(rabbitUrl); const channel = await conn.createChannel(); diff --git a/process-image.ts b/process-image.ts index 6c515e9..6411c19 100644 --- a/process-image.ts +++ b/process-image.ts @@ -1,6 +1,7 @@ import sharp from "sharp"; import { getMinioClient } from "./minio"; import { lookup } from "mime-types"; +import { sleepWithCountdown } from "./util"; export async function processImage( bucket: string, @@ -51,6 +52,8 @@ export async function processImage( } try { + await sleepWithCountdown(5); + // 🖼️ Create thumbnail const thumb = await sharp(buffer).resize(200).toBuffer(); await writeImage(`${filePath}/thumbs/${fileName}`, thumb, mime); diff --git a/util.ts b/util.ts new file mode 100644 index 0000000..95694f2 --- /dev/null +++ b/util.ts @@ -0,0 +1,28 @@ +export function sleepWithCountdown(seconds: number): Promise { + return new Promise((resolve) => { + let remaining = seconds; + + const update = () => { + const msg = `⏳ Waiting ${remaining}s...`; + process.stdout.clearLine(0); // Clear current line + process.stdout.cursorTo(0); // Move cursor to start of line + process.stdout.write(msg); // Write message + }; + + update(); // Initial message + + const interval = setInterval(() => { + remaining--; + + if (remaining > 0) { + update(); + } else { + clearInterval(interval); + process.stdout.clearLine(0); + process.stdout.cursorTo(0); + process.stdout.write(`✅ Resuming now\n`); + resolve(); + } + }, 1000); + }); +}