Move export functions to preload

This commit is contained in:
Manav Rathi 2024-03-22 09:38:50 +05:30
parent 388904a46a
commit b1f45c8602
No known key found for this signature in database
2 changed files with 49 additions and 40 deletions

View file

@ -1,23 +0,0 @@
import * as fs from "promise-fs";
import { writeStream } from "./../services/fs";
export const exists = (path: string) => {
return fs.existsSync(path);
};
export const checkExistsAndCreateDir = async (dirPath: string) => {
if (!fs.existsSync(dirPath)) {
await fs.mkdir(dirPath);
}
};
export const saveStreamToDisk = async (
filePath: string,
fileStream: ReadableStream<Uint8Array>,
) => {
await writeStream(filePath, fileStream);
};
export const saveFileToDisk = async (path: string, fileData: string) => {
await fs.writeFile(path, fileData);
};

View file

@ -34,12 +34,6 @@ import * as fs from "promise-fs";
import { Readable } from "stream"; import { Readable } from "stream";
import { deleteDiskCache, openDiskCache } from "./api/cache"; import { deleteDiskCache, openDiskCache } from "./api/cache";
import { logToDisk, openLogDirectory } from "./api/common"; import { logToDisk, openLogDirectory } from "./api/common";
import {
checkExistsAndCreateDir,
exists,
saveFileToDisk,
saveStreamToDisk,
} from "./api/export";
import { runFFmpegCmd } from "./api/ffmpeg"; import { runFFmpegCmd } from "./api/ffmpeg";
import { getDirFiles } from "./api/fs"; import { getDirFiles } from "./api/fs";
import { convertToJPEG, generateImageThumbnail } from "./api/imageProcessor"; import { convertToJPEG, generateImageThumbnail } from "./api/imageProcessor";
@ -67,8 +61,12 @@ import {
} from "./api/watch"; } from "./api/watch";
import { setupLogging } from "./utils/logging"; import { setupLogging } from "./utils/logging";
/* Some of the code below has been duplicated to make this file self contained. /*
Enhancement: consider alternatives */ Some of the code below has been duplicated to make this file self contained
(see the documentation at the top of why it needs to be a single file).
Enhancement: consider alternatives
*/
/* preload: duplicated logError */ /* preload: duplicated logError */
export function logError(error: Error, message: string, info?: string): void { export function logError(error: Error, message: string, info?: string): void {
@ -133,6 +131,29 @@ export async function writeStream(
await writeNodeStream(filePath, readable); await writeNodeStream(filePath, readable);
} }
// - Export
export const exists = (path: string) => {
return fs.existsSync(path);
};
export const checkExistsAndCreateDir = async (dirPath: string) => {
if (!fs.existsSync(dirPath)) {
await fs.mkdir(dirPath);
}
};
export const saveStreamToDisk = async (
filePath: string,
fileStream: ReadableStream<Uint8Array>,
) => {
await writeStream(filePath, fileStream);
};
export const saveFileToDisk = async (path: string, fileData: string) => {
await fs.writeFile(path, fileData);
};
// - // -
async function readTextFile(filePath: string) { async function readTextFile(filePath: string) {
@ -211,7 +232,7 @@ function deleteFile(filePath: string): void {
fs.rmSync(filePath); fs.rmSync(filePath);
} }
// - // - ML
/* preload: duplicated Model */ /* preload: duplicated Model */
export enum Model { export enum Model {
@ -315,7 +336,7 @@ const parseExecError = (err: any) => {
} }
}; };
// - // - General
const selectDirectory = async (): Promise<string> => { const selectDirectory = async (): Promise<string> => {
try { try {
@ -349,7 +370,7 @@ const clearElectronStore = () => {
ipcRenderer.send("clear-electron-store"); ipcRenderer.send("clear-electron-store");
}; };
// - // - App update
const updateAndRestart = () => { const updateAndRestart = () => {
ipcRenderer.send("update-and-restart"); ipcRenderer.send("update-and-restart");
@ -410,10 +431,12 @@ setupLogging();
// running out of memory, causing the app to crash as it copies it over across // running out of memory, causing the app to crash as it copies it over across
// the processes. // the processes.
contextBridge.exposeInMainWorld("ElectronAPIs", { contextBridge.exposeInMainWorld("ElectronAPIs", {
// - Export
exists, exists,
checkExistsAndCreateDir, checkExistsAndCreateDir,
saveStreamToDisk, saveStreamToDisk,
saveFileToDisk, saveFileToDisk,
selectDirectory, selectDirectory,
clearElectronStore, clearElectronStore,
readTextFile, readTextFile,
@ -438,20 +461,29 @@ contextBridge.exposeInMainWorld("ElectronAPIs", {
updateWatchMappingIgnoredFiles, updateWatchMappingIgnoredFiles,
logToDisk, logToDisk,
convertToJPEG, convertToJPEG,
openLogDirectory,
registerUpdateEventListener, registerUpdateEventListener,
updateAndRestart,
skipAppUpdate,
getAppVersion,
runFFmpegCmd, runFFmpegCmd,
muteUpdateNotification,
generateImageThumbnail, generateImageThumbnail,
registerForegroundEventListener, registerForegroundEventListener,
openDirectory,
moveFile, moveFile,
deleteFolder, deleteFolder,
rename, rename,
deleteFile, deleteFile,
// General
getAppVersion,
openDirectory,
// Logging
openLogDirectory,
// - App update
updateAndRestart,
skipAppUpdate,
muteUpdateNotification,
// - ML
computeImageEmbedding, computeImageEmbedding,
computeTextEmbedding, computeTextEmbedding,
}); });