refactor code and create temp util

This commit is contained in:
Abhinav 2022-11-14 11:06:25 +05:30
parent 82021b3b7a
commit f81b852fb4
3 changed files with 43 additions and 26 deletions

View file

@ -1,9 +1,8 @@
import { exec, ExecException } from 'child_process';
import { app } from 'electron';
import { existsSync, rmSync } from 'fs';
import { rmSync } from 'fs';
import path from 'path';
import { mkdir, readFile, writeFile } from 'promise-fs';
import { generateRandomName } from '../utils/common';
import { readFile, writeFile } from 'promise-fs';
import { generateTempName, getTempDirPath } from '../utils/temp';
import { logErrorSentry } from './sentry';
export async function convertHEIC(
@ -12,14 +11,11 @@ export async function convertHEIC(
let tempInputFilePath: string;
let tempOutputFilePath: string;
try {
const tempDir = path.join(app.getPath('temp'), 'ente');
if (!existsSync(tempDir)) {
await mkdir(tempDir);
}
const tempName = generateRandomName(10);
const tempDirPath = await getTempDirPath();
const tempName = generateTempName(10);
tempInputFilePath = path.join(tempDir, tempName + '.heic');
tempOutputFilePath = path.join(tempDir, tempName + '.jpeg');
tempInputFilePath = path.join(tempDirPath, tempName + '.heic');
tempOutputFilePath = path.join(tempDirPath, tempName + '.jpeg');
await writeFile(tempInputFilePath, heicFileData);

View file

@ -1,17 +1,2 @@
import { app } from 'electron';
export const isDev = !app.isPackaged;
const characters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
export function generateRandomName(length: number) {
let result = '';
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(
Math.floor(Math.random() * charactersLength)
);
}
return result;
}

36
src/utils/temp.ts Normal file
View file

@ -0,0 +1,36 @@
import { app } from 'electron';
import { emptyDir } from 'fs-extra';
import path from 'path';
import { existsSync, mkdir } from 'promise-fs';
const ENTE_TEMP_DIRECTORY = 'ente';
const ALPHABETS =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
export async function getTempDirPath() {
const tempDirPath = path.join(app.getPath('temp'), ENTE_TEMP_DIRECTORY);
if (!existsSync(tempDirPath)) {
await mkdir(tempDirPath);
}
return tempDirPath;
}
export async function clearEnteTempFolder() {
const tempDirPath = await this.getTempDirPath();
if (existsSync(tempDirPath)) {
await emptyDir(tempDirPath);
}
}
export function generateTempName(length: number) {
let result = '';
const charactersLength = ALPHABETS.length;
for (let i = 0; i < length; i++) {
result += ALPHABETS.charAt(
Math.floor(Math.random() * charactersLength)
);
}
return result;
}