index.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import fs from "fs";
  2. import path from "path";
  3. import os from "os";
  4. import { DataPath, IElectronAPI } from "./../../src/types/globalExpose.d";
  5. import { contextBridge, ipcRenderer, shell } from "electron";
  6. import { isProd } from "../utils";
  7. interface NotificationData {
  8. title: string;
  9. body: string;
  10. }
  11. const getPlatform = () => {
  12. if (process.platform === "darwin") {
  13. return 4;
  14. }
  15. if (process.platform === "win32") {
  16. return 3;
  17. }
  18. return 7;
  19. };
  20. const getDataPath = (key: DataPath) => {
  21. switch (key) {
  22. case "public":
  23. return isProd ? ipcRenderer.sendSync("getDataPath", "public") : "";
  24. default:
  25. return "";
  26. }
  27. };
  28. const subscribe = (channel: string, callback: (...args: any[]) => void) => {
  29. const subscription = (_, ...args) => callback(...args);
  30. ipcRenderer.on(channel, subscription);
  31. return () => ipcRenderer.removeListener(channel, subscription);
  32. };
  33. const subscribeOnce = (channel: string, callback: (...args: any[]) => void) => {
  34. ipcRenderer.once(channel, (_, ...args) => callback(...args));
  35. };
  36. const unsubscribeAll = (channel: string) => {
  37. ipcRenderer.removeAllListeners(channel);
  38. };
  39. const ipcInvoke = (channel: string, ...arg: any) => {
  40. return ipcRenderer.invoke(channel, ...arg);
  41. };
  42. const ipcSendSync = (channel: string, ...arg: any) => {
  43. return ipcRenderer.sendSync(channel, ...arg);
  44. };
  45. const saveFileToDisk = async ({
  46. file,
  47. sync,
  48. }: {
  49. file: File;
  50. sync?: boolean;
  51. }): Promise<string> => {
  52. const arrayBuffer = await file.arrayBuffer();
  53. const saveDir = ipcRenderer.sendSync("getDataPath", "userData");
  54. const savePath = path.join(saveDir, file.name);
  55. if (!fs.existsSync(saveDir)) {
  56. fs.mkdirSync(saveDir, { recursive: true });
  57. }
  58. if (sync) {
  59. await fs.promises.writeFile(savePath, Buffer.from(arrayBuffer));
  60. } else {
  61. fs.promises.writeFile(savePath, Buffer.from(arrayBuffer));
  62. }
  63. return savePath;
  64. };
  65. const Api: IElectronAPI = {
  66. getDataPath,
  67. getVersion: () => process.version,
  68. getPlatform,
  69. getSystemVersion: process.getSystemVersion,
  70. subscribe,
  71. subscribeOnce,
  72. unsubscribeAll,
  73. ipcInvoke,
  74. ipcSendSync,
  75. saveFileToDisk,
  76. showNotification: (title: string, body: string): Promise<void> => {
  77. const data: NotificationData = { title, body };
  78. const content = JSON.parse(body)
  79. return ipcRenderer.invoke('show-notification', {
  80. title,
  81. body: content.textElem.content
  82. });
  83. },
  84. };
  85. contextBridge.exposeInMainWorld("electronAPI", Api);