Просмотр исходного кода

Convert the other conversion functions

Manav Rathi 1 год назад
Родитель
Сommit
e8acbd8c9f

+ 0 - 64
desktop/src/api/imageProcessor.ts

@@ -1,64 +0,0 @@
-import { ipcRenderer } from "electron/renderer";
-import { existsSync } from "fs";
-import { CustomErrors } from "../constants/errors";
-import { writeStream } from "../services/fs";
-import { logError } from "../main/log";
-import { ElectronFile } from "../types";
-import { isPlatform } from "../utils/common/platform";
-
-export async function convertToJPEG(
-    fileData: Uint8Array,
-    filename: string,
-): Promise<Uint8Array> {
-    if (isPlatform("windows")) {
-        throw Error(CustomErrors.WINDOWS_NATIVE_IMAGE_PROCESSING_NOT_SUPPORTED);
-    }
-    const convertedFileData = await ipcRenderer.invoke(
-        "convert-to-jpeg",
-        fileData,
-        filename,
-    );
-    return convertedFileData;
-}
-
-export async function generateImageThumbnail(
-    inputFile: File | ElectronFile,
-    maxDimension: number,
-    maxSize: number,
-): Promise<Uint8Array> {
-    let inputFilePath = null;
-    let createdTempInputFile = null;
-    try {
-        if (isPlatform("windows")) {
-            throw Error(
-                CustomErrors.WINDOWS_NATIVE_IMAGE_PROCESSING_NOT_SUPPORTED,
-            );
-        }
-        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");
-            }
-        }
-    }
-}

+ 16 - 2
desktop/src/main/ipc.ts

@@ -8,7 +8,6 @@
 
 
 import { ipcMain } from "electron/main";
 import { ipcMain } from "electron/main";
 import { clearElectronStore } from "../api/electronStore";
 import { clearElectronStore } from "../api/electronStore";
-import { runFFmpegCmd } from "../api/ffmpeg";
 import { getEncryptionKey, setEncryptionKey } from "../api/safeStorage";
 import { getEncryptionKey, setEncryptionKey } from "../api/safeStorage";
 import {
 import {
     appVersion,
     appVersion,
@@ -20,7 +19,12 @@ import {
     computeImageEmbedding,
     computeImageEmbedding,
     computeTextEmbedding,
     computeTextEmbedding,
 } from "../services/clipService";
 } from "../services/clipService";
-import type { Model } from "../types";
+import { runFFmpegCmd } from "../services/ffmpeg";
+import {
+    convertToJPEG,
+    generateImageThumbnail,
+} from "../services/imageProcessor";
+import type { ElectronFile, Model } from "../types";
 import { checkExistsAndCreateDir, fsExists } from "./fs";
 import { checkExistsAndCreateDir, fsExists } from "./fs";
 import { openDirectory, openLogDirectory } from "./general";
 import { openDirectory, openLogDirectory } from "./general";
 import { logToDisk } from "./log";
 import { logToDisk } from "./log";
@@ -71,6 +75,16 @@ export const attachIPCHandlers = () => {
         muteUpdateNotification(version),
         muteUpdateNotification(version),
     );
     );
 
 
