Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

update to new functions in more places

Manav Rathicf71d1477b80
diff --git a/desktop/src/main/util.ts b/desktop/src/main/util.ts
index 4933e5ff49..a06b4580c6 100644
--- a/desktop/src/main/util.ts
+++ b/desktop/src/main/util.ts
@@ -4,6 +4,7 @@ import { app } from "electron/main";
import { exec } from "node:child_process";
import path from "node:path";
import { promisify } from "node:util";
+import log from "./log";
/** `true` if the app is running in development mode. */
export const isDev = !app.isPackaged;
@@ -22,7 +23,17 @@ export const isDev = !app.isPackaged;
* this might not be the best option and it might be better to use the
* underlying functions.
*/
-export const execAsync = (command: string) => execAsync_(shellescape(command));
+export const execAsync = (command: string | string[]) => {
+ const escapedCommand = shellescape(command);
+ const startTime = Date.now();
+ log.debug(() => `Running shell command: ${escapedCommand}`);
+ const result = execAsync_(escapedCommand);
+ log.debug(
+ () =>
+ `Completed in ${Math.round(Date.now() - startTime)} ms (${escapedCommand})`,
+ );
+ return result;
+};
const execAsync_ = promisify(exec);
diff --git a/desktop/src/services/clipService.ts b/desktop/src/services/clipService.ts
index 6979054503..41e559a9b7 100644
--- a/desktop/src/services/clipService.ts
+++ b/desktop/src/services/clipService.ts
@@ -1,20 +1,16 @@
-import log from "electron-log";
import { app, net } from "electron/main";
import { existsSync } from "fs";
import fs from "node:fs/promises";
import path from "node:path";
-import util from "util";
import { CustomErrors } from "../constants/errors";
import { writeStream } from "../main/fs";
-import { logErrorSentry } from "../main/log";
-import { isDev } from "../main/util";
+import log, { logErrorSentry } from "../main/log";
+import { execAsync, isDev } from "../main/util";
import { Model } from "../types/ipc";
import Tokenizer from "../utils/clip-bpe-ts/mod";
import { getPlatform } from "../utils/common/platform";
import { generateTempFilePath } from "../utils/temp";
import { deleteTempFile } from "./ffmpeg";
-const shellescape = require("any-shell-escape");
-const execAsync = util.promisify(require("child_process").exec);
const jpeg = require("jpeg-js");
const CLIP_MODEL_PATH_PLACEHOLDER = "CLIP_MODEL";
@@ -100,8 +96,7 @@ export async function getClipImageModelPath(type: "ggml" | "onnx") {
const localFileSize = (await fs.stat(modelSavePath)).size;
if (localFileSize !== IMAGE_MODEL_SIZE_IN_BYTES[type]) {
log.info(
- "clip image model size mismatch, downloading again got:",
- localFileSize,
+ `clip image model size mismatch, downloading again got: ${localFileSize}`,
);
imageModelDownloadInProgress = downloadModel(
modelSavePath,
@@ -139,8 +134,7 @@ export async function getClipTextModelPath(type: "ggml" | "onnx") {
const localFileSize = (await fs.stat(modelSavePath)).size;
if (localFileSize !== TEXT_MODEL_SIZE_IN_BYTES[type]) {
log.info(
- "clip text model size mismatch, downloading again got:",
- localFileSize,
+ `clip text model size mismatch, downloading again got: ${localFileSize}`,
);
textModelDownloadInProgress = true;
downloadModel(modelSavePath, TEXT_MODEL_DOWNLOAD_URL[type])
@@ -278,11 +272,7 @@ export async function computeGGMLImageEmbedding(
}
});
- const escapedCmd = shellescape(cmd);
- log.info("running clip command", escapedCmd);
- const startTime = Date.now();
- const { stdout } = await execAsync(escapedCmd);
- log.info("clip command execution time ", Date.now() - startTime);
+ const { stdout } = await execAsync(cmd);
// parse stdout and return embedding
// get the last line of stdout
const lines = stdout.split("\n");
@@ -291,7 +281,7 @@ export async function computeGGMLImageEmbedding(
const embeddingArray = new Float32Array(embedding);
return embeddingArray;
} catch (err) {
- logErrorSentry(err, "Error in computeGGMLImageEmbedding");
+ log.error("Failed to compute GGML image embedding", err);
throw err;
}
}
@@ -316,7 +306,7 @@ export async function computeONNXImageEmbedding(
const imageEmbedding = results["output"].data; // Float32Array
return normalizeEmbedding(imageEmbedding);
} catch (err) {
- logErrorSentry(err, "Error in computeONNXImageEmbedding");
+ log.error("Failed to compute ONNX image embedding", err);
throw err;
}
}
@@ -367,11 +357,7 @@ export async function computeGGMLTextEmbedding(
}
});
- const escapedCmd = shellescape(cmd);
- log.info("running clip command", escapedCmd);
- const startTime = Date.now();
- const { stdout } = await execAsync(escapedCmd);
- log.info("clip command execution time ", Date.now() - startTime);
+ const { stdout } = await execAsync(cmd);
// parse stdout and return embedding
// get the last line of stdout
const lines = stdout.split("\n");
@@ -383,7 +369,7 @@ export async function computeGGMLTextEmbedding(
if (err.message === CustomErrors.MODEL_DOWNLOAD_PENDING) {
log.info(CustomErrors.MODEL_DOWNLOAD_PENDING);
} else {
- logErrorSentry(err, "Error in computeGGMLTextEmbedding");
+ log.error("Failed to compute GGML text embedding", err);
}
throw err;
}
diff --git a/desktop/src/services/ffmpeg.ts b/desktop/src/services/ffmpeg.ts
index b6ac4dbda7..ddb3361cff 100644
--- a/desktop/src/services/ffmpeg.ts
+++ b/desktop/src/services/ffmpeg.ts
@@ -1,18 +1,13 @@
-import log from "electron-log";
import pathToFfmpeg from "ffmpeg-static";
import { existsSync } from "node:fs";
import fs from "node:fs/promises";
-import util from "util";
import { CustomErrors } from "../constants/errors";
import { writeStream } from "../main/fs";
-import { logError, logErrorSentry } from "../main/log";
+import log from "../main/log";
+import { execAsync } from "../main/util";
import { ElectronFile } from "../types/ipc";
import { generateTempFilePath, getTempDirPath } from "../utils/temp";
-const shellescape = require("any-shell-escape");
-
-const execAsync = util.promisify(require("child_process").exec);
-
const INPUT_PATH_PLACEHOLDER = "INPUT";
const FFMPEG_PLACEHOLDER = "FFMPEG";
const OUTPUT_PATH_PLACEHOLDER = "OUTPUT";
@@ -70,11 +65,7 @@ export async function runFFmpegCmd(
return new File([outputFileData], outputFileName);
} finally {
if (createdTempInputFile) {
- try {
- await deleteTempFile(inputFilePath);
- } catch (e) {
- logError(e, "failed to deleteTempFile");
- }
+ await deleteTempFile(inputFilePath);
}
}
}
@@ -100,35 +91,23 @@ export async function runFFmpegCmd_(
return cmdPart;
}
});
- const escapedCmd = shellescape(cmd);
- log.info("running ffmpeg command", escapedCmd);
- const startTime = Date.now();
+
if (dontTimeout) {
- await execAsync(escapedCmd);
+ await execAsync(cmd);
} else {
- await promiseWithTimeout(execAsync(escapedCmd), 30 * 1000);
+ await promiseWithTimeout(execAsync(cmd), 30 * 1000);
}
+
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 fs.readFile(tempOutputFilePath);
return new Uint8Array(outputFile);
} catch (e) {
- logErrorSentry(e, "ffmpeg run command error");
+ log.error("FFMPEG command failed", e);
throw e;
} finally {
- try {
- await fs.rm(tempOutputFilePath, { force: true });
- } catch (e) {
- logErrorSentry(e, "failed to remove tempOutputFile");
- }
+ await deleteTempFile(tempOutputFilePath);
}
}
@@ -153,16 +132,12 @@ export async function writeTempFile(fileStream: Uint8Array, fileName: string) {
export async function deleteTempFile(tempFilePath: string) {
const tempDirPath = await getTempDirPath();
- if (!tempFilePath.startsWith(tempDirPath)) {
- logErrorSentry(
- Error("not a temp file"),
- "tried to delete a non temp file",
- );
- }
+ if (!tempFilePath.startsWith(tempDirPath))
+ log.error("Attempting to delete a non-temp file ${tempFilePath}");
await fs.rm(tempFilePath, { force: true });
}
-export const promiseWithTimeout = async <T>(
+const promiseWithTimeout = async <T>(
request: Promise<T>,
timeout: number,
): Promise<T> => {
diff --git a/desktop/src/services/imageProcessor.ts b/desktop/src/services/imageProcessor.ts
index 5da1c204c1..f6a567f8ca 100644
--- a/desktop/src/services/imageProcessor.ts
+++ b/desktop/src/services/imageProcessor.ts
@@ -1,22 +1,15 @@
-import { exec } from "child_process";
-import log from "electron-log";
import { existsSync } from "fs";
import fs from "node:fs/promises";
import path from "path";
-import util from "util";
import { CustomErrors } from "../constants/errors";
import { writeStream } from "../main/fs";
import { logError, logErrorSentry } from "../main/log";
-import { isDev } from "../main/util";
+import { execAsync, isDev } from "../main/util";
import { ElectronFile } from "../types/ipc";
import { isPlatform } from "../utils/common/platform";
import { generateTempFilePath } from "../utils/temp";
import { deleteTempFile } from "./ffmpeg";
-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";
@@ -104,7 +97,9 @@ async function convertToJPEG_(
await fs.writeFile(tempInputFilePath, fileData);
- await runConvertCommand(tempInputFilePath, tempOutputFilePath);
+ await execAsync(
+ constructConvertCommand(tempInputFilePath, tempOutputFilePath),
+ );
return new Uint8Array(await fs.readFile(tempOutputFilePath));
} catch (e) {
@@ -124,19 +119,6 @@ async function convertToJPEG_(
}
}
-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,
@@ -222,13 +204,14 @@ async function generateImageThumbnail_(
tempOutputFilePath = await generateTempFilePath("thumb.jpeg");
let thumbnail: Uint8Array;
do {
- await runThumbnailGenerationCommand(
- inputFilePath,
- tempOutputFilePath,
- width,
- quality,
+ await execAsync(
+ constructThumbnailGenerationCommand(
+ inputFilePath,
+ tempOutputFilePath,
+ width,
+ quality,
+ ),
);
-
thumbnail = new Uint8Array(await fs.readFile(tempOutputFilePath));
quality -= 10;
} while (thumbnail.length > maxSize && quality > MIN_QUALITY);
@@ -245,23 +228,6 @@ async function generateImageThumbnail_(
}
}
-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,