new.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. (function () {
  2. 'use strict';
  3. 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) {
  4. var tabOrders = ['links', 'options'];
  5. var parameters = $location.search();
  6. var saveDownloadPath = function (options) {
  7. if (!options || !options.dir) {
  8. return;
  9. }
  10. aria2SettingService.addSettingHistory('dir', options.dir);
  11. };
  12. var downloadByLinks = function (pauseOnAdded, responseCallback) {
  13. var urls = ariaNgCommonService.parseUrlsFromOriginInput($scope.context.urls);
  14. var options = angular.copy($scope.context.options);
  15. var tasks = [];
  16. for (var i = 0; i < urls.length; i++) {
  17. if (urls[i] === '' || urls[i].trim() === '') {
  18. continue;
  19. }
  20. tasks.push({
  21. urls: [urls[i].trim()],
  22. options: options
  23. });
  24. }
  25. saveDownloadPath(options);
  26. return aria2TaskService.newUriTasks(tasks, pauseOnAdded, responseCallback);
  27. };
  28. var downloadByTorrent = function (pauseOnAdded, responseCallback) {
  29. var task = {
  30. content: $scope.context.uploadFile.base64Content,
  31. options: angular.copy($scope.context.options)
  32. };
  33. saveDownloadPath(task.options);
  34. return aria2TaskService.newTorrentTask(task, pauseOnAdded, responseCallback);
  35. };
  36. var downloadByMetalink = function (pauseOnAdded, responseCallback) {
  37. var task = {
  38. content: $scope.context.uploadFile.base64Content,
  39. options: angular.copy($scope.context.options)
  40. };
  41. saveDownloadPath(task.options);
  42. return aria2TaskService.newMetalinkTask(task, pauseOnAdded, responseCallback);
  43. };
  44. $scope.context = {
  45. currentTab: 'links',
  46. taskType: 'urls',
  47. urls: '',
  48. uploadFile: null,
  49. availableOptions: (function () {
  50. var keys = aria2SettingService.getNewTaskOptionKeys();
  51. return aria2SettingService.getSpecifiedOptions(keys, {
  52. disableRequired: true
  53. });
  54. })(),
  55. globalOptions: null,
  56. options: {},
  57. optionFilter: {
  58. global: true,
  59. http: false,
  60. bittorrent: false
  61. }
  62. };
  63. if (parameters.url) {
  64. try {
  65. $scope.context.urls = ariaNgCommonService.base64UrlDecode(parameters.url);
  66. } catch (ex) {
  67. ariaNgLogService.error('[NewTaskController] base64 decode error, url=' + parameters.url, ex);
  68. }
  69. }
  70. $scope.changeTab = function (tabName) {
  71. if (tabName === 'options') {
  72. $scope.loadDefaultOption();
  73. }
  74. $scope.context.currentTab = tabName;
  75. };
  76. $rootScope.swipeActions.extentLeftSwipe = function () {
  77. var tabIndex = tabOrders.indexOf($scope.context.currentTab);
  78. if (tabIndex < tabOrders.length - 1) {
  79. $scope.changeTab(tabOrders[tabIndex + 1]);
  80. return true;
  81. } else {
  82. return false;
  83. }
  84. };
  85. $rootScope.swipeActions.extentRightSwipe = function () {
  86. var tabIndex = tabOrders.indexOf($scope.context.currentTab);
  87. if (tabIndex > 0) {
  88. $scope.changeTab(tabOrders[tabIndex - 1]);
  89. return true;
  90. } else {
  91. return false;
  92. }
  93. };
  94. $scope.loadDefaultOption = function () {
  95. if ($scope.context.globalOptions) {
  96. return;
  97. }
  98. $rootScope.loadPromise = aria2SettingService.getGlobalOption(function (response) {
  99. if (response.success) {
  100. $scope.context.globalOptions = response.data;
  101. }
  102. });
  103. };
  104. $scope.openTorrent = function () {
  105. ariaNgFileService.openFileContent({
  106. scope: $scope,
  107. fileFilter: '.torrent',
  108. fileType: 'binary'
  109. }, function (result) {
  110. $scope.context.uploadFile = result;
  111. $scope.context.taskType = 'torrent';
  112. $scope.changeTab('options');
  113. }, function (error) {
  114. ariaNgLocalizationService.showError(error);
  115. }, angular.element('#file-holder'));
  116. };
  117. $scope.openMetalink = function () {
  118. ariaNgFileService.openFileContent({
  119. scope: $scope,
  120. fileFilter: '.meta4,.metalink',
  121. fileType: 'binary'
  122. }, function (result) {
  123. $scope.context.uploadFile = result;
  124. $scope.context.taskType = 'metalink';
  125. $scope.changeTab('options');
  126. }, function (error) {
  127. ariaNgLocalizationService.showError(error);
  128. }, angular.element('#file-holder'));
  129. };
  130. $scope.startDownload = function (pauseOnAdded) {
  131. var responseCallback = function (response) {
  132. if (!response.hasSuccess && !response.success) {
  133. return;
  134. }
  135. var firstTask = null;
  136. if (response.results && response.results.length > 0) {
  137. firstTask = response.results[0];
  138. } else if (response) {
  139. firstTask = response;
  140. }
  141. if (ariaNgSettingService.getAfterCreatingNewTask() === 'task-detail' && firstTask && firstTask.data) {
  142. $location.path('/task/detail/' + firstTask.data);
  143. } else {
  144. if (pauseOnAdded) {
  145. $location.path('/waiting');
  146. } else {
  147. $location.path('/downloading');
  148. }
  149. }
  150. };
  151. if ($scope.context.taskType === 'urls') {
  152. $rootScope.loadPromise = downloadByLinks(pauseOnAdded, responseCallback);
  153. } else if ($scope.context.taskType === 'torrent') {
  154. $rootScope.loadPromise = downloadByTorrent(pauseOnAdded, responseCallback);
  155. } else if ($scope.context.taskType === 'metalink') {
  156. $rootScope.loadPromise = downloadByMetalink(pauseOnAdded, responseCallback);
  157. }
  158. };
  159. $scope.setOption = function (key, value, optionStatus) {
  160. if (value !== '') {
  161. $scope.context.options[key] = value;
  162. } else {
  163. delete $scope.context.options[key];
  164. }
  165. optionStatus.setReady();
  166. };
  167. $scope.urlTextboxKeyDown = function (event) {
  168. if (event.keyCode === 13 && event.ctrlKey && $scope.newTaskForm.$valid) {
  169. $scope.startDownload();
  170. }
  171. };
  172. $scope.getValidUrlsCount = function () {
  173. var urls = ariaNgCommonService.parseUrlsFromOriginInput($scope.context.urls);
  174. return urls ? urls.length : 0;
  175. };
  176. $scope.$on('$viewContentLoaded', function () {
  177. var result = ariaNgNativeElectronService.getAndClearToBeCreatedTaskFilePath();
  178. if (result && !result.exception) {
  179. $scope.context.uploadFile = result;
  180. $scope.context.taskType = result.type;
  181. $scope.changeTab('options');
  182. } else if (result && result.exception) {
  183. ariaNgLogService.error('[NewTaskController] get file via electron error', result.exception);
  184. ariaNgLocalizationService.showError(result.exception);
  185. }
  186. });
  187. $rootScope.loadPromise = $timeout(function () {}, 100);
  188. }]);
  189. }());