From ccd515e50c3f349fe3532f48ba5e606599af55de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillermo=20Vay=C3=A1?= Date: Mon, 19 Jul 2021 17:26:37 +0200 Subject: [PATCH] fix crash on invalid download paths (#1662) Co-authored-by: = <=> --- src/common/config/defaultPreferences.ts | 14 +++++--------- src/main/main.ts | 6 +++++- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/common/config/defaultPreferences.ts b/src/common/config/defaultPreferences.ts index 18c30d9a..b963fdb1 100644 --- a/src/common/config/defaultPreferences.ts +++ b/src/common/config/defaultPreferences.ts @@ -2,6 +2,9 @@ // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. +import path from 'path'; +import os from 'os'; + /** * Default user preferences. End-users can change these parameters by editing config.json * @param {number} version - Scheme version. (Not application version) @@ -9,15 +12,8 @@ import {ConfigV2} from 'types/config'; -export const getDefaultDownloadLocation = () => { - switch (process.platform) { - case 'darwin': - return `/Users/${process.env.USER || process.env.USERNAME}/Downloads`; - case 'win32': - return `C:\\Users\\${process.env.USER || process.env.USERNAME}\\Downloads`; - default: - return `/home/${process.env.USER || process.env.USERNAME}/Downloads`; - } +export const getDefaultDownloadLocation = (): string => { + return path.join(os.homedir(), 'Downloads'); }; const defaultPreferences: ConfigV2 = { diff --git a/src/main/main.ts b/src/main/main.ts index 3371b869..31a6aba7 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -282,7 +282,11 @@ function handleConfigSynchronize() { WindowManager.setConfig(config.data); setUnreadBadgeSetting(config.data.showUnreadBadge); if (config.data.downloadLocation) { - app.setPath('downloads', config.data.downloadLocation); + try { + app.setPath('downloads', config.data.downloadLocation); + } catch (e) { + log.error(`There was a problem trying to set the default download path: ${e}`); + } } if (app.isReady()) { WindowManager.sendToRenderer(RELOAD_CONFIGURATION);