From a41e5d8c99dfd81766e461e036adecb065257710 Mon Sep 17 00:00:00 2001 From: C4illin Date: Sat, 25 May 2024 18:09:28 +0200 Subject: [PATCH] wrap converters in promises --- src/converters/ffmpeg.ts | 26 +++++++++-------- src/converters/graphicsmagick.ts | 34 +++++++++++++---------- src/converters/{sharp.ts => old.sharp.ts} | 0 src/converters/pandoc.ts | 34 +++++++++++++---------- src/converters/vips.ts | 15 +++++----- src/index.tsx | 4 ++- src/public/results.js | 8 ++---- src/public/script.js | 6 ++-- 8 files changed, 70 insertions(+), 57 deletions(-) rename src/converters/{sharp.ts => old.sharp.ts} (100%) diff --git a/src/converters/ffmpeg.ts b/src/converters/ffmpeg.ts index 874d2ea..65ddd4e 100644 --- a/src/converters/ffmpeg.ts +++ b/src/converters/ffmpeg.ts @@ -772,7 +772,7 @@ export async function convert( targetPath: string, // biome-ignore lint/suspicious/noExplicitAny: options?: any, -) { +): Promise { // let command = "ffmpeg"; // these are containers that can contain multiple formats @@ -807,17 +807,21 @@ export async function convert( const command = `ffmpeg -i "${filePath}" "${targetPath}"`; - return exec(command, (error, stdout, stderr) => { - if (error) { - return error; - } + return new Promise((resolve, reject) => { + exec(command, (error, stdout, stderr) => { + if (error) { + reject(`error: ${error}`); + } - if (stdout) { - console.log(`stdout: ${stdout}`); - } + if (stdout) { + console.log(`stdout: ${stdout}`); + } - if (stderr) { - console.error(`stderr: ${stderr}`); - } + if (stderr) { + console.error(`stderr: ${stderr}`); + } + + resolve("success"); + }); }); } diff --git a/src/converters/graphicsmagick.ts b/src/converters/graphicsmagick.ts index c4f292d..069d003 100644 --- a/src/converters/graphicsmagick.ts +++ b/src/converters/graphicsmagick.ts @@ -315,21 +315,25 @@ export function convert( targetPath: string, // biome-ignore lint/suspicious/noExplicitAny: options?: any, -) { - return exec( - `gm convert "${filePath}" "${targetPath}"`, - (error, stdout, stderr) => { - if (error) { - return error; - } +): Promise { + return new Promise((resolve, reject) => { + exec( + `gm convert "${filePath}" "${targetPath}"`, + (error, stdout, stderr) => { + if (error) { + reject(`error: ${error}`); + } - if (stdout) { - console.log(`stdout: ${stdout}`); - } + if (stdout) { + console.log(`stdout: ${stdout}`); + } - if (stderr) { - console.error(`stderr: ${stderr}`); - } - }, - ); + if (stderr) { + console.error(`stderr: ${stderr}`); + } + + resolve("success"); + }, + ); + }); } diff --git a/src/converters/sharp.ts b/src/converters/old.sharp.ts similarity index 100% rename from src/converters/sharp.ts rename to src/converters/old.sharp.ts diff --git a/src/converters/pandoc.ts b/src/converters/pandoc.ts index e874e32..875e4d9 100644 --- a/src/converters/pandoc.ts +++ b/src/converters/pandoc.ts @@ -126,21 +126,25 @@ export function convert( targetPath: string, // biome-ignore lint/suspicious/noExplicitAny: options?: any, -) { - return exec( - `pandoc "${filePath}" -f ${fileType} -t ${convertTo} -o "${targetPath}"`, - (error, stdout, stderr) => { - if (error) { - return error; - } +): Promise { + return new Promise((resolve, reject) => { + exec( + `pandoc "${filePath}" -f ${fileType} -t ${convertTo} -o "${targetPath}"`, + (error, stdout, stderr) => { + if (error) { + reject(`error: ${error}`); + } - if (stdout) { - console.log(`stdout: ${stdout}`); - } + if (stdout) { + console.log(`stdout: ${stdout}`); + } - if (stderr) { - console.error(`stderr: ${stderr}`); - } - }, - ); + if (stderr) { + console.error(`stderr: ${stderr}`); + } + + resolve("success"); + }, + ); + }); } diff --git a/src/converters/vips.ts b/src/converters/vips.ts index 8708317..d1944df 100644 --- a/src/converters/vips.ts +++ b/src/converters/vips.ts @@ -96,7 +96,7 @@ export function convert( targetPath: string, // biome-ignore lint/suspicious/noExplicitAny: options?: any, -) { +): Promise { // if (fileType === "svg") { // const scale = options.scale || 1; // const metadata = await sharp(filePath).metadata(); @@ -114,11 +114,10 @@ export function convert( // .toFile(targetPath); // } - return exec( - `vips copy ${filePath} ${targetPath}`, - (error, stdout, stderr) => { + return new Promise((resolve, reject) => { + exec(`vips copy ${filePath} ${targetPath}`, (error, stdout, stderr) => { if (error) { - return error; + reject(`error: ${error}`); } if (stdout) { @@ -128,6 +127,8 @@ export function convert( if (stderr) { console.error(`stderr: ${stderr}`); } - }, - ); + + resolve("success"); + }); + }); } diff --git a/src/index.tsx b/src/index.tsx index def2dcc..2285e9d 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -824,7 +824,7 @@ const app = new Elysia() ); }, ) - .get( + .post( "/progress/:jobId", async ({ jwt, set, params, redirect, cookie: { auth, job_id } }) => { if (!auth?.value) { @@ -877,6 +877,7 @@ const app = new Elysia() Converted File Name + Status View Download @@ -886,6 +887,7 @@ const app = new Elysia() // biome-ignore lint/correctness/useJsxKeyInIterable: {file.output_file_name} + {file.status} View diff --git a/src/public/results.js b/src/public/results.js index b7d8549..662e06d 100644 --- a/src/public/results.js +++ b/src/public/results.js @@ -16,12 +16,10 @@ const main = document.querySelector("main"); const progressElem = document.querySelector("progress"); const refreshData = () => { - console.log("Refreshing data..."); - console.log(progressElem.value); - console.log(progressElem.max); - if (progressElem.value !== progressElem.max) { - fetch(`/progress/${jobId}`) + fetch(`/progress/${jobId}`, { + method: "POST", + }) .then((res) => res.text()) .then((html) => { main.innerHTML = html; diff --git a/src/public/script.js b/src/public/script.js index 253a276..0838f65 100644 --- a/src/public/script.js +++ b/src/public/script.js @@ -3,7 +3,7 @@ const fileInput = document.querySelector('input[type="file"]'); const fileNames = []; let fileType; -const selectElem = document.querySelector("select[name='convert_to']"); +const selectContainer = document.querySelector("form > article"); // const convertFromSelect = document.querySelector("select[name='convert_from']"); @@ -46,9 +46,9 @@ fileInput.addEventListener("change", (e) => { "Content-Type": "application/json", }, }) - .then((res) => res.text()) // Convert the response to text + .then((res) => res.text()) .then((html) => { - selectElem.outerHTML = html; // Set the HTML + selectContainer.innerHTML = html; }) .catch((err) => console.log(err)); }