Skip to content

Commit

Permalink
wrap converters in promises
Browse files Browse the repository at this point in the history
  • Loading branch information
C4illin committed May 25, 2024
1 parent 7a9def5 commit a41e5d8
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 57 deletions.
26 changes: 15 additions & 11 deletions src/converters/ffmpeg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ export async function convert(
targetPath: string,
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
options?: any,
) {
): Promise<string> {
// let command = "ffmpeg";

// these are containers that can contain multiple formats
Expand Down Expand Up @@ -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");
});
});
}
34 changes: 19 additions & 15 deletions src/converters/graphicsmagick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,21 +315,25 @@ export function convert(
targetPath: string,
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
options?: any,
) {
return exec(
`gm convert "${filePath}" "${targetPath}"`,
(error, stdout, stderr) => {
if (error) {
return error;
}
): Promise<string> {
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");
},
);
});
}
File renamed without changes.
34 changes: 19 additions & 15 deletions src/converters/pandoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,25 @@ export function convert(
targetPath: string,
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
options?: any,
) {
return exec(
`pandoc "${filePath}" -f ${fileType} -t ${convertTo} -o "${targetPath}"`,
(error, stdout, stderr) => {
if (error) {
return error;
}
): Promise<string> {
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");
},
);
});
}
15 changes: 8 additions & 7 deletions src/converters/vips.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function convert(
targetPath: string,
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
options?: any,
) {
): Promise<string> {
// if (fileType === "svg") {
// const scale = options.scale || 1;
// const metadata = await sharp(filePath).metadata();
Expand All @@ -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) {
Expand All @@ -128,6 +127,8 @@ export function convert(
if (stderr) {
console.error(`stderr: ${stderr}`);
}
},
);

resolve("success");
});
});
}
4 changes: 3 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ const app = new Elysia()
);
},
)
.get(
.post(
"/progress/:jobId",
async ({ jwt, set, params, redirect, cookie: { auth, job_id } }) => {
if (!auth?.value) {
Expand Down Expand Up @@ -877,6 +877,7 @@ const app = new Elysia()
<thead>
<tr>
<th>Converted File Name</th>
<th>Status</th>
<th>View</th>
<th>Download</th>
</tr>
Expand All @@ -886,6 +887,7 @@ const app = new Elysia()
// biome-ignore lint/correctness/useJsxKeyInIterable: <explanation>
<tr>
<td>{file.output_file_name}</td>
<td>{file.status}</td>
<td>
<a href={`/download/${outputPath}${file.output_file_name}`}>
View
Expand Down
8 changes: 3 additions & 5 deletions src/public/results.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/public/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']");

Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit a41e5d8

Please sign in to comment.