Skip to content

A full-featured download manager.

refactor: portable config file base dir

Dr_rOotb8c57df4f52f
diff --git a/src/main/core/ConfigManager.js b/src/main/core/ConfigManager.js
index 339e6eb4..3adab314 100644
--- a/src/main/core/ConfigManager.js
+++ b/src/main/core/ConfigManager.js
@@ -3,9 +3,10 @@ import is from 'electron-is'
import Store from 'electron-store'
import {
+ getConfigBasePath,
getDhtPath,
- getUserDownloadsPath,
- getMaxConnectionPerServer
+ getMaxConnectionPerServer,
+ getUserDownloadsPath
} from '../utils/index'
import {
APP_RUN_MODE,
@@ -43,6 +44,7 @@ export default class ConfigManager {
initSystemConfig () {
this.systemConfig = new Store({
name: 'system',
+ cwd: getConfigBasePath(),
/* eslint-disable quote-props */
defaults: {
'all-proxy': EMPTY_STRING,
@@ -85,6 +87,7 @@ export default class ConfigManager {
initUserConfig () {
this.userConfig = new Store({
name: 'user',
+ cwd: getConfigBasePath(),
// Schema need electron-store upgrade to 3.x.x,
// but it will cause the application build to fail.
// schema: {
diff --git a/src/main/core/Context.js b/src/main/core/Context.js
index 99abd701..81900efc 100644
--- a/src/main/core/Context.js
+++ b/src/main/core/Context.js
@@ -3,7 +3,6 @@ import {
getEnginePath,
getAria2BinPath,
getAria2ConfPath,
- getLogPath,
getSessionPath
} from '../utils'
@@ -14,12 +13,17 @@ export default class Context {
this.init()
}
+ getLogPath () {
+ const { path } = logger.transports.file.getFile()
+ return path
+ }
+
init () {
// The key of Context cannot be the same as that of userConfig and systemConfig.
this.context = {
platform: platform,
arch: arch,
- 'log-path': getLogPath(),
+ 'log-path': this.getLogPath(),
'session-path': getSessionPath(),
'engine-path': getEnginePath(platform, arch),
'aria2-bin-path': getAria2BinPath(platform, arch),
diff --git a/src/main/core/Logger.js b/src/main/core/Logger.js
index 4638633e..3056cb6e 100644
--- a/src/main/core/Logger.js
+++ b/src/main/core/Logger.js
@@ -1,7 +1,16 @@
+import { join } from 'node:path'
import is from 'electron-is'
import logger from 'electron-log'
-logger.transports.file.level = is.production() ? 'info' : 'silly'
+import { IS_PORTABLE, PORTABLE_EXECUTABLE_DIR } from '@shared/constants'
+
+const level = is.production() ? 'info' : 'silly'
+logger.transports.file.level = level
+
+if (IS_PORTABLE) {
+ logger.transports.file.resolvePath = () => join(PORTABLE_EXECUTABLE_DIR, 'main.log')
+}
+
logger.info('[Motrix] Logger init')
logger.warn('[Motrix] Logger init')
diff --git a/src/main/utils/index.js b/src/main/utils/index.js
index 374d9b88..5494faa5 100644
--- a/src/main/utils/index.js
+++ b/src/main/utils/index.js
@@ -6,34 +6,41 @@ import is from 'electron-is'
import {
APP_THEME,
ENGINE_MAX_CONNECTION_PER_SERVER,
- IP_VERSION
+ IP_VERSION,
+ IS_PORTABLE,
+ PORTABLE_EXECUTABLE_DIR
} from '@shared/constants'
import { engineBinMap, engineArchMap } from '../configs/engine'
import logger from '../core/Logger'
-export function getLogPath () {
+export const getUserDataPath = () => {
+ return IS_PORTABLE ? PORTABLE_EXECUTABLE_DIR : app.getPath('userData')
+}
+
+export const getSystemLogPath = () => {
return app.getPath('logs')
}
-export function getDhtPath (protocol) {
- const name = protocol === IP_VERSION.V6 ? 'dht6.dat' : 'dht.dat'
- return resolve(app.getPath('userData'), `./${name}`)
+export const getUserDownloadsPath = () => {
+ return app.getPath('downloads')
}
-export function getSessionPath () {
- return resolve(app.getPath('userData'), './download.session')
+export const getConfigBasePath = () => {
+ const path = getUserDataPath()
+ return path
}
-export function getEnginePidPath () {
- return resolve(app.getPath('userData'), './engine.pid')
+export const getSessionPath = () => {
+ return resolve(getUserDataPath(), './download.session')
}
-export function getUserDataPath () {
- return app.getPath('userData')
+export const getEnginePidPath = () => {
+ return resolve(getUserDataPath(), './engine.pid')
}
-export function getUserDownloadsPath () {
- return app.getPath('downloads')
+export const getDhtPath = (protocol) => {
+ const name = protocol === IP_VERSION.V6 ? 'dht6.dat' : 'dht.dat'
+ return resolve(getUserDataPath(), `./${name}`)
}
export function getEngineBin (platform) {
diff --git a/src/shared/constants.js b/src/shared/constants.js
index d7fb4077..f663ad24 100644
--- a/src/shared/constants.js
+++ b/src/shared/constants.js
@@ -1,4 +1,6 @@
export const EMPTY_STRING = ''
+export const PORTABLE_EXECUTABLE_DIR = process.env.PORTABLE_EXECUTABLE_DIR
+export const IS_PORTABLE = PORTABLE_EXECUTABLE_DIR && PORTABLE_EXECUTABLE_DIR !== EMPTY_STRING
export const APP_THEME = {
AUTO: 'auto',