+    ipcMain.handle("convertToJPEG", (_, fileData, filename) =>
+        convertToJPEG(fileData, filename),
+    );
+
+    ipcMain.handle(
+        "generateImageThumbnail",
+        (_, inputFile, maxDimension, maxSize) =>
+            generateImageThumbnail(inputFile, maxDimension, maxSize),
+    );
+
     ipcMain.handle(
     ipcMain.handle(
         "runFFmpegCmd",
         "runFFmpegCmd",
         (
         (

+ 21 - 4
desktop/src/preload.ts

@@ -32,7 +32,6 @@ import * as fs from "node:fs/promises";
 import { Readable } from "node:stream";
 import { Readable } from "node:stream";
 import path from "path";
 import path from "path";
 import { getDirFiles } from "./api/fs";
 import { getDirFiles } from "./api/fs";
-import { convertToJPEG, generateImageThumbnail } from "./api/imageProcessor";
 import {
 import {
     getElectronFilesFromGoogleZip,
     getElectronFilesFromGoogleZip,
     getPendingUploads,
     getPendingUploads,
@@ -51,6 +50,7 @@ import {
     updateWatchMappingSyncedFiles,
     updateWatchMappingSyncedFiles,
 } from "./api/watch";
 } from "./api/watch";
 import { logErrorSentry, setupLogging } from "./main/log";
 import { logErrorSentry, setupLogging } from "./main/log";
+import type { ElectronFile } from "types";
 
 
 setupLogging();
 setupLogging();
 
 
@@ -142,6 +142,24 @@ const muteUpdateNotification = (version: string) => {
 
 
 // - Conversion
 // - Conversion
 
 
+const convertToJPEG = (
+    fileData: Uint8Array,
+    filename: string,
+): Promise<Uint8Array> =>
+    ipcRenderer.invoke("convertToJPEG", fileData, filename);
+
+const generateImageThumbnail = (
+    inputFile: File | ElectronFile,
+    maxDimension: number,
+    maxSize: number,
+): Promise<Uint8Array> =>
+    ipcRenderer.invoke(
+        "generateImageThumbnail",
+        inputFile,
+        maxDimension,
+        maxSize,
+    );
+
 const runFFmpegCmd = (
 const runFFmpegCmd = (
     cmd: string[],
     cmd: string[],
     inputFile: File | ElectronFile,
     inputFile: File | ElectronFile,
@@ -398,6 +416,8 @@ contextBridge.exposeInMainWorld("ElectronAPIs", {
     registerUpdateEventListener,
     registerUpdateEventListener,
 
 
     // - Conversion
     // - Conversion
+    convertToJPEG,
+    generateImageThumbnail,
     runFFmpegCmd,
     runFFmpegCmd,
 
 
     // - ML
     // - ML
@@ -434,9 +454,6 @@ contextBridge.exposeInMainWorld("ElectronAPIs", {
     isFolder,
     isFolder,
     updateWatchMappingSyncedFiles,
     updateWatchMappingSyncedFiles,
     updateWatchMappingIgnoredFiles,
     updateWatchMappingIgnoredFiles,
-    convertToJPEG,
-
-    generateImageThumbnail,
     moveFile,
     moveFile,
     deleteFolder,
     deleteFolder,
     rename,
     rename,

+ 56 - 3
desktop/src/services/imageProcessor.ts

@@ -1,14 +1,18 @@
 import { exec } from "child_process";
 import { exec } from "child_process";
-import util from "util";
-
 import log from "electron-log";
 import log from "electron-log";
+import { existsSync } from "fs";
 import * as fs from "node:fs/promises";
 import * as fs from "node:fs/promises";
 import path from "path";
 import path from "path";
+import util from "util";
 import { CustomErrors } from "../constants/errors";
 import { CustomErrors } from "../constants/errors";
 import { isDev } from "../main/general";
 import { isDev } from "../main/general";
+import { logError, logErrorSentry } from "../main/log";
+import { writeStream } from "../services/fs";
+import { ElectronFile } from "../types";
 import { isPlatform } from "../utils/common/platform";
 import { isPlatform } from "../utils/common/platform";
 import { generateTempFilePath } from "../utils/temp";
 import { generateTempFilePath } from "../utils/temp";
-import { logErrorSentry } from "../main/log";
+import { deleteTempFile } from "./ffmpeg";
+
 const shellescape = require("any-shell-escape");
 const shellescape = require("any-shell-escape");
 
 
 const asyncExec = util.promisify(exec);
 const asyncExec = util.promisify(exec);
@@ -80,6 +84,17 @@ function getImageMagickStaticPath() {
 export async function convertToJPEG(
 export async function convertToJPEG(
     fileData: Uint8Array,
     fileData: Uint8Array,
     filename: string,
     filename: string,
+): Promise<Uint8Array> {
+    if (isPlatform("windows")) {
+        throw Error(CustomErrors.WINDOWS_NATIVE_IMAGE_PROCESSING_NOT_SUPPORTED);
+    }
+    const convertedFileData = await convertToJPEG_(fileData, filename);
+    return convertedFileData;
+}
+
+async function convertToJPEG_(
+    fileData: Uint8Array,
+    filename: string,
 ): Promise<Uint8Array> {
 ): Promise<Uint8Array> {
     let tempInputFilePath: string;
     let tempInputFilePath: string;
     let tempOutputFilePath: string;
     let tempOutputFilePath: string;
@@ -159,6 +174,44 @@ function constructConvertCommand(
 }
 }
 
 
 export async function generateImageThumbnail(
 export async function generateImageThumbnail(
+    inputFile: File | ElectronFile,
+    maxDimension: number,
+    maxSize: number,
+): Promise<Uint8Array> {
+    let inputFilePath = null;
+    let createdTempInputFile = null;
+    try {
+        if (isPlatform("windows")) {
+            throw Error(
+                CustomErrors.WINDOWS_NATIVE_IMAGE_PROCESSING_NOT_SUPPORTED,
+            );
+        }
+        if (!existsSync(inputFile.path)) {
+            const tempFilePath = await generateTempFilePath(inputFile.name);
+            await writeStream(tempFilePath, await inputFile.stream());
+            inputFilePath = tempFilePath;
+            createdTempInputFile = true;
+        } else {
+            inputFilePath = inputFile.path;
+        }
+        const thumbnail = await generateImageThumbnail_(
+            inputFilePath,
+            maxDimension,
+            maxSize,
+        );
+        return thumbnail;
+    } finally {
+        if (createdTempInputFile) {
+            try {
+                await deleteTempFile(inputFilePath);
+            } catch (e) {
+                logError(e, "failed to deleteTempFile");
+            }
+        }
+    }
+}
+
+async function generateImageThumbnail_(
     inputFilePath: string,
     inputFilePath: string,
     width: number,
     width: number,
     maxSize: number,
     maxSize: number,

+ 0 - 35
desktop/src/utils/ipcComms.ts

@@ -2,17 +2,7 @@ import chokidar from "chokidar";
 import { BrowserWindow, dialog, ipcMain, Tray } from "electron";
 import { BrowserWindow, dialog, ipcMain, Tray } from "electron";
 import path from "path";
 import path from "path";
 import { attachIPCHandlers } from "../main/ipc";
 import { attachIPCHandlers } from "../main/ipc";
-import {
-    computeImageEmbedding,
-    computeTextEmbedding,
-} from "../services/clipService";
-import { deleteTempFile } from "../services/ffmpeg";
 import { getDirFilePaths } from "../services/fs";
 import { getDirFilePaths } from "../services/fs";
-import {
-    convertToJPEG,
-    generateImageThumbnail,
-} from "../services/imageProcessor";
-import { generateTempFilePath } from "./temp";
 
 
 export default function setupIpcComs(
 export default function setupIpcComs(
     tray: Tray,
     tray: Tray,
@@ -65,29 +55,4 @@ export default function setupIpcComs(
     ipcMain.handle("remove-watcher", async (_, args: { dir: string }) => {
     ipcMain.handle("remove-watcher", async (_, args: { dir: string }) => {
         watcher.unwatch(args.dir);
         watcher.unwatch(args.dir);
     });
     });
-
-    ipcMain.handle("convert-to-jpeg", (_, fileData, filename) => {
-        return convertToJPEG(fileData, 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);
-        },
-    );
-
-    ipcMain.handle("compute-image-embedding", (_, model, inputFilePath) => {
-        return computeImageEmbedding(model, inputFilePath);
-    });
-    ipcMain.handle("compute-text-embedding", (_, model, text) => {
-        return computeTextEmbedding(model, text);
-    });
 }
 }

+ 18 - 10
web/packages/shared/electron/types.ts

@@ -79,16 +79,23 @@ export interface ElectronAPIsType {
     };
     };
 
 
     /** TODO: AUDIT below this */
     /** TODO: AUDIT below this */
+
     // - General
     // - General
+
     registerForegroundEventListener: (onForeground: () => void) => void;
     registerForegroundEventListener: (onForeground: () => void) => void;
+
     clearElectronStore: () => void;
     clearElectronStore: () => void;
 
 
     setEncryptionKey: (encryptionKey: string) => Promise<void>;
     setEncryptionKey: (encryptionKey: string) => Promise<void>;
+
     getEncryptionKey: () => Promise<string>;
     getEncryptionKey: () => Promise<string>;
 
 
     // - App update
     // - App update
+
     updateAndRestart: () => void;
     updateAndRestart: () => void;
+
     skipAppUpdate: (version: string) => void;
     skipAppUpdate: (version: string) => void;
+
     muteUpdateNotification: (version: string) => void;
     muteUpdateNotification: (version: string) => void;
 
 
     registerUpdateEventListener: (
     registerUpdateEventListener: (
@@ -97,6 +104,17 @@ export interface ElectronAPIsType {
 
 
     // - Conversion
     // - Conversion
 
 
+    convertToJPEG: (
+        fileData: Uint8Array,
+        filename: string,
+    ) => Promise<Uint8Array>;
+
+    generateImageThumbnail: (
+        inputFile: File | ElectronFile,
+        maxDimension: number,
+        maxSize: number,
+    ) => Promise<Uint8Array>;
+
     runFFmpegCmd: (
     runFFmpegCmd: (
         cmd: string[],
         cmd: string[],
         inputFile: File | ElectronFile,
         inputFile: File | ElectronFile,
@@ -160,16 +178,6 @@ export interface ElectronAPIsType {
         removeFolder: (folderPath: string) => Promise<void>,
         removeFolder: (folderPath: string) => Promise<void>,
     ) => void;
     ) => void;
     isFolder: (dirPath: string) => Promise<boolean>;
     isFolder: (dirPath: string) => Promise<boolean>;
-    convertToJPEG: (
-        fileData: Uint8Array,
-        filename: string,
-    ) => Promise<Uint8Array>;
-
-    generateImageThumbnail: (
-        inputFile: File | ElectronFile,
-        maxDimension: number,
-        maxSize: number,
-    ) => Promise<Uint8Array>;
     moveFile: (oldPath: string, newPath: string) => Promise<void>;
     moveFile: (oldPath: string, newPath: string) => Promise<void>;
     deleteFolder: (path: string) => Promise<void>;
     deleteFolder: (path: string) => Promise<void>;
     deleteFile: (path: string) => Promise<void>;
     deleteFile: (path: string) => Promise<void>;