Skip to content

Commit 0f5ef2f

Browse files
author
Maxime
committed
added setting number of concurrent convert processes
1 parent 2ce3fee commit 0f5ef2f

File tree

3 files changed

+66
-32
lines changed

3 files changed

+66
-32
lines changed

src/converters/main.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { normalizeFiletype } from "../helpers/normalizeFiletype";
1+
import db from "../db/db";
2+
import { MAX_CONVERT_PROCESS } from "../helpers/env";
3+
import { normalizeFiletype, normalizeOutputFiletype } from "../helpers/normalizeFiletype";
24
import { convert as convertassimp, properties as propertiesassimp } from "./assimp";
35
import { convert as convertCalibre, properties as propertiesCalibre } from "./calibre";
46
import { convert as convertDvisvgm, properties as propertiesDvisvgm } from "./dvisvgm";
@@ -105,6 +107,64 @@ const properties: Record<
105107
},
106108
};
107109

110+
function chunks<T>(arr: T[], size: number): T[][] {
111+
if(size <= 0){
112+
return [arr]
113+
}
114+
return Array.from({ length: Math.ceil(arr.length / size) }, (_: T, i: number) =>
115+
arr.slice(i * size, i * size + size)
116+
);
117+
}
118+
119+
export async function handleConvert(
120+
fileNames: string[],
121+
userUploadsDir: string,
122+
userOutputDir: string,
123+
convertTo: string,
124+
converterName: string,
125+
jobId: any
126+
) {
127+
128+
const query = db.query(
129+
"INSERT INTO file_names (job_id, file_name, output_file_name, status) VALUES (?1, ?2, ?3, ?4)",
130+
);
131+
132+
133+
for (const chunk of chunks(fileNames, MAX_CONVERT_PROCESS)) {
134+
for(const fileName of chunk) {
135+
const toProcess: Promise<string>[] = [];
136+
const filePath = `${userUploadsDir}${fileName}`;
137+
const fileTypeOrig = fileName.split(".").pop() ?? "";
138+
const fileType = normalizeFiletype(fileTypeOrig);
139+
const newFileExt = normalizeOutputFiletype(convertTo);
140+
const newFileName = fileName.replace(
141+
new RegExp(`${fileTypeOrig}(?!.*${fileTypeOrig})`),
142+
newFileExt,
143+
);
144+
const targetPath = `${userOutputDir}${newFileName}`;
145+
toProcess.push(
146+
new Promise((resolve, reject) => {
147+
mainConverter(
148+
filePath,
149+
fileType,
150+
convertTo,
151+
targetPath,
152+
{},
153+
converterName,
154+
).then(r => {
155+
if (jobId.value) {
156+
query.run(jobId.value, fileName, newFileName, r);
157+
}
158+
resolve(r);
159+
}).catch(c => reject(c));
160+
})
161+
);
162+
163+
await Promise.all(toProcess);
164+
}
165+
}
166+
}
167+
108168
export async function mainConverter(
109169
inputFilePath: string,
110170
fileTypeOriginal: string,

src/helpers/env.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ export const HIDE_HISTORY = process.env.HIDE_HISTORY?.toLowerCase() === "true" |
1515
export const WEBROOT = process.env.WEBROOT ?? "";
1616

1717
export const LANGUAGE = process.env.LANGUAGE?.toLowerCase() || "en";
18+
19+
export const MAX_CONVERT_PROCESS = process.env.MAX_CONVERT_PROCESS && Number(process.env.MAX_CONVERT_PROCESS) > 0 ? Number(process.env.MAX_CONVERT_PROCESS) : 0

src/pages/convert.tsx

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { mkdir } from "node:fs/promises";
22
import { Elysia, t } from "elysia";
33
import sanitize from "sanitize-filename";
44
import { outputDir, uploadsDir } from "..";
5-
import { mainConverter } from "../converters/main";
5+
import { handleConvert } from "../converters/main";
66
import db from "../db/db";
77
import { Jobs } from "../db/types";
88
import { WEBROOT } from "../helpers/env";
9-
import { normalizeFiletype, normalizeOutputFiletype } from "../helpers/normalizeFiletype";
9+
import { normalizeFiletype } from "../helpers/normalizeFiletype";
1010
import { userService } from "./user";
1111

1212
export const convert = new Elysia().use(userService).post(
@@ -61,36 +61,8 @@ export const convert = new Elysia().use(userService).post(
6161
jobId.value,
6262
);
6363

64-
const query = db.query(
65-
"INSERT INTO file_names (job_id, file_name, output_file_name, status) VALUES (?1, ?2, ?3, ?4)",
66-
);
67-
6864
// Start the conversion process in the background
69-
Promise.all(
70-
fileNames.map(async (fileName) => {
71-
const filePath = `${userUploadsDir}${fileName}`;
72-
const fileTypeOrig = fileName.split(".").pop() ?? "";
73-
const fileType = normalizeFiletype(fileTypeOrig);
74-
const newFileExt = normalizeOutputFiletype(convertTo);
75-
const newFileName = fileName.replace(
76-
new RegExp(`${fileTypeOrig}(?!.*${fileTypeOrig})`),
77-
newFileExt,
78-
);
79-
const targetPath = `${userOutputDir}${newFileName}`;
80-
81-
const result = await mainConverter(
82-
filePath,
83-
fileType,
84-
convertTo,
85-
targetPath,
86-
{},
87-
converterName,
88-
);
89-
if (jobId.value) {
90-
query.run(jobId.value, fileName, newFileName, result);
91-
}
92-
}),
93-
)
65+
handleConvert(fileNames, userUploadsDir, userOutputDir, convertTo, converterName, jobId)
9466
.then(() => {
9567
// All conversions are done, update the job status to 'completed'
9668
if (jobId.value) {

0 commit comments

Comments
 (0)