( fileNames: string[], userUploadsDir: string, userOutputDir: string, convertTo: string, converterName: string, jobId: Cookie<string | undefined>, )
| 149 | } |
| 150 | |
| 151 | export async function handleConvert( |
| 152 | fileNames: string[], |
| 153 | userUploadsDir: string, |
| 154 | userOutputDir: string, |
| 155 | convertTo: string, |
| 156 | converterName: string, |
| 157 | jobId: Cookie<string | undefined>, |
| 158 | ) { |
| 159 | const query = db.query( |
| 160 | "INSERT INTO file_names (job_id, file_name, output_file_name, status) VALUES (?1, ?2, ?3, ?4)", |
| 161 | ); |
| 162 | |
| 163 | for (const chunk of chunks(fileNames, MAX_CONVERT_PROCESS)) { |
| 164 | const toProcess: Promise<string>[] = []; |
| 165 | for (const fileName of chunk) { |
| 166 | const filePath = `${userUploadsDir}${fileName}`; |
| 167 | const fileTypeOrig = fileName.includes(".") ? (fileName.split(".").pop() ?? "") : ""; |
| 168 | const fileType = normalizeFiletype(fileTypeOrig); |
| 169 | const newFileExt = normalizeOutputFiletype(convertTo); |
| 170 | let newFileName: string; |
| 171 | if (fileTypeOrig === "") { |
| 172 | newFileName = `${fileName}.${newFileExt}`; |
| 173 | } else { |
| 174 | newFileName = fileName.replace( |
| 175 | new RegExp(`${fileTypeOrig}(?!.*${fileTypeOrig})`), |
| 176 | newFileExt, |
| 177 | ); |
| 178 | } |
| 179 | const targetPath = `${userOutputDir}${newFileName}`; |
| 180 | toProcess.push( |
| 181 | new Promise((resolve, reject) => { |
| 182 | mainConverter(filePath, fileType, convertTo, targetPath, {}, converterName) |
| 183 | .then((r) => { |
| 184 | if (jobId.value) { |
| 185 | query.run(jobId.value, fileName, newFileName, r); |
| 186 | } |
| 187 | resolve(r); |
| 188 | }) |
| 189 | .catch((c) => reject(c)); |
| 190 | }), |
| 191 | ); |
| 192 | } |
| 193 | await Promise.all(toProcess); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | async function mainConverter( |
| 198 | inputFilePath: string, |
no test coverage detected