Browse Source

support open torrent/metalink file via command line argument

MaysWind 6 years ago
parent
commit
705255ffa0
5 changed files with 132 additions and 6 deletions
  1. 12 1
      app.js
  2. 14 1
      app/scripts/controllers/new.js
  3. 4 0
      app/scripts/core/root.js
  4. 11 0
      app/scripts/services/ariaNgNativeElectronService.js
  5. 91 4
      cmd.js

+ 12 - 1
app.js

@@ -45,6 +45,11 @@ app.on('second-instance', (event, argv, workingDirectory) => {
         }
 
         core.mainWindow.focus();
+
+        if (cmd.isContainsSupportedFileArg(argv[1])) {
+            cmd.asyncNewTaskFromFile(argv[1]);
+            cmd.navigateToNewTask();
+        }
     }
 });
 
@@ -101,7 +106,13 @@ app.on('ready', () => {
     }
 
     core.mainWindow.setMenu(null);
-    core.mainWindow.loadURL(cmd.getMainUrl());
+
+    if (cmd.isContainsSupportedFileArg(process.argv[1])) {
+        cmd.asyncNewTaskFromFile(process.argv[1]);
+        cmd.loadNewTaskUrl();
+    } else {
+        cmd.loadIndexUrl();
+    }
 
     core.mainWindow.once('ready-to-show', () => {
         core.mainWindow.show();

+ 14 - 1
app/scripts/controllers/new.js

@@ -1,7 +1,7 @@
 (function () {
     'use strict';
 
-    angular.module('ariaNg').controller('NewTaskController', ['$rootScope', '$scope', '$location', '$timeout', 'ariaNgCommonService', 'ariaNgLocalizationService', 'ariaNgLogService', 'ariaNgFileService', 'ariaNgSettingService', 'aria2TaskService', 'aria2SettingService', function ($rootScope, $scope, $location, $timeout, ariaNgCommonService, ariaNgLocalizationService, ariaNgLogService, ariaNgFileService, ariaNgSettingService, aria2TaskService, aria2SettingService) {
+    angular.module('ariaNg').controller('NewTaskController', ['$rootScope', '$scope', '$location', '$timeout', 'ariaNgCommonService', 'ariaNgLocalizationService', 'ariaNgLogService', 'ariaNgFileService', 'ariaNgSettingService', 'aria2TaskService', 'aria2SettingService', 'ariaNgNativeElectronService', function ($rootScope, $scope, $location, $timeout, ariaNgCommonService, ariaNgLocalizationService, ariaNgLogService, ariaNgFileService, ariaNgSettingService, aria2TaskService, aria2SettingService, ariaNgNativeElectronService) {
         var tabOrders = ['links', 'options'];
         var parameters = $location.search();
 
@@ -210,6 +210,19 @@
             return urls ? urls.length : 0;
         };
 
+        $scope.$on('$viewContentLoaded', function () {
+            var result = ariaNgNativeElectronService.getAndClearToBeCreatedTaskFilePath();
+
+            if (result && !result.exception) {
+                $scope.context.uploadFile = result;
+                $scope.context.taskType = result.type;
+                $scope.changeTab('options');
+            } else if (result && result.exception) {
+                ariaNgLogService.error('[NewTaskController] get file via electron error', result.exception);
+                ariaNgLocalizationService.showError(result.exception);
+            }
+        });
+
         $rootScope.loadPromise = $timeout(function () {}, 100);
     }]);
 }());

+ 4 - 0
app/scripts/core/root.js

@@ -215,6 +215,10 @@
             $rootScope.windowContext.maximized = false;
         });
 
+        ariaNgNativeElectronService.onMessage('navigate-to', function (event, routeUrl) {
+            $location.path(routeUrl);
+        });
+
         ariaNgSettingService.setDebugMode(ariaNgNativeElectronService.isDevMode());
 
         ariaNgSettingService.onFirstAccess(function () {

+ 11 - 0
app/scripts/services/ariaNgNativeElectronService.js

@@ -4,6 +4,9 @@
     angular.module('ariaNg').factory('ariaNgNativeElectronService', ['ariaNgLocalizationService', function (ariaNgLocalizationService) {
         var electron = angular.isFunction(window.nodeRequire) ? nodeRequire('electron') : {};
         var remote = electron.remote || {
+            require: function () {
+                return {};
+            },
             getGlobal: function () {
                 return {};
             },
@@ -11,11 +14,13 @@
                 return {};
             }
         };
+        var ipcRenderer = electron.ipcRenderer || {};
         var shell = electron.shell || {
             openExternal: function () {
                 return false;
             }
         };
+        var cmd = remote.require('./cmd');
         var tray = remote.require('./tray');
 
         return {
@@ -60,6 +65,9 @@
             registerEvent: function (event, callback) {
                 this.getCurrentWindow().on && this.getCurrentWindow().on(event, callback);
             },
+            onMessage: function (messageType, callback) {
+                ipcRenderer.on && ipcRenderer.on(messageType, callback);
+            },
             initTray: function () {
                 tray.init({
                     labels: {
@@ -72,6 +80,9 @@
                 tray.destroy();
                 this.initTray();
             },
+            getAndClearToBeCreatedTaskFilePath: function () {
+                return cmd.getAndClearToBeCreatedTaskFilePath();
+            },
             isMaximized: function () {
                 return this.getCurrentWindow().isMaximized && this.getCurrentWindow().isMaximized();
             },

+ 91 - 4
cmd.js

@@ -1,5 +1,17 @@
+const fs = require('fs');
+const path = require('path');
+const url = require('url');
+
+const core = require('./core');
+
+const supportedFileExtensions = {
+    '.torrent': 'torrent',
+    '.meta4': 'metalink',
+    '.metalink': 'metalink'
+};
+
 const argv = require('yargs')
-    .usage('Usage: $0 [options]')
+    .usage('Usage: $0 [file] [options]')
     .option('d', {
         alias: 'development',
         type: 'boolean',
@@ -10,13 +22,88 @@ const argv = require('yargs')
     .parse(process.argv.slice(1));
 
 let cmd = (function () {
-    let getMainUrl = function () {
-        return 'file://' + __dirname + '/app/index.html'
+    let toBeCreatedTaskFilePath = null;
+
+    let isContainsSupportedFileArg = function (arg) {
+        if (!arg) {
+            return false;
+        }
+
+        var fileExtension = path.extname(arg);
+
+        if (!supportedFileExtensions[fileExtension]) {
+            return false;
+        }
+
+        return fs.existsSync(arg);
+    };
+
+    let getIndexUrl = function () {
+        return url.format({
+            protocol: 'file',
+            slashes: true,
+            pathname: path.join(__dirname, 'app', 'index.html')
+        });
+    };
+
+    let loadIndexUrl = function () {
+        core.mainWindow.loadURL(getIndexUrl());
+    };
+
+    let loadNewTaskUrl = function () {
+        core.mainWindow.loadURL(getIndexUrl() + '#!/new');
+    };
+
+    let navigateTo = function (routeUrl) {
+        core.mainWindow.webContents.send('navigate-to', routeUrl);
+    };
+
+    let navigateToNewTask = function () {
+        navigateTo('/new');
+    };
+
+    let asyncNewTaskFromFile = function (filePath) {
+        toBeCreatedTaskFilePath = filePath;
+    };
+
+    let getAndClearToBeCreatedTaskFilePath = function () {
+        let result = null;
+        let filePath = toBeCreatedTaskFilePath;
+
+        if (!filePath) {
+            return result;
+        }
+
+        toBeCreatedTaskFilePath = null;
+
+        try {
+            let fileExtension = path.extname(filePath);
+            let fileContent = fs.readFileSync(filePath);
+
+            result = {
+                type: supportedFileExtensions[fileExtension],
+                fileName: path.basename(filePath),
+                base64Content: Buffer.from(fileContent).toString('base64')
+            };
+        } catch (e) {
+            result = {
+                exception: e
+            }
+        }
+
+        return result;
     };
 
     return {
         argv: argv,
-        getMainUrl: getMainUrl
+        isContainsSupportedFileArg: isContainsSupportedFileArg,
+        getIndexUrl: getIndexUrl,
+        loadIndexUrl: loadIndexUrl,
+        loadNewTaskUrl: loadNewTaskUrl,
+        navigateTo: navigateTo,
+        navigateToNewTask: navigateToNewTask,
+        asyncNewTaskFromFile: asyncNewTaskFromFile,
+        getAndClearToBeCreatedTaskFilePath: getAndClearToBeCreatedTaskFilePath
     }
 })();