command.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. (function () {
  2. 'use strict';
  3. angular.module('ariaNg').controller('CommandController', ['$rootScope', '$window', '$location', '$routeParams', 'ariaNgDefaultOptions', 'ariaNgCommonService', 'ariaNgLogService', 'ariaNgSettingService', 'aria2TaskService', 'aria2SettingService', function ($rootScope, $window, $location, $routeParams, ariaNgDefaultOptions, ariaNgCommonService, ariaNgLogService, ariaNgSettingService, aria2TaskService, aria2SettingService) {
  4. var path = $location.path();
  5. var doNewTaskCommand = function (url, params) {
  6. try {
  7. url = ariaNgCommonService.base64UrlDecode(url);
  8. } catch (ex) {
  9. ariaNgCommonService.showError('URL is not base64 encoded!');
  10. return false;
  11. }
  12. var options = {};
  13. var isPaused = false;
  14. if (params) {
  15. for (var key in params) {
  16. if (!params.hasOwnProperty(key)) {
  17. continue;
  18. }
  19. if (aria2SettingService.isOptionKeyValid(key)) {
  20. options[key] = params[key];
  21. }
  22. }
  23. if (params.pause === 'true') {
  24. isPaused = true;
  25. }
  26. }
  27. $rootScope.loadPromise = aria2TaskService.newUriTask({
  28. urls: [url],
  29. options: options
  30. }, isPaused, function (response) {
  31. if (!response.success) {
  32. return false;
  33. }
  34. if (isPaused) {
  35. $location.path('/waiting');
  36. } else {
  37. $location.path('/downloading');
  38. }
  39. });
  40. ariaNgLogService.info('[CommandController] new download: ' + url);
  41. return true;
  42. };
  43. var doSetRpcCommand = function (rpcProtocol, rpcHost, rpcPort, rpcInterface, secret) {
  44. rpcPort = rpcPort || ariaNgDefaultOptions.rpcPort;
  45. rpcInterface = rpcInterface || ariaNgDefaultOptions.rpcInterface;
  46. secret = secret || ariaNgDefaultOptions.secret;
  47. ariaNgLogService.info('[CommandController] set rpc: ' + rpcProtocol + '://' + rpcHost + ':' + rpcPort + '/' + rpcInterface + ', secret: ' + secret);
  48. if (!rpcProtocol || (rpcProtocol !== 'http' && rpcProtocol !== 'https' && rpcProtocol !== 'ws' && rpcProtocol !== 'wss')) {
  49. ariaNgCommonService.showError('Protocol is invalid!');
  50. return false;
  51. }
  52. if (!rpcHost) {
  53. ariaNgCommonService.showError('RPC host cannot be empty!');
  54. return false;
  55. }
  56. if (secret) {
  57. try {
  58. secret = ariaNgCommonService.base64UrlDecode(secret);
  59. } catch (ex) {
  60. ariaNgCommonService.showError('RPC secret is not base64 encoded!');
  61. return false;
  62. }
  63. }
  64. var newSetting = {
  65. rpcAlias: '',
  66. rpcHost: rpcHost,
  67. rpcPort: rpcPort,
  68. rpcInterface: rpcInterface,
  69. protocol: rpcProtocol,
  70. httpMethod: ariaNgDefaultOptions.httpMethod,
  71. secret: secret
  72. };
  73. if (ariaNgSettingService.isRpcSettingEqualsDefault(newSetting)) {
  74. $location.path('/downloading');
  75. } else {
  76. ariaNgSettingService.setDefaultRpcSetting(newSetting, {
  77. keepCurrent: false,
  78. forceSet: true
  79. });
  80. $location.path('/downloading');
  81. $window.location.reload();
  82. }
  83. return true;
  84. };
  85. var doCommand = function (path, params) {
  86. if (path.indexOf('/new') === 0) {
  87. return doNewTaskCommand(params.url, params);
  88. } else if (path.indexOf('/settings/rpc/set') === 0) {
  89. return doSetRpcCommand(params.protocol, params.host, params.port, params.interface, params.secret);
  90. } else {
  91. ariaNgCommonService.showError('Parameter is invalid!');
  92. return false;
  93. }
  94. };
  95. var allParameters = angular.extend({}, $routeParams, $location.search());
  96. if (!doCommand(path, allParameters)) {
  97. $location.path('/downloading');
  98. }
  99. }]);
  100. }());