diff --git a/src/api/heicConvert.ts b/src/api/heicConvert.ts index 94797a252..68909b678 100644 --- a/src/api/heicConvert.ts +++ b/src/api/heicConvert.ts @@ -1,10 +1,6 @@ import { ipcRenderer } from 'electron/renderer'; -import { isPlatform } from '../utils/preload'; export async function convertHEIC(fileData: Uint8Array): Promise { - if (!isPlatform('mac')) { - throw Error('native heic conversion only supported on mac'); - } const convertedFileData = await ipcRenderer.invoke( 'convert-heic', fileData diff --git a/src/pkg/image-magick-static/index.d.ts b/src/pkg/image-magick-static/index.d.ts new file mode 100644 index 000000000..3eb2cc7a6 --- /dev/null +++ b/src/pkg/image-magick-static/index.d.ts @@ -0,0 +1,2 @@ +declare const imageMagickPath: string | null; +export default imageMagickPath; diff --git a/src/services/heicConvertor.ts b/src/services/heicConvertor.ts index cb90bbba3..08fdec1d9 100644 --- a/src/services/heicConvertor.ts +++ b/src/services/heicConvertor.ts @@ -5,6 +5,8 @@ import { rmSync } from 'fs'; import { readFile, writeFile } from 'promise-fs'; import { generateTempFilePath } from '../utils/temp'; import { logErrorSentry } from './sentry'; +import { isPlatform } from '../utils/main'; +import pathToImageMagick from '../pkg/image-magick-static'; const asyncExec = util.promisify(exec); @@ -19,9 +21,8 @@ export async function convertHEIC( await writeFile(tempInputFilePath, heicFileData); - await asyncExec( - `sips -s format jpeg ${tempInputFilePath} --out ${tempOutputFilePath}` - ); + await runConvertCommand(tempInputFilePath, tempOutputFilePath); + const convertedFileData = new Uint8Array( await readFile(tempOutputFilePath) ); @@ -42,3 +43,20 @@ export async function convertHEIC( } } } + +async function runConvertCommand( + tempInputFilePath: string, + tempOutputFilePath: string +) { + if (isPlatform('mac')) { + await asyncExec( + `sips -s format jpeg ${tempInputFilePath} --out ${tempOutputFilePath}` + ); + } else if (isPlatform('linux')) { + await asyncExec( + `${pathToImageMagick} ${tempInputFilePath} -quality 100% ${tempOutputFilePath}` + ); + } else { + Error(`${process.platform} native heic convert not supported yet`); + } +} diff --git a/src/utils/preload.ts b/src/utils/preload.ts index c8d8173d8..f29a1d543 100644 --- a/src/utils/preload.ts +++ b/src/utils/preload.ts @@ -14,15 +14,3 @@ export const fixHotReloadNext12 = () => { }) });`); }; - -export function isPlatform(platform: 'mac' | 'windows' | 'linux') { - if (process.platform === 'darwin') { - return platform === 'mac'; - } else if (process.platform === 'win32') { - return platform === 'windows'; - } else if (process.platform === 'linux') { - return platform === 'linux'; - } else { - return false; - } -}