1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- import fs from "fs";
- import path from "path";
- import os from "os";
- import { DataPath, IElectronAPI } from "./../../src/types/globalExpose.d";
- import { contextBridge, ipcRenderer, shell } from "electron";
- import { isProd } from "../utils";
- interface NotificationData {
- title: string;
- body: string;
- }
- const getPlatform = () => {
- if (process.platform === "darwin") {
- return 4;
- }
- if (process.platform === "win32") {
- return 3;
- }
- return 7;
- };
- const getDataPath = (key: DataPath) => {
- switch (key) {
- case "public":
- return isProd ? ipcRenderer.sendSync("getDataPath", "public") : "";
- default:
- return "";
- }
- };
- const subscribe = (channel: string, callback: (...args: any[]) => void) => {
- const subscription = (_, ...args) => callback(...args);
- ipcRenderer.on(channel, subscription);
- return () => ipcRenderer.removeListener(channel, subscription);
- };
- const subscribeOnce = (channel: string, callback: (...args: any[]) => void) => {
- ipcRenderer.once(channel, (_, ...args) => callback(...args));
- };
- const unsubscribeAll = (channel: string) => {
- ipcRenderer.removeAllListeners(channel);
- };
- const ipcInvoke = (channel: string, ...arg: any) => {
- return ipcRenderer.invoke(channel, ...arg);
- };
- const ipcSendSync = (channel: string, ...arg: any) => {
- return ipcRenderer.sendSync(channel, ...arg);
- };
- const saveFileToDisk = async ({
- file,
- sync,
- }: {
- file: File;
- sync?: boolean;
- }): Promise<string> => {
- const arrayBuffer = await file.arrayBuffer();
- const saveDir = ipcRenderer.sendSync("getDataPath", "userData");
- const savePath = path.join(saveDir, file.name);
- if (!fs.existsSync(saveDir)) {
- fs.mkdirSync(saveDir, { recursive: true });
- }
- if (sync) {
- await fs.promises.writeFile(savePath, Buffer.from(arrayBuffer));
- } else {
- fs.promises.writeFile(savePath, Buffer.from(arrayBuffer));
- }
- return savePath;
- };
- const Api: IElectronAPI = {
- getDataPath,
- getVersion: () => process.version,
- getPlatform,
- getSystemVersion: process.getSystemVersion,
- subscribe,
- subscribeOnce,
- unsubscribeAll,
- ipcInvoke,
- ipcSendSync,
- saveFileToDisk,
- showNotification: (title: string, body: string): Promise<void> => {
- const data: NotificationData = { title, body };
- const content = JSON.parse(body)
- return ipcRenderer.invoke('show-notification', {
- title,
- body: content.textElem.content
- });
- },
- };
- contextBridge.exposeInMainWorld("electronAPI", Api);
|