add support to convert heic using imageMagick on linux

This commit is contained in:
Abhinav 2022-12-03 12:11:33 +05:30
parent f181865e64
commit 750bd46315
4 changed files with 23 additions and 19 deletions

View file

@ -1,10 +1,6 @@
import { ipcRenderer } from 'electron/renderer'; import { ipcRenderer } from 'electron/renderer';
import { isPlatform } from '../utils/preload';
export async function convertHEIC(fileData: Uint8Array): Promise<Uint8Array> { export async function convertHEIC(fileData: Uint8Array): Promise<Uint8Array> {
if (!isPlatform('mac')) {
throw Error('native heic conversion only supported on mac');
}
const convertedFileData = await ipcRenderer.invoke( const convertedFileData = await ipcRenderer.invoke(
'convert-heic', 'convert-heic',
fileData fileData

View file

@ -0,0 +1,2 @@
declare const imageMagickPath: string | null;
export default imageMagickPath;

View file

@ -5,6 +5,8 @@ import { rmSync } from 'fs';
import { readFile, writeFile } from 'promise-fs'; import { readFile, writeFile } from 'promise-fs';
import { generateTempFilePath } from '../utils/temp'; import { generateTempFilePath } from '../utils/temp';
import { logErrorSentry } from './sentry'; import { logErrorSentry } from './sentry';
import { isPlatform } from '../utils/main';
import pathToImageMagick from '../pkg/image-magick-static';
const asyncExec = util.promisify(exec); const asyncExec = util.promisify(exec);
@ -19,9 +21,8 @@ export async function convertHEIC(
await writeFile(tempInputFilePath, heicFileData); await writeFile(tempInputFilePath, heicFileData);
await asyncExec( await runConvertCommand(tempInputFilePath, tempOutputFilePath);
`sips -s format jpeg ${tempInputFilePath} --out ${tempOutputFilePath}`
);
const convertedFileData = new Uint8Array( const convertedFileData = new Uint8Array(
await readFile(tempOutputFilePath) 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`);
}
}

View file

@ -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;
}
}