fix convertHEIC logic
This commit is contained in:
parent
a838ca698b
commit
7da745c465
|
@ -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<Uint8Array> {
|
||||
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}`
|
||||
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(tempInputOutputName)
|
||||
await readFile(tempOutputFilePath)
|
||||
);
|
||||
return convertedFileData;
|
||||
} catch (e) {
|
||||
logErrorSentry(e, 'failed to convert heic');
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
function generateRandomName(length: number) {
|
||||
|
|
Loading…
Reference in a new issue