/
githubmirror
/
Motrix
Обзор
Документация
Войти
/
githubmirror
/
Motrix
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/utils/index.js
214 строк
5 KB
Dr_rOot
refactor: convert to arrow function
11 май 2023, 16:40
11 май 2023, 16:40
cc27cb3
Код
Авторство
О чём код?
import { resolve } from 'node:path' import { access, constants, existsSync, lstatSync } from 'node:fs' import { app, nativeTheme, shell } from 'electron' import is from 'electron-is' import { APP_THEME, ENGINE_MAX_CONNECTION_PER_SERVER, IP_VERSION, IS_PORTABLE, PORTABLE_EXECUTABLE_DIR } from '@shared/constants' import { engineBinMap, engineArchMap } from '../configs/engine' import logger from '../core/Logger' export const getUserDataPath = () => { return IS_PORTABLE ? PORTABLE_EXECUTABLE_DIR : app.getPath('userData') } export const getSystemLogPath = () => { return app.getPath('logs') } export const getUserDownloadsPath = () => { return app.getPath('downloads') } export const getConfigBasePath = () => { const path = getUserDataPath() return path } export const getSessionPath = () => { return resolve(getUserDataPath(), './download.session') } export const getEnginePidPath = () => { return resolve(getUserDataPath(), './engine.pid') } export const getDhtPath = (protocol) => { const name = protocol === IP_VERSION.V6 ? 'dht6.dat' : 'dht.dat' return resolve(getUserDataPath(), `./${name}`) } export const getEngineBin = (platform) => { const result = engineBinMap[platform] || '' return result } export const getEngineArch = (platform, arch) => { if (!['darwin', 'win32', 'linux'].includes(platform)) { return '' } const result = engineArchMap[platform][arch] return result } export const getDevEnginePath = (platform, arch) => { const ah = getEngineArch(platform, arch) const base = `../../../extra/${platform}/${ah}/engine` const result = resolve(__dirname, base) return result } export const getProdEnginePath = () => { return resolve(app.getAppPath(), '../engine') } export const getEnginePath = (platform, arch) => { return is.dev() ? getDevEnginePath(platform, arch) : getProdEnginePath() } export const getAria2BinPath = (platform, arch) => { const base = getEnginePath(platform, arch) const binName = getEngineBin(platform) const result = resolve(base, `./${binName}`) return result } export const getAria2ConfPath = (platform, arch) => { const base = getEnginePath(platform, arch) return resolve(base, './aria2.conf') } export const transformConfig = (config) => { const result = [] for (const [k, v] of Object.entries(config)) { if (v !== '') { result.push(`--${k}=${v}`) } } return result } export const isRunningInDmg = () => { if (!is.macOS() || is.dev()) { return false } const appPath = app.getAppPath() const result = appPath.startsWith('/Volumes/') return result } export const moveAppToApplicationsFolder = (errorMsg = '') => { return new Promise((resolve, reject) => { try { const result = app.moveToApplicationsFolder() if (result) { resolve(result) } else { reject(new Error(errorMsg)) } } catch (err) { reject(err) } }) } export const splitArgv = (argv) => { const args = [] const extra = {} for (const arg of argv) { if (arg.startsWith('--')) { const kv = arg.split('=') const key = kv[0] const value = kv[1] || '1' extra[key] = value continue } args.push(arg) } return { args, extra } } export const parseArgvAsUrl = (argv) => { const arg = argv[1] if (!arg) { return } if (checkIsSupportedSchema(arg)) { return arg } } export const checkIsSupportedSchema = (url = '') => { const str = url.toLowerCase() if ( str.startsWith('ftp:') || str.startsWith('http:') || str.startsWith('https:') || str.startsWith('magnet:') || str.startsWith('thunder:') || str.startsWith('mo:') || str.startsWith('motrix:') ) { return true } else { return false } } export const isDirectory = (path) => { return existsSync(path) && lstatSync(path).isDirectory() } export const parseArgvAsFile = (argv) => { let arg = argv[1] if (!arg || isDirectory(arg)) { return } if (is.linux()) { arg = arg.replace('file://', '') } return arg } export const getMaxConnectionPerServer = () => { return ENGINE_MAX_CONNECTION_PER_SERVER } export const getSystemTheme = () => { let result = APP_THEME.LIGHT result = nativeTheme.shouldUseDarkColors ? APP_THEME.DARK : APP_THEME.LIGHT return result } export const convertArrayBufferToBuffer = (arrayBuffer) => { const buffer = Buffer.alloc(arrayBuffer.byteLength) const view = new Uint8Array(arrayBuffer) for (let i = 0; i < buffer.length; ++i) { buffer[i] = view[i] } return buffer } export const showItemInFolder = (fullPath) => { if (!fullPath) { return } fullPath = resolve(fullPath) access(fullPath, constants.F_OK, (err) => { if (err) { logger.warn(`[Motrix] ${fullPath} ${err ? 'does not exist' : 'exists'}`) return } shell.showItemInFolder(fullPath) }) }