Merge branch 'main' into update-actions-node-version
This commit is contained in:
commit
d6d2116d32
|
@ -22,11 +22,11 @@ export const checkExistsAndRename = async (
|
|||
}
|
||||
};
|
||||
|
||||
export const saveStreamToDisk = (
|
||||
export const saveStreamToDisk = async (
|
||||
filePath: string,
|
||||
fileStream: ReadableStream<any>
|
||||
fileStream: ReadableStream<Uint8Array>
|
||||
) => {
|
||||
writeStream(filePath, fileStream);
|
||||
await writeStream(filePath, fileStream);
|
||||
};
|
||||
|
||||
export const saveFileToDisk = async (path: string, fileData: any) => {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { ipcRenderer } from 'electron';
|
||||
import { existsSync } from 'fs';
|
||||
import { writeStream } from '../services/fs';
|
||||
import { logError } from '../services/logging';
|
||||
import { ElectronFile } from '../types';
|
||||
|
||||
|
@ -12,12 +13,12 @@ export async function runFFmpegCmd(
|
|||
let createdTempInputFile = null;
|
||||
try {
|
||||
if (!existsSync(inputFile.path)) {
|
||||
const inputFileData = new Uint8Array(await inputFile.arrayBuffer());
|
||||
inputFilePath = await ipcRenderer.invoke(
|
||||
'write-temp-file',
|
||||
inputFileData,
|
||||
const tempFilePath = await ipcRenderer.invoke(
|
||||
'get-temp-file-path',
|
||||
inputFile.name
|
||||
);
|
||||
await writeStream(tempFilePath, await inputFile.stream());
|
||||
inputFilePath = tempFilePath;
|
||||
createdTempInputFile = true;
|
||||
} else {
|
||||
inputFilePath = inputFile.path;
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
import { ipcRenderer } from 'electron/renderer';
|
||||
|
||||
export async function convertHEIC(fileData: Uint8Array): Promise<Uint8Array> {
|
||||
const convertedFileData = await ipcRenderer.invoke(
|
||||
'convert-heic',
|
||||
fileData
|
||||
);
|
||||
return convertedFileData;
|
||||
}
|
50
src/api/imageProcessor.ts
Normal file
50
src/api/imageProcessor.ts
Normal file
|
@ -0,0 +1,50 @@
|
|||
import { ipcRenderer } from 'electron/renderer';
|
||||
import { existsSync } from 'fs';
|
||||
import { writeStream } from '../services/fs';
|
||||
import { logError } from '../services/logging';
|
||||
import { ElectronFile } from '../types';
|
||||
|
||||
export async function convertHEIC(fileData: Uint8Array): Promise<Uint8Array> {
|
||||
const convertedFileData = await ipcRenderer.invoke(
|
||||
'convert-heic',
|
||||
fileData
|
||||
);
|
||||
return convertedFileData;
|
||||
}
|
||||
|
||||
export async function generateImageThumbnail(
|
||||
inputFile: File | ElectronFile,
|
||||
maxDimension: number,
|
||||
maxSize: number
|
||||
): Promise<Uint8Array> {
|
||||
let inputFilePath = null;
|
||||
let createdTempInputFile = null;
|
||||
try {
|
||||
if (!existsSync(inputFile.path)) {
|
||||
const tempFilePath = await ipcRenderer.invoke(
|
||||
'get-temp-file-path',
|
||||
inputFile.name
|
||||
);
|
||||
await writeStream(tempFilePath, await inputFile.stream());
|
||||
inputFilePath = tempFilePath;
|
||||
createdTempInputFile = true;
|
||||
} else {
|
||||
inputFilePath = inputFile.path;
|
||||
}
|
||||
const thumbnail = await ipcRenderer.invoke(
|
||||
'generate-image-thumbnail',
|
||||
inputFilePath,
|
||||
maxDimension,
|
||||
maxSize
|
||||
);
|
||||
return thumbnail;
|
||||
} finally {
|
||||
if (createdTempInputFile) {
|
||||
try {
|
||||
await ipcRenderer.invoke('remove-temp-file', inputFilePath);
|
||||
} catch (e) {
|
||||
logError(e, 'failed to deleteTempFile');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
const PROD_HOST_URL: string = 'ente://app';
|
||||
const RENDERER_OUTPUT_DIR: string = './ui/out';
|
||||
const LOG_FILENAME = 'ente.log';
|
||||
const MAX_LOG_SIZE = 5 * 1024 * 1024; // 5MB
|
||||
const MAX_LOG_SIZE = 50 * 1024 * 1024; // 50MB
|
||||
|
||||
const FILE_STREAM_CHUNK_SIZE: number = 4 * 1024 * 1024;
|
||||
|
||||
|
|
|
@ -48,7 +48,7 @@ import {
|
|||
} from './api/common';
|
||||
import { fixHotReloadNext12 } from './utils/preload';
|
||||
import { isFolder, getDirFiles } from './api/fs';
|
||||
import { convertHEIC } from './api/heicConvert';
|
||||
import { convertHEIC, generateImageThumbnail } from './api/imageProcessor';
|
||||
import { setupLogging } from './utils/logging';
|
||||
import { setupRendererProcessStatsLogger } from './utils/processStats';
|
||||
import { runFFmpegCmd } from './api/ffmpeg';
|
||||
|
@ -104,4 +104,5 @@ windowObject['ElectronAPIs'] = {
|
|||
getSentryUserID,
|
||||
getAppVersion,
|
||||
runFFmpegCmd,
|
||||
generateImageThumbnail,
|
||||
};
|
||||
|
|
|
@ -9,9 +9,9 @@ import { existsSync } from 'fs';
|
|||
|
||||
const execAsync = util.promisify(require('child_process').exec);
|
||||
|
||||
export const INPUT_PATH_PLACEHOLDER = 'INPUT';
|
||||
export const FFMPEG_PLACEHOLDER = 'FFMPEG';
|
||||
export const OUTPUT_PATH_PLACEHOLDER = 'OUTPUT';
|
||||
const INPUT_PATH_PLACEHOLDER = 'INPUT';
|
||||
const FFMPEG_PLACEHOLDER = 'FFMPEG';
|
||||
const OUTPUT_PATH_PLACEHOLDER = 'OUTPUT';
|
||||
|
||||
function getFFmpegStaticPath() {
|
||||
return pathToFfmpeg.replace('app.asar', 'app.asar.unpacked');
|
||||
|
@ -37,12 +37,20 @@ export async function runFFmpegCmd(
|
|||
return cmdPart;
|
||||
}
|
||||
});
|
||||
cmd = shellescape(cmd);
|
||||
log.info('cmd', cmd);
|
||||
await execAsync(cmd);
|
||||
const escapedCmd = shellescape(cmd);
|
||||
log.info('running ffmpeg command', escapedCmd);
|
||||
const startTime = Date.now();
|
||||
await execAsync(escapedCmd);
|
||||
if (!existsSync(tempOutputFilePath)) {
|
||||
throw new Error('ffmpeg output file not found');
|
||||
}
|
||||
log.info(
|
||||
'ffmpeg command execution time ',
|
||||
escapedCmd,
|
||||
Date.now() - startTime,
|
||||
'ms'
|
||||
);
|
||||
|
||||
const outputFile = await readFile(tempOutputFilePath);
|
||||
return new Uint8Array(outputFile);
|
||||
} catch (e) {
|
||||
|
|
|
@ -192,7 +192,9 @@ export async function isFolder(dirPath: string) {
|
|||
.catch(() => false);
|
||||
}
|
||||
|
||||
export const convertBrowserStreamToNode = (fileStream: any) => {
|
||||
export const convertBrowserStreamToNode = (
|
||||
fileStream: ReadableStream<Uint8Array>
|
||||
) => {
|
||||
const reader = fileStream.getReader();
|
||||
const rs = new Readable();
|
||||
|
||||
|
@ -210,10 +212,17 @@ export const convertBrowserStreamToNode = (fileStream: any) => {
|
|||
return rs;
|
||||
};
|
||||
|
||||
export function writeStream(filePath: string, fileStream: any) {
|
||||
export async function writeStream(
|
||||
filePath: string,
|
||||
fileStream: ReadableStream<Uint8Array>
|
||||
) {
|
||||
const writeable = fs.createWriteStream(filePath);
|
||||
const readable = convertBrowserStreamToNode(fileStream);
|
||||
readable.pipe(writeable);
|
||||
await new Promise((resolve, reject) => {
|
||||
writeable.on('finish', resolve);
|
||||
writeable.on('error', reject);
|
||||
});
|
||||
}
|
||||
|
||||
export async function readTextFile(filePath: string) {
|
||||
|
|
|
@ -1,72 +0,0 @@
|
|||
import util from 'util';
|
||||
import { exec } from 'child_process';
|
||||
|
||||
import { existsSync, rmSync } from 'fs';
|
||||
import { readFile, writeFile } from 'promise-fs';
|
||||
import { generateTempFilePath } from '../utils/temp';
|
||||
import { logErrorSentry } from './sentry';
|
||||
import { isPlatform } from '../utils/main';
|
||||
import { isDev } from '../utils/common';
|
||||
import path from 'path';
|
||||
|
||||
const asyncExec = util.promisify(exec);
|
||||
|
||||
function getImageMagickStaticPath() {
|
||||
return isDev
|
||||
? 'build/image-magick'
|
||||
: path.join(process.resourcesPath, 'image-magick');
|
||||
}
|
||||
|
||||
export async function convertHEIC(
|
||||
heicFileData: Uint8Array
|
||||
): Promise<Uint8Array> {
|
||||
let tempInputFilePath: string;
|
||||
let tempOutputFilePath: string;
|
||||
try {
|
||||
tempInputFilePath = await generateTempFilePath('.heic');
|
||||
tempOutputFilePath = await generateTempFilePath('.jpeg');
|
||||
|
||||
await writeFile(tempInputFilePath, heicFileData);
|
||||
|
||||
await runConvertCommand(tempInputFilePath, tempOutputFilePath);
|
||||
|
||||
if (!existsSync(tempOutputFilePath)) {
|
||||
throw new Error('heic convert output file not found');
|
||||
}
|
||||
const convertedFileData = new Uint8Array(
|
||||
await readFile(tempOutputFilePath)
|
||||
);
|
||||
return convertedFileData;
|
||||
} catch (e) {
|
||||
logErrorSentry(e, 'failed to convert heic');
|
||||
throw e;
|
||||
} finally {
|
||||
try {
|
||||
rmSync(tempInputFilePath, { force: true });
|
||||
} catch (e) {
|
||||
logErrorSentry(e, 'failed to remove tempInputFile');
|
||||
}
|
||||
try {
|
||||
rmSync(tempOutputFilePath, { force: true });
|
||||
} catch (e) {
|
||||
logErrorSentry(e, 'failed to remove tempOutputFile');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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(
|
||||
`${getImageMagickStaticPath()} ${tempInputFilePath} -quality 100% ${tempOutputFilePath}`
|
||||
);
|
||||
} else {
|
||||
Error(`${process.platform} native heic convert not supported yet`);
|
||||
}
|
||||
}
|
279
src/services/imageProcessor.ts
Normal file
279
src/services/imageProcessor.ts
Normal file
|
@ -0,0 +1,279 @@
|
|||
import util from 'util';
|
||||
import { exec } from 'child_process';
|
||||
|
||||
import { existsSync, rmSync } from 'fs';
|
||||
import { readFile, writeFile } from 'promise-fs';
|
||||
import { generateTempFilePath } from '../utils/temp';
|
||||
import { logErrorSentry } from './sentry';
|
||||
import { isPlatform } from '../utils/main';
|
||||
import { isDev } from '../utils/common';
|
||||
import path from 'path';
|
||||
import log from 'electron-log';
|
||||
const shellescape = require('any-shell-escape');
|
||||
|
||||
const asyncExec = util.promisify(exec);
|
||||
|
||||
const IMAGE_MAGICK_PLACEHOLDER = 'IMAGE_MAGICK';
|
||||
const MAX_DIMENSION_PLACEHOLDER = 'MAX_DIMENSION';
|
||||
const SAMPLE_SIZE_PLACEHOLDER = 'SAMPLE_SIZE';
|
||||
const INPUT_PATH_PLACEHOLDER = 'INPUT';
|
||||
const OUTPUT_PATH_PLACEHOLDER = 'OUTPUT';
|
||||
const QUALITY_PLACEHOLDER = 'QUALITY';
|
||||
|
||||
const MAX_QUALITY = 70;
|
||||
const MIN_QUALITY = 50;
|
||||
|
||||
const SIPS_HEIC_CONVERT_COMMAND_TEMPLATE = [
|
||||
'sips',
|
||||
'-s',
|
||||
'format',
|
||||
'jpeg',
|
||||
INPUT_PATH_PLACEHOLDER,
|
||||
'--out',
|
||||
OUTPUT_PATH_PLACEHOLDER,
|
||||
];
|
||||
|
||||
const SIPS_THUMBNAIL_GENERATE_COMMAND_TEMPLATE = [
|
||||
'sips',
|
||||
'-s',
|
||||
'format',
|
||||
'jpeg',
|
||||
'-s',
|
||||
'formatOptions',
|
||||
QUALITY_PLACEHOLDER,
|
||||
'-Z',
|
||||
MAX_DIMENSION_PLACEHOLDER,
|
||||
INPUT_PATH_PLACEHOLDER,
|
||||
'--out',
|
||||
OUTPUT_PATH_PLACEHOLDER,
|
||||
];
|
||||
|
||||
const IMAGEMAGICK_HEIC_CONVERT_COMMAND_TEMPLATE = [
|
||||
IMAGE_MAGICK_PLACEHOLDER,
|
||||
INPUT_PATH_PLACEHOLDER,
|
||||
'-quality',
|
||||
'100%',
|
||||
OUTPUT_PATH_PLACEHOLDER,
|
||||
];
|
||||
|
||||
const IMAGE_MAGICK_THUMBNAIL_GENERATE_COMMAND_TEMPLATE = [
|
||||
IMAGE_MAGICK_PLACEHOLDER,
|
||||
'-define',
|
||||
`jpeg:size=${SAMPLE_SIZE_PLACEHOLDER}x${SAMPLE_SIZE_PLACEHOLDER}`,
|
||||
INPUT_PATH_PLACEHOLDER,
|
||||
'-thumbnail',
|
||||
`${MAX_DIMENSION_PLACEHOLDER}x${MAX_DIMENSION_PLACEHOLDER}>`,
|
||||
'-unsharp',
|
||||
'0x.5',
|
||||
'-quality',
|
||||
QUALITY_PLACEHOLDER,
|
||||
OUTPUT_PATH_PLACEHOLDER,
|
||||
];
|
||||
|
||||
function getImageMagickStaticPath() {
|
||||
return isDev
|
||||
? 'build/image-magick'
|
||||
: path.join(process.resourcesPath, 'image-magick');
|
||||
}
|
||||
|
||||
export async function convertHEIC(
|
||||
heicFileData: Uint8Array
|
||||
): Promise<Uint8Array> {
|
||||
let tempInputFilePath: string;
|
||||
let tempOutputFilePath: string;
|
||||
try {
|
||||
tempInputFilePath = await generateTempFilePath('input.heic');
|
||||
tempOutputFilePath = await generateTempFilePath('output.jpeg');
|
||||
|
||||
await writeFile(tempInputFilePath, heicFileData);
|
||||
|
||||
await runConvertCommand(tempInputFilePath, tempOutputFilePath);
|
||||
|
||||
if (!existsSync(tempOutputFilePath)) {
|
||||
throw new Error('heic convert output file not found');
|
||||
}
|
||||
const convertedFileData = new Uint8Array(
|
||||
await readFile(tempOutputFilePath)
|
||||
);
|
||||
return convertedFileData;
|
||||
} catch (e) {
|
||||
logErrorSentry(e, 'failed to convert heic');
|
||||
throw e;
|
||||
} finally {
|
||||
try {
|
||||
rmSync(tempInputFilePath, { force: true });
|
||||
} catch (e) {
|
||||
logErrorSentry(e, 'failed to remove tempInputFile');
|
||||
}
|
||||
try {
|
||||
rmSync(tempOutputFilePath, { force: true });
|
||||
} catch (e) {
|
||||
logErrorSentry(e, 'failed to remove tempOutputFile');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function runConvertCommand(
|
||||
tempInputFilePath: string,
|
||||
tempOutputFilePath: string
|
||||
) {
|
||||
const convertCmd = constructConvertCommand(
|
||||
tempInputFilePath,
|
||||
tempOutputFilePath
|
||||
);
|
||||
const escapedCmd = shellescape(convertCmd);
|
||||
log.info('running convert command: ' + escapedCmd);
|
||||
await asyncExec(escapedCmd);
|
||||
}
|
||||
|
||||
function constructConvertCommand(
|
||||
tempInputFilePath: string,
|
||||
tempOutputFilePath: string
|
||||
) {
|
||||
let convertCmd: string[];
|
||||
if (isPlatform('mac')) {
|
||||
convertCmd = SIPS_HEIC_CONVERT_COMMAND_TEMPLATE.map((cmdPart) => {
|
||||
if (cmdPart === INPUT_PATH_PLACEHOLDER) {
|
||||
return tempInputFilePath;
|
||||
}
|
||||
if (cmdPart === OUTPUT_PATH_PLACEHOLDER) {
|
||||
return tempOutputFilePath;
|
||||
}
|
||||
return cmdPart;
|
||||
});
|
||||
} else if (isPlatform('linux')) {
|
||||
convertCmd = IMAGEMAGICK_HEIC_CONVERT_COMMAND_TEMPLATE.map(
|
||||
(cmdPart) => {
|
||||
if (cmdPart === IMAGE_MAGICK_PLACEHOLDER) {
|
||||
return getImageMagickStaticPath();
|
||||
}
|
||||
if (cmdPart === INPUT_PATH_PLACEHOLDER) {
|
||||
return tempInputFilePath;
|
||||
}
|
||||
if (cmdPart === OUTPUT_PATH_PLACEHOLDER) {
|
||||
return tempOutputFilePath;
|
||||
}
|
||||
return cmdPart;
|
||||
}
|
||||
);
|
||||
} else {
|
||||
Error(`${process.platform} native heic convert not supported yet`);
|
||||
}
|
||||
return convertCmd;
|
||||
}
|
||||
|
||||
export async function generateImageThumbnail(
|
||||
inputFilePath: string,
|
||||
width: number,
|
||||
maxSize: number
|
||||
): Promise<Uint8Array> {
|
||||
let tempOutputFilePath: string;
|
||||
let quality = MAX_QUALITY;
|
||||
try {
|
||||
tempOutputFilePath = await generateTempFilePath('thumb.jpeg');
|
||||
let thumbnail: Uint8Array;
|
||||
do {
|
||||
await runThumbnailGenerationCommand(
|
||||
inputFilePath,
|
||||
tempOutputFilePath,
|
||||
width,
|
||||
quality
|
||||
);
|
||||
|
||||
if (!existsSync(tempOutputFilePath)) {
|
||||
throw new Error('output thumbnail file not found');
|
||||
}
|
||||
thumbnail = new Uint8Array(await readFile(tempOutputFilePath));
|
||||
quality -= 10;
|
||||
} while (thumbnail.length > maxSize && quality > MIN_QUALITY);
|
||||
return thumbnail;
|
||||
} catch (e) {
|
||||
logErrorSentry(e, 'generate image thumbnail failed');
|
||||
throw e;
|
||||
} finally {
|
||||
try {
|
||||
rmSync(tempOutputFilePath, { force: true });
|
||||
} catch (e) {
|
||||
logErrorSentry(e, 'failed to remove tempOutputFile');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function runThumbnailGenerationCommand(
|
||||
inputFilePath: string,
|
||||
tempOutputFilePath: string,
|
||||
maxDimension: number,
|
||||
quality: number
|
||||
) {
|
||||
const thumbnailGenerationCmd: string[] =
|
||||
constructThumbnailGenerationCommand(
|
||||
inputFilePath,
|
||||
tempOutputFilePath,
|
||||
maxDimension,
|
||||
quality
|
||||
);
|
||||
const escapedCmd = shellescape(thumbnailGenerationCmd);
|
||||
log.info('running thumbnail generation command: ' + escapedCmd);
|
||||
await asyncExec(escapedCmd);
|
||||
}
|
||||
function constructThumbnailGenerationCommand(
|
||||
inputFilePath: string,
|
||||
tempOutputFilePath: string,
|
||||
maxDimension: number,
|
||||
quality: number
|
||||
) {
|
||||
let thumbnailGenerationCmd: string[];
|
||||
if (isPlatform('mac')) {
|
||||
thumbnailGenerationCmd = SIPS_THUMBNAIL_GENERATE_COMMAND_TEMPLATE.map(
|
||||
(cmdPart) => {
|
||||
if (cmdPart === INPUT_PATH_PLACEHOLDER) {
|
||||
return inputFilePath;
|
||||
}
|
||||
if (cmdPart === OUTPUT_PATH_PLACEHOLDER) {
|
||||
return tempOutputFilePath;
|
||||
}
|
||||
if (cmdPart === MAX_DIMENSION_PLACEHOLDER) {
|
||||
return maxDimension.toString();
|
||||
}
|
||||
if (cmdPart === QUALITY_PLACEHOLDER) {
|
||||
return quality.toString();
|
||||
}
|
||||
return cmdPart;
|
||||
}
|
||||
);
|
||||
} else if (isPlatform('linux')) {
|
||||
thumbnailGenerationCmd =
|
||||
IMAGE_MAGICK_THUMBNAIL_GENERATE_COMMAND_TEMPLATE.map((cmdPart) => {
|
||||
if (cmdPart === IMAGE_MAGICK_PLACEHOLDER) {
|
||||
return getImageMagickStaticPath();
|
||||
}
|
||||
if (cmdPart === INPUT_PATH_PLACEHOLDER) {
|
||||
return inputFilePath;
|
||||
}
|
||||
if (cmdPart === OUTPUT_PATH_PLACEHOLDER) {
|
||||
return tempOutputFilePath;
|
||||
}
|
||||
if (cmdPart.includes(SAMPLE_SIZE_PLACEHOLDER)) {
|
||||
return cmdPart.replaceAll(
|
||||
SAMPLE_SIZE_PLACEHOLDER,
|
||||
(2 * maxDimension).toString()
|
||||
);
|
||||
}
|
||||
if (cmdPart.includes(MAX_DIMENSION_PLACEHOLDER)) {
|
||||
return cmdPart.replaceAll(
|
||||
MAX_DIMENSION_PLACEHOLDER,
|
||||
maxDimension.toString()
|
||||
);
|
||||
}
|
||||
if (cmdPart === QUALITY_PLACEHOLDER) {
|
||||
return quality.toString();
|
||||
}
|
||||
return cmdPart;
|
||||
});
|
||||
} else {
|
||||
Error(
|
||||
`${process.platform} native thumbnail generation not supported yet`
|
||||
);
|
||||
}
|
||||
return thumbnailGenerationCmd;
|
||||
}
|
|
@ -14,17 +14,17 @@ import { getSentryUserID, logErrorSentry } from '../services/sentry';
|
|||
import chokidar from 'chokidar';
|
||||
import path from 'path';
|
||||
import { getDirFilePaths } from '../services/fs';
|
||||
import { convertHEIC } from '../services/heicConverter';
|
||||
import {
|
||||
convertHEIC,
|
||||
generateImageThumbnail,
|
||||
} from '../services/imageProcessor';
|
||||
import {
|
||||
getAppVersion,
|
||||
skipAppVersion,
|
||||
updateAndRestart,
|
||||
} from '../services/appUpdater';
|
||||
import {
|
||||
deleteTempFile,
|
||||
runFFmpegCmd,
|
||||
writeTempFile,
|
||||
} from '../services/ffmpeg';
|
||||
import { deleteTempFile, runFFmpegCmd } from '../services/ffmpeg';
|
||||
import { generateTempFilePath } from './temp';
|
||||
|
||||
export default function setupIpcComs(
|
||||
tray: Tray,
|
||||
|
@ -137,13 +137,17 @@ export default function setupIpcComs(
|
|||
return runFFmpegCmd(cmd, inputFilePath, outputFileName);
|
||||
}
|
||||
);
|
||||
ipcMain.handle(
|
||||
'write-temp-file',
|
||||
(_, fileStream: Uint8Array, fileName: string) => {
|
||||
return writeTempFile(fileStream, fileName);
|
||||
}
|
||||
);
|
||||
ipcMain.handle('get-temp-file-path', (_, formatSuffix) => {
|
||||
return generateTempFilePath(formatSuffix);
|
||||
});
|
||||
ipcMain.handle('remove-temp-file', (_, tempFilePath: string) => {
|
||||
return deleteTempFile(tempFilePath);
|
||||
});
|
||||
|
||||
ipcMain.handle(
|
||||
'generate-image-thumbnail',
|
||||
(_, fileData, maxDimension, maxSize) => {
|
||||
return generateImageThumbnail(fileData, maxDimension, maxSize);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -1,27 +1,149 @@
|
|||
import ElectronLog from 'electron-log';
|
||||
import { webFrame } from 'electron/renderer';
|
||||
|
||||
const FIVE_MINUTES_IN_MICROSECONDS = 30 * 1000;
|
||||
const LOGGING_INTERVAL_IN_MICROSECONDS = 30 * 1000; // 30 seconds
|
||||
|
||||
const SPIKE_DETECTION_INTERVAL_IN_MICROSECONDS = 1 * 1000; // 1 seconds
|
||||
|
||||
const MAIN_MEMORY_USAGE_DIFF_IN_KILOBYTES_CONSIDERED_AS_SPIKE = 10 * 1024; // 10 MB
|
||||
|
||||
const HIGH_MAIN_MEMORY_USAGE_THRESHOLD_IN_KILOBYTES = 100 * 1024; // 100 MB
|
||||
|
||||
const RENDERER_MEMORY_USAGE_DIFF_IN_KILOBYTES_CONSIDERED_AS_SPIKE = 200 * 1024; // 200 MB
|
||||
|
||||
const HIGH_RENDERER_MEMORY_USAGE_THRESHOLD_IN_KILOBYTES = 1024 * 1024; // 1 GB
|
||||
|
||||
async function logMainProcessStats() {
|
||||
const systemMemoryInfo = process.getSystemMemoryInfo();
|
||||
const processMemoryInfo = await getNormalizedProcessMemoryInfo(
|
||||
await process.getProcessMemoryInfo()
|
||||
);
|
||||
const cpuUsage = process.getCPUUsage();
|
||||
const processMemoryInfo = await process.getProcessMemoryInfo();
|
||||
const heapStatistics = process.getHeapStatistics();
|
||||
const heapStatistics = getNormalizedHeapStatistics(
|
||||
process.getHeapStatistics()
|
||||
);
|
||||
|
||||
ElectronLog.log('main process stats', {
|
||||
systemMemoryInfo,
|
||||
cpuUsage,
|
||||
processMemoryInfo,
|
||||
heapStatistics,
|
||||
cpuUsage,
|
||||
});
|
||||
}
|
||||
|
||||
let previousMainProcessMemoryInfo: Electron.ProcessMemoryInfo = {
|
||||
private: 0,
|
||||
shared: 0,
|
||||
residentSet: 0,
|
||||
};
|
||||
|
||||
let mainProcessUsingHighMemory = false;
|
||||
|
||||
async function logSpikeMainMemoryUsage() {
|
||||
const processMemoryInfo = await process.getProcessMemoryInfo();
|
||||
const currentMemoryUsage = Math.max(
|
||||
processMemoryInfo.residentSet,
|
||||
processMemoryInfo.private
|
||||
);
|
||||
const previewMemoryUsage = Math.max(
|
||||
previousMainProcessMemoryInfo.private,
|
||||
previousMainProcessMemoryInfo.residentSet
|
||||
);
|
||||
const isSpiking =
|
||||
currentMemoryUsage - previewMemoryUsage >=
|
||||
MAIN_MEMORY_USAGE_DIFF_IN_KILOBYTES_CONSIDERED_AS_SPIKE;
|
||||
|
||||
const isHighMemoryUsage =
|
||||
currentMemoryUsage >= HIGH_MAIN_MEMORY_USAGE_THRESHOLD_IN_KILOBYTES;
|
||||
|
||||
const shouldReport =
|
||||
(isHighMemoryUsage && !mainProcessUsingHighMemory) ||
|
||||
(!isHighMemoryUsage && mainProcessUsingHighMemory);
|
||||
|
||||
if (isSpiking || shouldReport) {
|
||||
const normalizedCurrentProcessMemoryInfo =
|
||||
await getNormalizedProcessMemoryInfo(processMemoryInfo);
|
||||
const normalizedPreviousProcessMemoryInfo =
|
||||
await getNormalizedProcessMemoryInfo(previousMainProcessMemoryInfo);
|
||||
const cpuUsage = process.getCPUUsage();
|
||||
const heapStatistics = getNormalizedHeapStatistics(
|
||||
process.getHeapStatistics()
|
||||
);
|
||||
|
||||
ElectronLog.log('reporting main memory usage spike', {
|
||||
currentProcessMemoryInfo: normalizedCurrentProcessMemoryInfo,
|
||||
previousProcessMemoryInfo: normalizedPreviousProcessMemoryInfo,
|
||||
heapStatistics,
|
||||
cpuUsage,
|
||||
});
|
||||
}
|
||||
previousMainProcessMemoryInfo = processMemoryInfo;
|
||||
if (shouldReport) {
|
||||
mainProcessUsingHighMemory = !mainProcessUsingHighMemory;
|
||||
}
|
||||
}
|
||||
|
||||
let previousRendererProcessMemoryInfo: Electron.ProcessMemoryInfo = {
|
||||
private: 0,
|
||||
shared: 0,
|
||||
residentSet: 0,
|
||||
};
|
||||
|
||||
let rendererUsingHighMemory = false;
|
||||
|
||||
async function logSpikeRendererMemoryUsage() {
|
||||
const processMemoryInfo = await process.getProcessMemoryInfo();
|
||||
const currentMemoryUsage = Math.max(
|
||||
processMemoryInfo.residentSet,
|
||||
processMemoryInfo.private
|
||||
);
|
||||
const previewMemoryUsage = Math.max(
|
||||
previousRendererProcessMemoryInfo.private,
|
||||
previousRendererProcessMemoryInfo.residentSet
|
||||
);
|
||||
const isSpiking =
|
||||
currentMemoryUsage - previewMemoryUsage >=
|
||||
RENDERER_MEMORY_USAGE_DIFF_IN_KILOBYTES_CONSIDERED_AS_SPIKE;
|
||||
|
||||
const isHighMemoryUsage =
|
||||
currentMemoryUsage >= HIGH_RENDERER_MEMORY_USAGE_THRESHOLD_IN_KILOBYTES;
|
||||
|
||||
const shouldReport =
|
||||
(isHighMemoryUsage && !rendererUsingHighMemory) ||
|
||||
(!isHighMemoryUsage && rendererUsingHighMemory);
|
||||
|
||||
if (isSpiking || shouldReport) {
|
||||
const normalizedCurrentProcessMemoryInfo =
|
||||
await getNormalizedProcessMemoryInfo(processMemoryInfo);
|
||||
const normalizedPreviousProcessMemoryInfo =
|
||||
await getNormalizedProcessMemoryInfo(
|
||||
previousRendererProcessMemoryInfo
|
||||
);
|
||||
const cpuUsage = process.getCPUUsage();
|
||||
const heapStatistics = getNormalizedHeapStatistics(
|
||||
process.getHeapStatistics()
|
||||
);
|
||||
|
||||
ElectronLog.log('reporting renderer memory usage spike', {
|
||||
currentProcessMemoryInfo: normalizedCurrentProcessMemoryInfo,
|
||||
previousProcessMemoryInfo: normalizedPreviousProcessMemoryInfo,
|
||||
heapStatistics,
|
||||
cpuUsage,
|
||||
});
|
||||
}
|
||||
previousRendererProcessMemoryInfo = processMemoryInfo;
|
||||
if (shouldReport) {
|
||||
rendererUsingHighMemory = !rendererUsingHighMemory;
|
||||
}
|
||||
}
|
||||
|
||||
async function logRendererProcessStats() {
|
||||
const blinkMemoryInfo = process.getBlinkMemoryInfo();
|
||||
const heapStatistics = process.getHeapStatistics();
|
||||
const processMemoryInfo = process.getProcessMemoryInfo();
|
||||
const webFrameResourceUsage = webFrame.getResourceUsage();
|
||||
const blinkMemoryInfo = getNormalizedBlinkMemoryInfo();
|
||||
const heapStatistics = getNormalizedHeapStatistics(
|
||||
process.getHeapStatistics()
|
||||
);
|
||||
const webFrameResourceUsage = getNormalizedWebFrameResourceUsage();
|
||||
const processMemoryInfo = await getNormalizedProcessMemoryInfo(
|
||||
await process.getProcessMemoryInfo()
|
||||
);
|
||||
ElectronLog.log('renderer process stats', {
|
||||
blinkMemoryInfo,
|
||||
heapStatistics,
|
||||
|
@ -31,9 +153,138 @@ async function logRendererProcessStats() {
|
|||
}
|
||||
|
||||
export function setupMainProcessStatsLogger() {
|
||||
setInterval(logMainProcessStats, FIVE_MINUTES_IN_MICROSECONDS);
|
||||
setInterval(
|
||||
logSpikeMainMemoryUsage,
|
||||
SPIKE_DETECTION_INTERVAL_IN_MICROSECONDS
|
||||
);
|
||||
setInterval(logMainProcessStats, LOGGING_INTERVAL_IN_MICROSECONDS);
|
||||
}
|
||||
|
||||
export function setupRendererProcessStatsLogger() {
|
||||
setInterval(logRendererProcessStats, FIVE_MINUTES_IN_MICROSECONDS);
|
||||
setInterval(
|
||||
logSpikeRendererMemoryUsage,
|
||||
SPIKE_DETECTION_INTERVAL_IN_MICROSECONDS
|
||||
);
|
||||
setInterval(logRendererProcessStats, LOGGING_INTERVAL_IN_MICROSECONDS);
|
||||
}
|
||||
|
||||
const getNormalizedProcessMemoryInfo = async (
|
||||
processMemoryInfo: Electron.ProcessMemoryInfo
|
||||
) => {
|
||||
return {
|
||||
residentSet: convertBytesToHumanReadable(
|
||||
processMemoryInfo.residentSet * 1024
|
||||
),
|
||||
private: convertBytesToHumanReadable(processMemoryInfo.private * 1024),
|
||||
shared: convertBytesToHumanReadable(processMemoryInfo.shared * 1024),
|
||||
};
|
||||
};
|
||||
|
||||
const getNormalizedBlinkMemoryInfo = () => {
|
||||
const blinkMemoryInfo = process.getBlinkMemoryInfo();
|
||||
return {
|
||||
allocated: convertBytesToHumanReadable(
|
||||
blinkMemoryInfo.allocated * 1024
|
||||
),
|
||||
total: convertBytesToHumanReadable(blinkMemoryInfo.total * 1024),
|
||||
};
|
||||
};
|
||||
|
||||
const getNormalizedHeapStatistics = (
|
||||
heapStatistics: Electron.HeapStatistics
|
||||
) => {
|
||||
return {
|
||||
totalHeapSize: convertBytesToHumanReadable(
|
||||
heapStatistics.totalHeapSize * 1024
|
||||
),
|
||||
totalHeapSizeExecutable: convertBytesToHumanReadable(
|
||||
heapStatistics.totalHeapSizeExecutable * 1024
|
||||
),
|
||||
totalPhysicalSize: convertBytesToHumanReadable(
|
||||
heapStatistics.totalPhysicalSize * 1024
|
||||
),
|
||||
totalAvailableSize: convertBytesToHumanReadable(
|
||||
heapStatistics.totalAvailableSize * 1024
|
||||
),
|
||||
usedHeapSize: convertBytesToHumanReadable(
|
||||
heapStatistics.usedHeapSize * 1024
|
||||
),
|
||||
|
||||
heapSizeLimit: convertBytesToHumanReadable(
|
||||
heapStatistics.heapSizeLimit * 1024
|
||||
),
|
||||
mallocedMemory: convertBytesToHumanReadable(
|
||||
heapStatistics.mallocedMemory * 1024
|
||||
),
|
||||
peakMallocedMemory: convertBytesToHumanReadable(
|
||||
heapStatistics.peakMallocedMemory * 1024
|
||||
),
|
||||
doesZapGarbage: heapStatistics.doesZapGarbage,
|
||||
};
|
||||
};
|
||||
|
||||
const getNormalizedWebFrameResourceUsage = () => {
|
||||
const webFrameResourceUsage = webFrame.getResourceUsage();
|
||||
return {
|
||||
images: {
|
||||
count: webFrameResourceUsage.images.count,
|
||||
size: convertBytesToHumanReadable(
|
||||
webFrameResourceUsage.images.size
|
||||
),
|
||||
liveSize: convertBytesToHumanReadable(
|
||||
webFrameResourceUsage.images.liveSize
|
||||
),
|
||||
},
|
||||
scripts: {
|
||||
count: webFrameResourceUsage.scripts.count,
|
||||
size: convertBytesToHumanReadable(
|
||||
webFrameResourceUsage.scripts.size
|
||||
),
|
||||
liveSize: convertBytesToHumanReadable(
|
||||
webFrameResourceUsage.scripts.liveSize
|
||||
),
|
||||
},
|
||||
cssStyleSheets: {
|
||||
count: webFrameResourceUsage.cssStyleSheets.count,
|
||||
size: convertBytesToHumanReadable(
|
||||
webFrameResourceUsage.cssStyleSheets.size
|
||||
),
|
||||
liveSize: convertBytesToHumanReadable(
|
||||
webFrameResourceUsage.cssStyleSheets.liveSize
|
||||
),
|
||||
},
|
||||
xslStyleSheets: {
|
||||
count: webFrameResourceUsage.xslStyleSheets.count,
|
||||
size: convertBytesToHumanReadable(
|
||||
webFrameResourceUsage.xslStyleSheets.size
|
||||
),
|
||||
liveSize: convertBytesToHumanReadable(
|
||||
webFrameResourceUsage.xslStyleSheets.liveSize
|
||||
),
|
||||
},
|
||||
fonts: {
|
||||
count: webFrameResourceUsage.fonts.count,
|
||||
size: convertBytesToHumanReadable(webFrameResourceUsage.fonts.size),
|
||||
liveSize: convertBytesToHumanReadable(
|
||||
webFrameResourceUsage.fonts.liveSize
|
||||
),
|
||||
},
|
||||
other: {
|
||||
count: webFrameResourceUsage.other.count,
|
||||
size: convertBytesToHumanReadable(webFrameResourceUsage.other.size),
|
||||
liveSize: convertBytesToHumanReadable(
|
||||
webFrameResourceUsage.other.liveSize
|
||||
),
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
function convertBytesToHumanReadable(bytes: number, precision = 2): string {
|
||||
if (bytes === 0 || isNaN(bytes)) {
|
||||
return '0 MB';
|
||||
}
|
||||
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
||||
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
||||
return (bytes / Math.pow(1024, i)).toFixed(precision) + ' ' + sizes[i];
|
||||
}
|
||||
|
|
|
@ -28,11 +28,11 @@ function generateTempName(length: number) {
|
|||
}
|
||||
|
||||
export async function generateTempFilePath(formatSuffix: string) {
|
||||
const tempDirPath = await getTempDirPath();
|
||||
const namePrefix = generateTempName(10);
|
||||
const tempFilePath = path.join(
|
||||
tempDirPath,
|
||||
namePrefix + '-' + formatSuffix
|
||||
);
|
||||
let tempFilePath: string;
|
||||
do {
|
||||
const tempDirPath = await getTempDirPath();
|
||||
const namePrefix = generateTempName(10);
|
||||
tempFilePath = path.join(tempDirPath, namePrefix + '-' + formatSuffix);
|
||||
} while (existsSync(tempFilePath));
|
||||
return tempFilePath;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2017",
|
||||
"target": "es2021",
|
||||
"module": "commonjs",
|
||||
"esModuleInterop": true,
|
||||
"noImplicitAny": true,
|
||||
|
|
2
ui
2
ui
|
@ -1 +1 @@
|
|||
Subproject commit a8c520a4b0ff90279cffedf41b9e8f8564d9a753
|
||||
Subproject commit 86a7f1cdab5c3a9a2cfbd0daecb9f7e1dd06660e
|
Loading…
Reference in a new issue