diff --git a/src/renderer/actions/paths.js b/src/renderer/actions/paths.js index da4b2738..5e07e570 100644 --- a/src/renderer/actions/paths.js +++ b/src/renderer/actions/paths.js @@ -5,13 +5,22 @@ import {remote} from "electron"; export const platforms = {stable: "Discord", ptb: "Discord PTB", canary: "Discord Canary"}; export const locations = {stable: "", ptb: "", canary: ""}; +const safeIsDir = (fullpath) => { + try { + return fs.lstatSync(fullpath).isDirectory(); + } + catch { + return false; + } +}; + const getDiscordPath = function(releaseChannel) { let resourcePath = ""; if (process.platform === "win32") { let basedir = path.join(process.env.LOCALAPPDATA, releaseChannel.replace(/ /g, "")); // Normal install path in AppData\Local if (!fs.existsSync(basedir)) basedir = path.join(process.env.PROGRAMDATA, process.env.USERNAME, releaseChannel.replace(/ /g, "")); // Atypical location in ProgramData\%username% if (!fs.existsSync(basedir)) return ""; - const version = fs.readdirSync(basedir).filter(f => fs.lstatSync(path.join(basedir, f)).isDirectory() && f.split(".").length > 1).sort().reverse()[0]; + const version = fs.readdirSync(basedir).filter(f => safeIsDir(path.join(basedir, f)) && f.split(".").length > 1).sort().reverse()[0]; if (!version) return ""; resourcePath = path.join(basedir, version, "resources"); } @@ -21,7 +30,7 @@ const getDiscordPath = function(releaseChannel) { else { const basedir = path.join(remote.app.getPath("userData"), "..", releaseChannel.toLowerCase().replace(" ", "")); if (!fs.existsSync(basedir)) return ""; - const version = fs.readdirSync(basedir).filter(f => fs.lstatSync(path.join(basedir, f)).isDirectory() && f.split(".").length > 1).sort().reverse()[0]; + const version = fs.readdirSync(basedir).filter(f => safeIsDir(path.join(basedir, f)) && f.split(".").length > 1).sort().reverse()[0]; if (!version) return ""; resourcePath = path.join(basedir, version, "modules", "discord_desktop_core"); } @@ -56,7 +65,7 @@ const validateWindows = function(channel, proposedPath) { const selected = path.basename(proposedPath); const isBaseDir = selected === channelName; if (isBaseDir) { - const version = fs.readdirSync(proposedPath).filter(f => fs.lstatSync(path.join(proposedPath, f)).isDirectory() && f.split(".").length > 1).sort().reverse()[0]; + const version = fs.readdirSync(proposedPath).filter(f => safeIsDir(path.join(proposedPath, f)) && f.split(".").length > 1).sort().reverse()[0]; if (!version) return ""; resourcePath = path.join(proposedPath, version, "resources"); } @@ -91,7 +100,7 @@ const validateLinux = function(channel, proposedPath) { let resourcePath = ""; const selected = path.basename(proposedPath); if (selected === channelName) { - const version = fs.readdirSync(proposedPath).filter(f => fs.lstatSync(path.join(proposedPath, f)).isDirectory() && f.split(".").length > 1).sort().reverse()[0]; + const version = fs.readdirSync(proposedPath).filter(f => safeIsDir(path.join(proposedPath, f)) && f.split(".").length > 1).sort().reverse()[0]; if (!version) return ""; resourcePath = path.join(proposedPath, version, "modules", "discord_desktop_core"); }