From ffc8de4766556e46e1aa97a69d2a426768e0af4e Mon Sep 17 00:00:00 2001 From: Dr_rOot Date: Fri, 29 May 2020 16:58:35 +0800 Subject: [PATCH 1/6] refactor: add task utils --- src/renderer/components/Task/AddTask.vue | 129 ++--------------------- src/renderer/utils/task.js | 129 +++++++++++++++++++++++ 2 files changed, 139 insertions(+), 119 deletions(-) create mode 100644 src/renderer/utils/task.js diff --git a/src/renderer/components/Task/AddTask.vue b/src/renderer/components/Task/AddTask.vue index b2e15ffa4..09b14772a 100644 --- a/src/renderer/components/Task/AddTask.vue +++ b/src/renderer/components/Task/AddTask.vue @@ -173,40 +173,15 @@ import SelectDirectory from '@/components/Native/SelectDirectory' import SelectTorrent from '@/components/Task/SelectTorrent' import { prettifyDir } from '@/utils/native' - import { ADD_TASK_TYPE, NONE_SELECTED_FILES, SELECTED_ALL_FILES } from '@shared/constants' - import { detectResource, splitTaskLinks } from '@shared/utils' - import { buildOuts } from '@shared/utils/rename' + import { + initialForm, + buildUriPayload, + buildTorrentPayload + } from '@/utils/task' + import { ADD_TASK_TYPE } from '@shared/constants' + import { detectResource } from '@shared/utils' import '@/components/Icons/inbox' - const initialForm = state => { - const { addTaskUrl, addTaskOptions } = state.app - const { - allProxy, - dir, - engineMaxConnectionPerServer, - maxConnectionPerServer, - newTaskShowDownloading, - split - } = state.preference.config - const result = { - allProxy, - cookie: '', - dir, - engineMaxConnectionPerServer, - maxConnectionPerServer, - newTaskShowDownloading, - out: '', - referer: '', - selectFile: NONE_SELECTED_FILES, - split, - torrent: '', - uris: addTaskUrl, - userAgent: '', - ...addTaskOptions - } - return result - } - export default { name: 'mo-add-task', components: { @@ -336,99 +311,15 @@ this.showAdvanced = false this.form = initialForm(this.$store.state) }, - buildHeader (form) { - const { userAgent, referer, cookie } = form - const result = [] - - if (!isEmpty(userAgent)) { - result.push(`User-Agent: ${userAgent}`) - } - if (!isEmpty(referer)) { - result.push(`Referer: ${referer}`) - } - if (!isEmpty(cookie)) { - result.push(`Cookie: ${cookie}`) - } - return result - }, - buildOption (type, form) { - const { - allProxy, - dir, - out, - selectFile, - split - } = form - const result = {} - - if (!isEmpty(allProxy)) { - result.allProxy = allProxy - } - - if (!isEmpty(dir)) { - result.dir = dir - } - - if (!isEmpty(out)) { - result.out = out - } - - if (split > 0) { - result.split = split - } - - if (type === ADD_TASK_TYPE.TORRENT) { - if ( - selectFile !== SELECTED_ALL_FILES && - selectFile !== NONE_SELECTED_FILES - ) { - result.selectFile = selectFile - } - } - - const header = this.buildHeader(form) - if (!isEmpty(header)) { - result.header = header - } - return result - }, - buildUriPayload (form) { - let { uris, out } = form - if (isEmpty(uris)) { - throw new Error(this.$t('task.new-task-uris-required')) - } - uris = splitTaskLinks(uris) - const outs = buildOuts(uris, out) - - const options = this.buildOption(ADD_TASK_TYPE.URI, form) - const result = { - uris, - outs, - options - } - return result - }, - buildTorrentPayload (form) { - const { torrent } = form - if (isEmpty(torrent)) { - throw new Error(this.$t('task.new-task-torrent-required')) - } - const options = this.buildOption(ADD_TASK_TYPE.TORRENT, form) - const result = { - torrent, - options - } - return result - }, addTask (type, form) { let payload = null if (type === ADD_TASK_TYPE.URI) { - payload = this.buildUriPayload(form) + payload = buildUriPayload(form) this.$store.dispatch('task/addUri', payload).catch(err => { this.$msg.error(err.message) }) } else if (type === ADD_TASK_TYPE.TORRENT) { - payload = this.buildTorrentPayload(form) + payload = buildTorrentPayload(form) this.$store.dispatch('task/addTorrent', payload).catch(err => { this.$msg.error(err.message) }) @@ -456,7 +347,7 @@ }) } } catch (err) { - this.$msg.error(err.message) + this.$msg.error(this.$t(err.message)) } }) } diff --git a/src/renderer/utils/task.js b/src/renderer/utils/task.js new file mode 100644 index 000000000..32bae95db --- /dev/null +++ b/src/renderer/utils/task.js @@ -0,0 +1,129 @@ +import { isEmpty } from 'lodash' + +import { + ADD_TASK_TYPE, + NONE_SELECTED_FILES, + SELECTED_ALL_FILES +} from '@shared/constants' +import { splitTaskLinks } from '@shared/utils' +import { buildOuts } from '@shared/utils/rename' + +export const initialForm = state => { + const { addTaskUrl, addTaskOptions } = state.app + const { + allProxy, + dir, + engineMaxConnectionPerServer, + maxConnectionPerServer, + newTaskShowDownloading, + split + } = state.preference.config + const result = { + allProxy, + cookie: '', + dir, + engineMaxConnectionPerServer, + maxConnectionPerServer, + newTaskShowDownloading, + out: '', + referer: '', + selectFile: NONE_SELECTED_FILES, + split, + torrent: '', + uris: addTaskUrl, + userAgent: '', + ...addTaskOptions + } + return result +} + +export const buildHeader = (form) => { + const { userAgent, referer, cookie } = form + const result = [] + + if (!isEmpty(userAgent)) { + result.push(`User-Agent: ${userAgent}`) + } + if (!isEmpty(referer)) { + result.push(`Referer: ${referer}`) + } + if (!isEmpty(cookie)) { + result.push(`Cookie: ${cookie}`) + } + + return result +} + +export const buildOption = (type, form) => { + const { + allProxy, + dir, + out, + selectFile, + split + } = form + const result = {} + + if (!isEmpty(allProxy)) { + result.allProxy = allProxy + } + + if (!isEmpty(dir)) { + result.dir = dir + } + + if (!isEmpty(out)) { + result.out = out + } + + if (split > 0) { + result.split = split + } + + if (type === ADD_TASK_TYPE.TORRENT) { + if ( + selectFile !== SELECTED_ALL_FILES && + selectFile !== NONE_SELECTED_FILES + ) { + result.selectFile = selectFile + } + } + + const header = buildHeader(form) + if (!isEmpty(header)) { + result.header = header + } + return result +} + +export const buildUriPayload = (form) => { + let { uris, out } = form + if (isEmpty(uris)) { + throw new Error('task.new-task-uris-required') + } + + uris = splitTaskLinks(uris) + const outs = buildOuts(uris, out) + + const options = buildOption(ADD_TASK_TYPE.URI, form) + const result = { + uris, + outs, + options + } + return result +} + +export const buildTorrentPayload = (form) => { + const { torrent } = form + if (isEmpty(torrent)) { + throw new Error('task.new-task-torrent-required') + } + + const options = buildOption(ADD_TASK_TYPE.TORRENT, form) + const result = { + torrent, + options + } + return result +} From 60d4108ddc68cea895613a4a17107b363c5dda15 Mon Sep 17 00:00:00 2001 From: Dr_rOot Date: Fri, 29 May 2020 17:00:47 +0800 Subject: [PATCH 2/6] refactor: promise all to allSettled --- src/main/Application.js | 6 +++--- src/renderer/components/Task/Index.vue | 4 ++-- src/renderer/utils/native.js | 5 ++--- src/shared/utils/tracker.js | 5 +++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/Application.js b/src/main/Application.js index 3638fd6bf..bd3bac5ad 100644 --- a/src/main/Application.js +++ b/src/main/Application.js @@ -177,7 +177,7 @@ export default class Application extends EventEmitter { this.upnp.map(dhtPort) ] try { - await Promise.all(promises) + await Promise.allSettled(promises) } catch (e) { logger.warn('[Motrix] start UPnP mapping fail', e) } @@ -192,7 +192,7 @@ export default class Application extends EventEmitter { this.upnp.unmap(dhtPort) ] try { - await Promise.all(promises) + await Promise.allSettled(promises) } catch (e) { logger.warn('[Motrix] stop UPnP mapping fail', e) } @@ -214,7 +214,7 @@ export default class Application extends EventEmitter { this.upnp.map(newValue) ] try { - await Promise.all(promises) + await Promise.allSettled(promises) } catch (e) { logger.info('[Motrix] change UPnP port mapping failed:', e) } diff --git a/src/renderer/components/Task/Index.vue b/src/renderer/components/Task/Index.vue index 80f5edf96..c0546c576 100644 --- a/src/renderer/components/Task/Index.vue +++ b/src/renderer/components/Task/Index.vue @@ -213,8 +213,8 @@ }, batchDeleteTaskFiles (taskList) { const promises = taskList.map((task, index) => delayDeleteTaskFiles(task, index * 200)) - Promise.all(promises).then(values => { - console.log('[Motrix] batch delete task files: ', values) + Promise.allSettled(promises).then(results => { + console.log('[Motrix] batch delete task files: ', results) }) }, removeTaskItems (gids) { diff --git a/src/renderer/utils/native.js b/src/renderer/utils/native.js index 2c5ea1a39..c011b0594 100644 --- a/src/renderer/utils/native.js +++ b/src/renderer/utils/native.js @@ -152,14 +152,13 @@ export const openExternal = (url, options) => { } export const delayDeleteTaskFiles = (task, delay) => { - return new Promise((resolve) => { + return new Promise((resolve, reject) => { setTimeout(() => { try { const result = moveTaskFilesToTrash(task) resolve(result) } catch (err) { - console.log('[Motrix] batch delay delete task files fail', err) - resolve(false) + reject(err.message) } }, delay) }) diff --git a/src/shared/utils/tracker.js b/src/shared/utils/tracker.js index 2205d241a..6f1b26f60 100644 --- a/src/shared/utils/tracker.js +++ b/src/shared/utils/tracker.js @@ -15,8 +15,9 @@ export const fetchBtTrackerFromSource = async (source) => { return axios.get(`${url}?t=${now}`).then((value) => value.data) }) - const resp = await Promise.all(promises) - const result = [...new Set(resp)] + const results = await Promise.allSettled(promises) + const values = results.map((item) => item.value) + const result = [...new Set(values)] return result } From c9ea1dece2f2365e910821c12dc147d32b9526a1 Mon Sep 17 00:00:00 2001 From: Dr_rOot Date: Fri, 29 May 2020 17:02:43 +0800 Subject: [PATCH 3/6] refactor: motrix url protocol --- src/main/Application.js | 20 ++++--- src/main/core/ProtocolManager.js | 17 +++--- src/main/menus/darwin.json | 8 +-- src/main/menus/linux.json | 10 ++-- src/main/menus/touchBar.json | 10 ++-- src/main/menus/tray.json | 10 ++-- src/main/menus/win32.json | 10 ++-- src/main/utils/menu.js | 12 ++-- src/renderer/pages/index/commands.js | 82 ++++++++++++++++++++++++---- 9 files changed, 124 insertions(+), 55 deletions(-) diff --git a/src/main/Application.js b/src/main/Application.js index bd3bac5ad..1fccfb3da 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 cfce24747..da900def6 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 928866790..6e77dbf11 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 04d416977..990700134 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 956a58636..bc2540187 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 7b2d97a3f..c7bb318ce 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 65d4e362d..17c37f00d 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 8f6b0038d..01f33bae2 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 e75856cae..389e12e34 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) From e5241d09d70d01b788dd504b63a24375c9234c73 Mon Sep 17 00:00:00 2001 From: Dr_rOot Date: Fri, 29 May 2020 17:28:03 +0800 Subject: [PATCH 4/6] refactor: protocol manager init --- src/main/Application.js | 8 -------- src/main/core/ProtocolManager.js | 5 +++++ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/main/Application.js b/src/main/Application.js index 1fccfb3da..fdf793064 100644 --- a/src/main/Application.js +++ b/src/main/Application.js @@ -423,10 +423,6 @@ export default class Application extends EventEmitter { } initProtocolManager () { - if (is.dev() || is.mas()) { - return - } - const protocols = this.configManager.getUserConfig('protocols', {}) this.protocolManager = new ProtocolManager({ protocols @@ -434,10 +430,6 @@ export default class Application extends EventEmitter { } handleProtocol (url) { - if (is.dev() || is.mas()) { - return - } - this.show() this.protocolManager.handle(url) diff --git a/src/main/core/ProtocolManager.js b/src/main/core/ProtocolManager.js index da900def6..6cd2bbfe4 100644 --- a/src/main/core/ProtocolManager.js +++ b/src/main/core/ProtocolManager.js @@ -1,5 +1,6 @@ import { EventEmitter } from 'events' import { app } from 'electron' +import is from 'electron-is' import { parse } from 'querystring' import logger from './Logger' @@ -28,6 +29,10 @@ export default class ProtocolManager extends EventEmitter { } setup (protocols) { + if (is.dev() || is.mas()) { + return + } + Object.keys(protocols).forEach((protocol) => { const enabled = protocols[protocol] if (enabled) { From cf3ef7606cbd99184bd97b74dfb4c81911e5fc98 Mon Sep 17 00:00:00 2001 From: Dr_rOot Date: Sat, 6 Jun 2020 23:31:24 +0800 Subject: [PATCH 5/6] fix: new task uri --- src/main/core/ProtocolManager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/core/ProtocolManager.js b/src/main/core/ProtocolManager.js index 6cd2bbfe4..fa3e6ac66 100644 --- a/src/main/core/ProtocolManager.js +++ b/src/main/core/ProtocolManager.js @@ -70,7 +70,7 @@ export default class ProtocolManager extends EventEmitter { global.application.sendCommandToAll('application:new-task', { type: ADD_TASK_TYPE.URI, - url + uri: url }) } From b8507570c621c882d46fb8886b16faff94667a9a Mon Sep 17 00:00:00 2001 From: Dr_rOot Date: Fri, 12 Jun 2020 17:22:38 +0800 Subject: [PATCH 6/6] refactor: rename initialForm to initForm --- src/renderer/components/Preference/Advanced.vue | 6 +++--- src/renderer/components/Preference/Basic.vue | 6 +++--- src/renderer/components/Task/AddTask.vue | 6 +++--- src/renderer/pages/index/commands.js | 4 ++-- src/renderer/utils/task.js | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/renderer/components/Preference/Advanced.vue b/src/renderer/components/Preference/Advanced.vue index 3b0c3748e..921431831 100644 --- a/src/renderer/components/Preference/Advanced.vue +++ b/src/renderer/components/Preference/Advanced.vue @@ -341,7 +341,7 @@ import '@/components/Icons/sync' import '@/components/Icons/refresh' - const initialForm = (config) => { + const initForm = (config) => { const { allProxy, allProxyBackup, @@ -395,7 +395,7 @@ }, data () { const { locale } = this.$store.state.preference.config - const form = initialForm(this.$store.state.preference.config) + const form = initForm(this.$store.state.preference.config) const formOriginal = cloneDeep(form) return { @@ -524,7 +524,7 @@ syncFormConfig () { this.$store.dispatch('preference/fetchPreference') .then((config) => { - this.form = initialForm(config) + this.form = initForm(config) this.formOriginal = cloneDeep(this.form) }) }, diff --git a/src/renderer/components/Preference/Basic.vue b/src/renderer/components/Preference/Basic.vue index b2650d7a8..451db8f67 100644 --- a/src/renderer/components/Preference/Basic.vue +++ b/src/renderer/components/Preference/Basic.vue @@ -217,7 +217,7 @@ } from '@shared/utils' import { APP_RUN_MODE } from '@shared/constants' - const initialForm = (config) => { + const initForm = (config) => { const { autoHideWindow, dir, @@ -269,7 +269,7 @@ }, data () { const { locale } = this.$store.state.preference.config - const form = initialForm(this.$store.state.preference.config) + const form = initForm(this.$store.state.preference.config) const formOriginal = cloneDeep(form) return { @@ -379,7 +379,7 @@ syncFormConfig () { this.$store.dispatch('preference/fetchPreference') .then((config) => { - this.form = initialForm(config) + this.form = initForm(config) this.formOriginal = cloneDeep(this.form) }) }, diff --git a/src/renderer/components/Task/AddTask.vue b/src/renderer/components/Task/AddTask.vue index 09b14772a..c87092401 100644 --- a/src/renderer/components/Task/AddTask.vue +++ b/src/renderer/components/Task/AddTask.vue @@ -174,7 +174,7 @@ import SelectTorrent from '@/components/Task/SelectTorrent' import { prettifyDir } from '@/utils/native' import { - initialForm, + initTaskForm, buildUriPayload, buildTorrentPayload } from '@/utils/task' @@ -254,7 +254,7 @@ } }, handleOpen () { - this.form = initialForm(this.$store.state) + this.form = initTaskForm(this.$store.state) if (this.taskType === ADD_TASK_TYPE.URI) { this.autofillResourceLink() setTimeout(() => { @@ -309,7 +309,7 @@ }, reset () { this.showAdvanced = false - this.form = initialForm(this.$store.state) + this.form = initTaskForm(this.$store.state) }, addTask (type, form) { let payload = null diff --git a/src/renderer/pages/index/commands.js b/src/renderer/pages/index/commands.js index 389e12e34..c6a834cc8 100644 --- a/src/renderer/pages/index/commands.js +++ b/src/renderer/pages/index/commands.js @@ -8,7 +8,7 @@ import { ADD_TASK_TYPE } from '@shared/constants' import { getLocaleManager } from '@/components/Locale' import { commands } from '@/components/CommandManager/instance' import { - initialForm, + initTaskForm, buildUriPayload, buildTorrentPayload } from '@/utils/task' @@ -63,7 +63,7 @@ const addTaskSilent = (type) => { } const addTaskByType = (type) => { - const form = initialForm(store.state) + const form = initTaskForm(store.state) let payload = null if (type === ADD_TASK_TYPE.URI) { diff --git a/src/renderer/utils/task.js b/src/renderer/utils/task.js index 32bae95db..afb4e602a 100644 --- a/src/renderer/utils/task.js +++ b/src/renderer/utils/task.js @@ -8,7 +8,7 @@ import { import { splitTaskLinks } from '@shared/utils' import { buildOuts } from '@shared/utils/rename' -export const initialForm = state => { +export const initTaskForm = state => { const { addTaskUrl, addTaskOptions } = state.app const { allProxy,