mirror of
https://github.com/C4illin/ConvertX.git
synced 2026-06-30 07:55:48 +00:00
run jobs in parallell
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
"@elysiajs/html": "^1.0.2",
|
||||
"@elysiajs/jwt": "^1.0.2",
|
||||
"@elysiajs/static": "^1.0.3",
|
||||
"elysia": "latest",
|
||||
"elysia": "^1.0.21",
|
||||
"sharp": "^0.33.4"
|
||||
},
|
||||
"module": "src/index.tsx",
|
||||
|
||||
@@ -64,18 +64,19 @@ export async function mainConverter(
|
||||
targetPath: string,
|
||||
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
||||
options?: any,
|
||||
converter?: string,
|
||||
converterName?: string,
|
||||
) {
|
||||
const fileType = normalizeFiletype(fileTypeOriginal);
|
||||
|
||||
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
||||
let converterFunc: any;
|
||||
let converterName = converter;
|
||||
// let converterName = converterName;
|
||||
|
||||
if (converter) {
|
||||
converterFunc = properties[converter];
|
||||
if (converterName) {
|
||||
converterFunc = properties[converterName]?.converter;
|
||||
} else {
|
||||
// Iterate over each converter in properties
|
||||
// biome-ignore lint/style/noParameterAssign: <explanation>
|
||||
for (converterName in properties) {
|
||||
const converterObj = properties[converterName];
|
||||
|
||||
@@ -83,9 +84,6 @@ export async function mainConverter(
|
||||
break;
|
||||
}
|
||||
|
||||
// if converter properties.from is an object loop thorugh the keys otherwise use the array
|
||||
// for example ffmpeg is an object eg from: {video: ["mp4", "webm"], audio: ["mp3"]}
|
||||
|
||||
for (const key in converterObj.properties.from) {
|
||||
if (
|
||||
converterObj.properties.from[key].includes(fileType) &&
|
||||
@@ -114,17 +112,17 @@ export async function mainConverter(
|
||||
options,
|
||||
);
|
||||
console.log(
|
||||
`Converted ${inputFilePath} from ${fileType} to ${convertTo} successfully using ${converter}.`,
|
||||
`Converted ${inputFilePath} from ${fileType} to ${convertTo} successfully using ${converterName}.`,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`Failed to convert ${inputFilePath} from ${fileType} to ${convertTo} using ${converter}.`,
|
||||
`Failed to convert ${inputFilePath} from ${fileType} to ${convertTo} using ${converterName}.`,
|
||||
error,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const possibleConversions: { [key: string]: string[] } = {};
|
||||
const possibleConversions: { [key: string]: { [key: string]: string[] } } = {};
|
||||
|
||||
for (const converterName in properties) {
|
||||
const converterProperties = properties[converterName]?.properties;
|
||||
@@ -134,12 +132,16 @@ for (const converterName in properties) {
|
||||
}
|
||||
|
||||
for (const key in converterProperties.from) {
|
||||
if (!converterProperties.from[key] || !converterProperties.to[key]) {
|
||||
if (converterProperties.from[key] === undefined) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const extension of converterProperties.from[key]) {
|
||||
possibleConversions[extension] = converterProperties.to[key];
|
||||
for (const extension of converterProperties.from[key] ?? []) {
|
||||
if (!possibleConversions[extension]) {
|
||||
possibleConversions[extension] = {};
|
||||
}
|
||||
possibleConversions[extension][converterName] =
|
||||
converterProperties.to[key] || [];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -153,19 +155,29 @@ for (const converterName in properties) {
|
||||
// JSON.stringify(possibleConversions),
|
||||
// );
|
||||
|
||||
export const getPossibleConversions = (from: string): string[] => {
|
||||
export const getPossibleConversions = (
|
||||
from: string,
|
||||
): { [key: string]: string[] } => {
|
||||
const fromClean = normalizeFiletype(from);
|
||||
|
||||
return possibleConversions[fromClean] || ([] as string[]);
|
||||
return possibleConversions[fromClean] || {};
|
||||
};
|
||||
|
||||
let allTargets: string[] = [];
|
||||
const allTargets: { [key: string]: string[] | undefined } = {};
|
||||
|
||||
for (const converterName in properties) {
|
||||
const converterProperties = properties[converterName].properties;
|
||||
const converterProperties = properties[converterName]?.properties;
|
||||
|
||||
if (!converterProperties) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const key in converterProperties.to) {
|
||||
allTargets = allTargets.concat(converterProperties.to[key]);
|
||||
if (allTargets[converterName]) {
|
||||
allTargets[converterName].push(...converterProperties.to[key]);
|
||||
} else {
|
||||
allTargets[converterName] = converterProperties.to[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -357,9 +357,14 @@ const app = new Elysia()
|
||||
<option selected disabled value="">
|
||||
Convert to
|
||||
</option>
|
||||
{getAllTargets().map((target) => (
|
||||
{Object.entries(getAllTargets()).map(([converter, targets]) => (
|
||||
// biome-ignore lint/correctness/useJsxKeyInIterable: <explanation>
|
||||
<option value={target}>{target}</option>
|
||||
<optgroup label={converter}>
|
||||
{targets.map((target) => (
|
||||
// biome-ignore lint/correctness/useJsxKeyInIterable: <explanation>
|
||||
<option value={`${target},${converter}`}>{target}</option>
|
||||
))}
|
||||
</optgroup>
|
||||
))}
|
||||
</select>
|
||||
</article>
|
||||
@@ -379,9 +384,14 @@ const app = new Elysia()
|
||||
<option selected disabled value="">
|
||||
Convert to
|
||||
</option>
|
||||
{getPossibleConversions(body.fileType).map((target) => (
|
||||
{Object.entries(getPossibleConversions(body.fileType)).map(([converter, targets]) => (
|
||||
// biome-ignore lint/correctness/useJsxKeyInIterable: <explanation>
|
||||
<option value={target}>{target}</option>
|
||||
<optgroup label={converter}>
|
||||
{targets.map((target) => (
|
||||
// biome-ignore lint/correctness/useJsxKeyInIterable: <explanation>
|
||||
<option value={`${target},${converter}`}>{target}</option>
|
||||
))}
|
||||
</optgroup>
|
||||
))}
|
||||
</select>
|
||||
);
|
||||
@@ -503,7 +513,8 @@ const app = new Elysia()
|
||||
);
|
||||
}
|
||||
|
||||
const convertTo = normalizeFiletype(body.convert_to);
|
||||
const convertTo = normalizeFiletype(body.convert_to.split(",")[0] as string);
|
||||
const converterName = body.convert_to.split(",")[1];
|
||||
const fileNames = JSON.parse(body.file_names) as string[];
|
||||
|
||||
if (!Array.isArray(fileNames) || fileNames.length === 0) {
|
||||
@@ -520,17 +531,29 @@ const app = new Elysia()
|
||||
"INSERT INTO file_names (job_id, file_name, output_file_name) VALUES (?, ?, ?)",
|
||||
);
|
||||
|
||||
for (const fileName of fileNames) {
|
||||
// Start the conversion process in the background
|
||||
Promise.all(fileNames.map(async (fileName) => {
|
||||
const filePath = `${userUploadsDir}${fileName}`;
|
||||
const fileTypeOrig = fileName.split(".").pop() as string;
|
||||
const fileType = normalizeFiletype(fileTypeOrig);
|
||||
const newFileName = fileName.replace(fileTypeOrig, convertTo);
|
||||
const targetPath = `${userOutputDir}${newFileName}`;
|
||||
|
||||
await mainConverter(filePath, fileType, convertTo, targetPath);
|
||||
await mainConverter(filePath, fileType, convertTo, targetPath, {}, converterName);
|
||||
query.run(jobId.value, fileName, newFileName);
|
||||
}
|
||||
}))
|
||||
.then(() => {
|
||||
// All conversions are done, update the job status to 'completed'
|
||||
db.run(
|
||||
"UPDATE jobs SET status = 'completed' WHERE id = ?",
|
||||
jobId.value,
|
||||
);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Error in conversion process:', error);
|
||||
});
|
||||
|
||||
// Redirect the client immediately
|
||||
return redirect(`/results/${jobId.value}`);
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user