From 466488c7da3cff61f23e150fc4a04491db66b197 Mon Sep 17 00:00:00 2001 From: Allen Ding Date: Wed, 24 Jul 2024 19:49:25 -0700 Subject: [PATCH] copy file to vesktop folder instead of storing full path --- src/main/constants.ts | 1 + src/main/index.ts | 6 ++++-- src/main/ipc.ts | 19 +++++++++++++++---- src/preload/VesktopNative.ts | 2 +- .../settings/CustomSplashAnimation.tsx | 17 +++++------------ 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/main/constants.ts b/src/main/constants.ts index 40d91a5..d03b25e 100644 --- a/src/main/constants.ts +++ b/src/main/constants.ts @@ -44,6 +44,7 @@ export const VENCORD_SETTINGS_DIR = join(DATA_DIR, "settings"); export const VENCORD_QUICKCSS_FILE = join(VENCORD_SETTINGS_DIR, "quickCss.css"); export const VENCORD_SETTINGS_FILE = join(VENCORD_SETTINGS_DIR, "settings.json"); export const VENCORD_THEMES_DIR = join(DATA_DIR, "themes"); +export const VESKTOP_SPLASH_DIR = join(DATA_DIR, "splash"); // needs to be inline require because of circular dependency // as otherwise "DATA_DIR" (which is used by ./settings) will be uninitialised diff --git a/src/main/index.ts b/src/main/index.ts index f8ba285..347bba1 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -6,10 +6,11 @@ import "./ipc"; +import { join } from "path"; import { app, BrowserWindow, nativeTheme, net, protocol, session } from "electron"; import { autoUpdater } from "electron-updater"; -import { DATA_DIR } from "./constants"; +import { DATA_DIR, VESKTOP_SPLASH_DIR } from "./constants"; import { createFirstLaunchTour } from "./firstLaunch"; import { createWindows, mainWin } from "./mainWindow"; import { registerMediaPermissionsHandler } from "./mediaPermissions"; @@ -84,7 +85,8 @@ function init() { //register file handler so we can load the custom splash animation from the user's filesystem protocol.handle("splash-animation", () => { const { splashAnimationPath } = Settings.store; - return net.fetch("file:///"+splashAnimationPath); + const fullPath = join(VESKTOP_SPLASH_DIR, splashAnimationPath as string); + return net.fetch("file:///"+fullPath); }); //this patches the discord csp to allow the splash-animation:// protocol diff --git a/src/main/ipc.ts b/src/main/ipc.ts index e9137c0..fc0f23a 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -9,15 +9,16 @@ if (process.platform === "linux") import("./venmic"); import { execFile } from "child_process"; import { app, BrowserWindow, clipboard, dialog, nativeImage, RelaunchOptions, session, shell } from "electron"; import { mkdirSync, readFileSync, watch } from "fs"; -import { open, readFile } from "fs/promises"; +import { open, readFile, copyFile, mkdir, rmdir } from "fs/promises"; import { release } from "os"; -import { join } from "path"; +import { randomBytes } from "crypto"; +import { join, extname } from "path"; import { debounce } from "shared/utils/debounce"; import { IpcEvents } from "../shared/IpcEvents"; import { setBadgeCount } from "./appBadge"; import { autoStart } from "./autoStart"; -import { VENCORD_FILES_DIR, VENCORD_QUICKCSS_FILE, VENCORD_THEMES_DIR } from "./constants"; +import { VENCORD_FILES_DIR, VENCORD_QUICKCSS_FILE, VENCORD_THEMES_DIR, VESKTOP_SPLASH_DIR } from "./constants"; import { mainWin } from "./mainWindow"; import { Settings, State } from "./settings"; import { handle, handleSync } from "./utils/ipcWrappers"; @@ -134,7 +135,17 @@ handle(IpcEvents.SELECT_IMAGE_PATH, async () => { ] }); if (!res.filePaths.length) return "cancelled"; - return res.filePaths[0]; + + const originalPath = res.filePaths[0]; + const uuid = randomBytes(16).toString("hex"); + const imageName = "splash_" + uuid + extname(originalPath); + const destPath = join(VESKTOP_SPLASH_DIR, imageName); + + await rmdir(VESKTOP_SPLASH_DIR, {recursive: true}) + await mkdir(VESKTOP_SPLASH_DIR, {recursive: true}); + await copyFile(originalPath, destPath); + + return imageName; }); handle(IpcEvents.SET_BADGE_COUNT, (_, count: number) => setBadgeCount(count)); diff --git a/src/preload/VesktopNative.ts b/src/preload/VesktopNative.ts index 7aff481..824f66b 100644 --- a/src/preload/VesktopNative.ts +++ b/src/preload/VesktopNative.ts @@ -35,7 +35,7 @@ export const VesktopNative = { showItemInFolder: (path: string) => invoke(IpcEvents.SHOW_ITEM_IN_FOLDER, path), getVencordDir: () => sendSync(IpcEvents.GET_VENCORD_DIR), selectVencordDir: (value?: null) => invoke<"cancelled" | "invalid" | "ok">(IpcEvents.SELECT_VENCORD_DIR, value), - selectImagePath: () => invoke>(IpcEvents.SELECT_IMAGE_PATH) + selectImagePath: () => invoke<"cancelled" | string>(IpcEvents.SELECT_IMAGE_PATH) }, settings: { get: () => sendSync(IpcEvents.GET_SETTINGS), diff --git a/src/renderer/components/settings/CustomSplashAnimation.tsx b/src/renderer/components/settings/CustomSplashAnimation.tsx index 578bce1..e3e144f 100644 --- a/src/renderer/components/settings/CustomSplashAnimation.tsx +++ b/src/renderer/components/settings/CustomSplashAnimation.tsx @@ -21,17 +21,7 @@ export const CustomSplashAnimation: SettingsComponent = ({ settings }) => { }}> {/* adding the Math.random() here ensures that a new image is fetched when the user changes the path */} -

The custom splash animation is enabled. It is loaded from - { - e.preventDefault(); - VesktopNative.fileManager.showItemInFolder(settings.splashAnimationPath!); - }} - > - {" " + settings.splashAnimationPath} - -

+

The custom splash animation is enabled.

) : ( "A custom splash animation is not set." @@ -51,7 +41,10 @@ export const CustomSplashAnimation: SettingsComponent = ({ settings }) => {