diff --git a/src/services/heicConversion.ts b/src/services/heicConversion.ts index b9750532f..61739bc4b 100644 --- a/src/services/heicConversion.ts +++ b/src/services/heicConversion.ts @@ -1,22 +1,54 @@ -import { exec } from 'child_process'; +import { exec, ExecException } from 'child_process'; import { app } from 'electron'; +import { existsSync } from 'fs'; import path from 'path'; -import { readFile, writeFile } from 'promise-fs'; +import { mkdir, readFile, writeFile } from 'promise-fs'; +import { logErrorSentry } from './sentry'; export async function convertHEIC( heicFileData: Uint8Array ): Promise { - const tempInputFileName = generateRandomName(10) + '.heic'; - const tempInputOutputName = generateRandomName(10) + '.jpeg'; - const tempDir = app.getPath('temp'); - writeFile(path.join(tempDir, tempInputFileName), heicFileData); - exec( - `sips -s format jpeg ${tempInputFileName} --out ${tempInputOutputName}` - ); - const convertedFileData = new Uint8Array( - await readFile(tempInputOutputName) - ); - return convertedFileData; + try { + const tempDir = path.join(app.getPath('temp'), 'ente'); + if (!existsSync(tempDir)) { + await mkdir(tempDir); + } + const tempInputFilePath = path.join( + tempDir, + generateRandomName(10) + '.heic' + ); + const tempOutputFilePath = path.join( + tempDir, + generateRandomName(10) + '.jpeg' + ); + writeFile(tempInputFilePath, heicFileData); + + await new Promise((resolve, reject) => { + exec( + `sips -s format jpeg ${tempInputFilePath} --out ${tempOutputFilePath}`, + ( + error: ExecException | null, + stdout: string, + stderr: string + ) => { + if (error) { + reject(error); + } else if (stderr) { + reject(stderr); + } else { + resolve(stdout); + } + } + ); + }); + const convertedFileData = new Uint8Array( + await readFile(tempOutputFilePath) + ); + return convertedFileData; + } catch (e) { + logErrorSentry(e, 'failed to convert heic'); + throw e; + } } function generateRandomName(length: number) {