diff --git a/bun.lockb b/bun.lockb index bc04852..5f01d43 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 2f0b233..e59dc23 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/converters/main.ts b/src/converters/main.ts index 345649e..3d3ea7f 100644 --- a/src/converters/main.ts +++ b/src/converters/main.ts @@ -64,18 +64,19 @@ export async function mainConverter( targetPath: string, // biome-ignore lint/suspicious/noExplicitAny: options?: any, - converter?: string, + converterName?: string, ) { const fileType = normalizeFiletype(fileTypeOriginal); // biome-ignore lint/suspicious/noExplicitAny: 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: 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]; + } } } diff --git a/src/index.tsx b/src/index.tsx index baa3343..31c6768 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -357,9 +357,14 @@ const app = new Elysia() - {getAllTargets().map((target) => ( + {Object.entries(getAllTargets()).map(([converter, targets]) => ( // biome-ignore lint/correctness/useJsxKeyInIterable: - + + {targets.map((target) => ( + // biome-ignore lint/correctness/useJsxKeyInIterable: + + ))} + ))} @@ -379,9 +384,14 @@ const app = new Elysia() - {getPossibleConversions(body.fileType).map((target) => ( + {Object.entries(getPossibleConversions(body.fileType)).map(([converter, targets]) => ( // biome-ignore lint/correctness/useJsxKeyInIterable: - + + {targets.map((target) => ( + // biome-ignore lint/correctness/useJsxKeyInIterable: + + ))} + ))} ); @@ -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}`); }, {