Skip to content

A full-featured download manager.

refactor: motrix url protocol

Dr_rOotc9ea1dece2f2
diff --git a/src/main/Application.js b/src/main/Application.js
index bd3bac5a..1fccfb3d 100644
--- a/src/main/Application.js
+++ b/src/main/Application.js
@@ -410,7 +410,7 @@ export default class Application extends EventEmitter {
this.themeManager = new ThemeManager()
this.themeManager.on('system-theme-change', (theme) => {
this.trayManager.changeIconTheme(theme)
- this.sendCommandToAll('application:update-system-theme', theme)
+ this.sendCommandToAll('application:update-system-theme', { theme })
})
}
@@ -454,15 +454,17 @@ export default class Application extends EventEmitter {
this.show()
- const fileName = basename(filePath)
+ const name = basename(filePath)
readFile(filePath, (err, data) => {
if (err) {
logger.warn(`[Motrix] read file error: ${filePath}`, err.message)
return
}
- const file = Buffer.from(data).toString('base64')
- const args = [fileName, file]
- this.sendCommandToAll('application:new-bt-task-with-file', ...args)
+ const dataURL = Buffer.from(data).toString('base64')
+ this.sendCommandToAll('application:new-bt-task-with-file', {
+ name,
+ dataURL
+ })
})
}
@@ -557,11 +559,11 @@ export default class Application extends EventEmitter {
}
})
- this.on('application:show', (page) => {
+ this.on('application:show', ({ page }) => {
this.show(page)
})
- this.on('application:hide', (page) => {
+ this.on('application:hide', ({ page }) => {
this.hide(page)
})
@@ -576,7 +578,7 @@ export default class Application extends EventEmitter {
this.on('application:change-theme', (theme) => {
this.themeManager.updateAppAppearance(theme)
- this.sendCommandToAll('application:update-theme', theme)
+ this.sendCommandToAll('application:update-theme', { theme })
})
this.on('application:change-locale', (locale) => {
@@ -663,7 +665,7 @@ export default class Application extends EventEmitter {
}
handleConfigChange (configName) {
- this.sendCommandToAll('application:update-preference-config', configName)
+ this.sendCommandToAll('application:update-preference-config', { configName })
}
handleEvents () {
diff --git a/src/main/core/ProtocolManager.js b/src/main/core/ProtocolManager.js
index cfce2474..da900def 100644
--- a/src/main/core/ProtocolManager.js
+++ b/src/main/core/ProtocolManager.js
@@ -1,6 +1,6 @@
-
import { EventEmitter } from 'events'
import { app } from 'electron'
+import { parse } from 'querystring'
import logger from './Logger'
import protocolMap from '../configs/protocol'
@@ -63,12 +63,15 @@ export default class ProtocolManager extends EventEmitter {
return
}
- global.application.sendCommandToAll('application:new-task', ADD_TASK_TYPE.URI, url)
+ global.application.sendCommandToAll('application:new-task', {
+ type: ADD_TASK_TYPE.URI,
+ url
+ })
}
handleMoProtocol (url) {
const parsed = new URL(url)
- const { host } = parsed
+ const { host, search } = parsed
logger.info('[Motrix] protocol parsed:', parsed, host)
const command = protocolMap[host]
@@ -76,10 +79,8 @@ export default class ProtocolManager extends EventEmitter {
return
}
- // @TODO 没想明白怎么传参数好
- // 如果按顺序传递,那 url 的 query string 就要求有序的了
- // const query = queryString.parse(parsed.query)
- const args = []
- global.application.sendCommandToAll(command, ...args)
+ const query = search.startsWith('?') ? search.replace('?', '') : search
+ const args = parse(query)
+ global.application.sendCommandToAll(command, args)
}
}
diff --git a/src/main/menus/darwin.json b/src/main/menus/darwin.json
index 92886679..6e77dbf1 100644
--- a/src/main/menus/darwin.json
+++ b/src/main/menus/darwin.json
@@ -3,7 +3,7 @@
{
"id": "menu.app",
"submenu": [
- { "id": "app.about", "command": "application:about", "command-before": "application:show,index" },
+ { "id": "app.about", "command": "application:about", "command-before": "application:show?page=index" },
{ "type": "separator" },
{ "id": "app.preferences", "command": "application:preferences" },
{ "id": "app.check-for-updates", "command": "application:check-for-updates" },
@@ -17,9 +17,9 @@
{
"id": "menu.task",
"submenu": [
- { "id": "task.new-task", "command": "application:new-task", "command-after": "application:show,index" },
- { "id": "task.new-bt-task", "command": "application:new-bt-task", "command-arg": "torrent", "command-after": "application:show,index" },
- { "id": "task.open-file", "command": "application:open-file", "command-before": "application:show,index" },
+ { "id": "task.new-task", "command": "application:new-task", "command-after": "application:show?page=index" },
+ { "id": "task.new-bt-task", "command": "application:new-bt-task", "command-after": "application:show?page=index" },
+ { "id": "task.open-file", "command": "application:open-file", "command-before": "application:show?page=index" },
{ "type": "separator" },
{ "id": "task.pause-task", "command": "application:pause-task" },
{ "id": "task.resume-task", "command": "application:resume-task" },
diff --git a/src/main/menus/linux.json b/src/main/menus/linux.json
index 04d41697..99070013 100644
--- a/src/main/menus/linux.json
+++ b/src/main/menus/linux.json
@@ -3,11 +3,11 @@
{
"id": "menu.file",
"submenu": [
- { "id": "app.about", "command": "application:about", "command-before": "application:show,index" },
+ { "id": "app.about", "command": "application:about", "command-before": "application:show?page=index" },
{ "type": "separator" },
{ "id": "app.preferences", "command": "application:preferences" },
{ "id": "app.check-for-updates", "command": "application:check-for-updates" },
- { "id": "app.show", "command": "application:show", "command-arg": "index" },
+ { "id": "app.show", "command": "application:show", "command-arg": { "page": "index" } },
{ "type": "separator" },
{ "id": "app.quit", "command": "application:quit" }
]
@@ -15,9 +15,9 @@
{
"id": "menu.task",
"submenu": [
- { "id": "task.new-task", "command": "application:new-task", "command-after": "application:show,index" },
- { "id": "task.new-bt-task", "command": "application:new-bt-task", "command-arg": "torrent", "command-after": "application:show,index" },
- { "id": "task.open-file", "command": "application:open-file", "command-before": "application:show,index" },
+ { "id": "task.new-task", "command": "application:new-task", "command-after": "application:show?page=index" },
+ { "id": "task.new-bt-task", "command": "application:new-bt-task", "command-after": "application:show?page=index" },
+ { "id": "task.open-file", "command": "application:open-file", "command-before": "application:show?page=index" },
{ "type": "separator" },
{ "id": "task.pause-task", "command": "application:pause-task" },
{ "id": "task.resume-task", "command": "application:resume-task" },
diff --git a/src/main/menus/touchBar.json b/src/main/menus/touchBar.json
index 956a5863..bc254018 100644
--- a/src/main/menus/touchBar.json
+++ b/src/main/menus/touchBar.json
@@ -1,6 +1,6 @@
[
{
- "type": "button", "icon": "new-task", "id": "task.new-task", "command": "application:new-task", "command-arg": "uri", "command-after": "application:show,index"
+ "type": "button", "icon": "new-task", "id": "task.new-task", "command": "application:new-task", "command-after": "application:show?page=index"
},
{
"type": "spacer", "size": "small"
@@ -10,13 +10,13 @@
"id": "task.task-list",
"items": [
{
- "type": "button", "icon": "task-active", "command": "application:task-list", "command-arg": "active"
+ "type": "button", "icon": "task-active", "command": "application:task-list", "command-arg": { "status": "active" }
},
{
- "type": "button", "icon": "task-waiting", "command": "application:task-list", "command-arg": "waiting"
+ "type": "button", "icon": "task-waiting", "command": "application:task-list", "command-arg": { "status": "waiting" }
},
{
- "type": "button", "icon": "task-stopped", "command": "application:task-list", "command-arg": "stopped"
+ "type": "button", "icon": "task-stopped", "command": "application:task-list", "command-arg": { "status": "stopped" }
}
]
},
@@ -30,6 +30,6 @@
"type": "spacer", "size": "small"
},
{
- "type": "button", "icon": "about", "id": "app.about", "command": "application:about", "command-before": "application:show,index"
+ "type": "button", "icon": "about", "id": "app.about", "command": "application:about", "command-before": "application:show?page=index"
}
]
diff --git a/src/main/menus/tray.json b/src/main/menus/tray.json
index 7b2d97a3..c7bb318c 100644
--- a/src/main/menus/tray.json
+++ b/src/main/menus/tray.json
@@ -1,12 +1,12 @@
[
- { "id": "task.new-task", "command": "application:new-task", "command-after": "application:show,index" },
- { "id": "task.new-bt-task", "command": "application:new-bt-task", "command-arg": "torrent", "command-after": "application:show,index" },
- { "id": "task.open-file", "command": "application:open-file", "command-before": "application:show,index" },
+ { "id": "task.new-task", "command": "application:new-task", "command-after": "application:show?page=index" },
+ { "id": "task.new-bt-task", "command": "application:new-bt-task", "command-arg": { "type": "torrent" }, "command-after": "application:show?page=index" },
+ { "id": "task.open-file", "command": "application:open-file", "command-before": "application:show?page=index" },
{ "type": "separator" },
- { "id": "app.show", "command": "application:show", "command-arg": "index" },
+ { "id": "app.show", "command": "application:show", "command-arg": { "page": "index" } },
{ "id": "help.manual", "command": "help:manual" },
{ "id": "app.check-for-updates", "command": "application:check-for-updates" },
{ "type": "separator" },
- { "id": "app.preferences", "command": "application:preferences", "command-before": "application:show,index" },
+ { "id": "app.preferences", "command": "application:preferences", "command-before": "application:show?page=index" },
{ "id": "app.quit", "command": "application:quit" }
]
diff --git a/src/main/menus/win32.json b/src/main/menus/win32.json
index 65d4e362..17c37f00 100644
--- a/src/main/menus/win32.json
+++ b/src/main/menus/win32.json
@@ -3,11 +3,11 @@
{
"id": "menu.file",
"submenu": [
- { "id": "app.about", "command": "application:about", "command-before": "application:show,index" },
+ { "id": "app.about", "command": "application:about", "command-before": "application:show?page=index" },
{ "type": "separator" },
{ "id": "app.preferences", "command": "application:preferences" },
{ "id": "app.check-for-updates", "command": "application:check-for-updates" },
- { "id": "app.show", "command": "application:show", "command-arg": "index" },
+ { "id": "app.show", "command": "application:show", "command-arg": { "page": "index" } },
{ "type": "separator" },
{ "id": "app.quit", "command": "application:quit" }
]
@@ -15,9 +15,9 @@
{
"id": "menu.task",
"submenu": [
- { "id": "task.new-task", "command": "application:new-task", "command-after": "application:show,index" },
- { "id": "task.new-bt-task", "command": "application:new-bt-task", "command-arg": "torrent", "command-after": "application:show,index" },
- { "id": "task.open-file", "command": "application:open-file", "command-before": "application:show,index" },
+ { "id": "task.new-task", "command": "application:new-task", "command-after": "application:show?page=index" },
+ { "id": "task.new-bt-task", "command": "application:new-bt-task", "command-after": "application:show?page=index" },
+ { "id": "task.open-file", "command": "application:open-file", "command-before": "application:show?page=index" },
{ "type": "separator" },
{ "id": "task.pause-task", "command": "application:pause-task" },
{ "id": "task.resume-task", "command": "application:resume-task" },
diff --git a/src/main/utils/menu.js b/src/main/utils/menu.js
index 8f6b0038..01f33bae 100644
--- a/src/main/utils/menu.js
+++ b/src/main/utils/menu.js
@@ -1,3 +1,5 @@
+import { parse } from 'querystring'
+
export function concat (template, submenu, submenuToAdd) {
submenuToAdd.forEach(sub => {
let relativeItem = null
@@ -115,16 +117,18 @@ function handleCommandBefore (item) {
if (!item['command-before']) {
return
}
- const [command, ...args] = item['command-before'].split(',')
- global.application.sendCommandToAll(command, ...args)
+ const [command, params] = item['command-before'].split('?')
+ const args = parse(params)
+ global.application.sendCommandToAll(command, args)
}
function handleCommandAfter (item) {
if (!item['command-after']) {
return
}
- const [command, ...args] = item['command-after'].split(',')
- global.application.sendCommandToAll(command, ...args)
+ const [command, params] = item['command-after'].split('?')
+ const args = parse(params)
+ global.application.sendCommandToAll(command, args)
}
function acceleratorForCommand (command, keystrokesByCommand) {
diff --git a/src/renderer/pages/index/commands.js b/src/renderer/pages/index/commands.js
index e75856ca..389e12e3 100644
--- a/src/renderer/pages/index/commands.js
+++ b/src/renderer/pages/index/commands.js
@@ -7,14 +7,21 @@ import { buildFileList } from '@shared/utils'
import { ADD_TASK_TYPE } from '@shared/constants'
import { getLocaleManager } from '@/components/Locale'
import { commands } from '@/components/CommandManager/instance'
+import {
+ initialForm,
+ buildUriPayload,
+ buildTorrentPayload
+} from '@/utils/task'
const i18n = getLocaleManager().getI18n()
-const updateSystemTheme = (theme) => {
+const updateSystemTheme = (payload = {}) => {
+ const { theme } = payload
store.dispatch('app/updateSystemTheme', theme)
}
-const updateTheme = (theme) => {
+const updateTheme = (payload = {}) => {
+ const { theme } = payload
store.dispatch('preference/changeThemeConfig', theme)
}
@@ -22,28 +29,83 @@ const showAboutPanel = () => {
store.dispatch('app/showAboutPanel')
}
-const showAddTask = (taskType = ADD_TASK_TYPE.URI, uri = '') => {
- if (taskType === ADD_TASK_TYPE.URI && uri) {
+const addTask = (payload = {}) => {
+ const {
+ type = ADD_TASK_TYPE.URI,
+ uri,
+ silent,
+ ...rest
+ } = payload
+
+ const options = {
+ ...rest
+ }
+
+ if (type === ADD_TASK_TYPE.URI && uri) {
store.dispatch('app/updateAddTaskUrl', uri)
}
- store.dispatch('app/showAddTaskDialog', taskType)
+ store.dispatch('app/updateAddTaskOptions', options)
+
+ if (silent) {
+ addTaskSilent(type)
+ return
+ }
+
+ store.dispatch('app/showAddTaskDialog', type)
+}
+
+const addTaskSilent = (type) => {
+ try {
+ addTaskByType(type)
+ } catch (err) {
+ Message.error(i18n.t(err.message))
+ }
+}
+
+const addTaskByType = (type) => {
+ const form = initialForm(store.state)
+
+ let payload = null
+ if (type === ADD_TASK_TYPE.URI) {
+ payload = buildUriPayload(form)
+ store.dispatch('task/addUri', payload).catch(err => {
+ Message.error(err.message)
+ })
+ } else if (type === ADD_TASK_TYPE.TORRENT) {
+ payload = buildTorrentPayload(form)
+ store.dispatch('task/addTorrent', payload).catch(err => {
+ Message.error(err.message)
+ })
+ } else if (type === 'metalink') {
+ // @TODO addMetalink
+ } else {
+ console.error('addTask fail', form)
+ }
}
const showAddBtTask = () => {
store.dispatch('app/showAddTaskDialog', ADD_TASK_TYPE.TORRENT)
}
-const showAddBtTaskWithFile = (fileName, base64Data = '') => {
- const blob = base64StringToBlob(base64Data, 'application/x-bittorrent')
- const file = new File([blob], fileName, { type: 'application/x-bittorrent' })
+const showAddBtTaskWithFile = (payload = {}) => {
+ const { name, dataURL = '' } = payload
+ if (!dataURL) {
+ return
+ }
+
+ const blob = base64StringToBlob(dataURL, 'application/x-bittorrent')
+ const file = new File([blob], name, { type: 'application/x-bittorrent' })
const fileList = buildFileList(file)
+
store.dispatch('app/showAddTaskDialog', ADD_TASK_TYPE.TORRENT)
setTimeout(() => {
store.dispatch('app/addTaskAddTorrents', { fileList })
}, 200)
}
-const navigateTaskList = (status = 'active') => {
+const navigateTaskList = (payload = {}) => {
+ const { status = 'active' } = payload
+
router.push({ path: `/task/${status}` }).catch(err => {
console.log(err)
})
@@ -101,7 +163,7 @@ commands.register('application:task-list', navigateTaskList)
commands.register('application:preferences', navigatePreferences)
commands.register('application:about', showAboutPanel)
-commands.register('application:new-task', showAddTask)
+commands.register('application:new-task', addTask)
commands.register('application:new-bt-task', showAddBtTask)
commands.register('application:new-bt-task-with-file', showAddBtTaskWithFile)
commands.register('application:pause-task', pauseTask)