| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- (function () {
- 'use strict';
- 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();
- var saveDownloadPath = function (options) {
- if (!options || !options.dir) {
- return;
- }
- aria2SettingService.addSettingHistory('dir', options.dir);
- };
- var downloadByLinks = function (pauseOnAdded, responseCallback) {
- var urls = ariaNgCommonService.parseUrlsFromOriginInput($scope.context.urls);
- var options = angular.copy($scope.context.options);
- var tasks = [];
- for (var i = 0; i < urls.length; i++) {
- if (urls[i] === '' || urls[i].trim() === '') {
- continue;
- }
- tasks.push({
- urls: [urls[i].trim()],
- options: options
- });
- }
- saveDownloadPath(options);
- return aria2TaskService.newUriTasks(tasks, pauseOnAdded, responseCallback);
- };
- var downloadByTorrent = function (pauseOnAdded, responseCallback) {
- var task = {
- content: $scope.context.uploadFile.base64Content,
- options: angular.copy($scope.context.options)
- };
- saveDownloadPath(task.options);
- return aria2TaskService.newTorrentTask(task, pauseOnAdded, responseCallback);
- };
- var downloadByMetalink = function (pauseOnAdded, responseCallback) {
- var task = {
- content: $scope.context.uploadFile.base64Content,
- options: angular.copy($scope.context.options)
- };
- saveDownloadPath(task.options);
- return aria2TaskService.newMetalinkTask(task, pauseOnAdded, responseCallback);
- };
- $scope.context = {
- currentTab: 'links',
- taskType: 'urls',
- urls: '',
- uploadFile: null,
- availableOptions: (function () {
- var keys = aria2SettingService.getNewTaskOptionKeys();
- return aria2SettingService.getSpecifiedOptions(keys, {
- disableRequired: true
- });
- })(),
- globalOptions: null,
- options: {},
- optionFilter: {
- global: true,
- http: false,
- bittorrent: false
- }
- };
- if (parameters.url) {
- try {
- $scope.context.urls = ariaNgCommonService.base64UrlDecode(parameters.url);
- } catch (ex) {
- ariaNgLogService.error('[NewTaskController] base64 decode error, url=' + parameters.url, ex);
- }
- }
- $scope.changeTab = function (tabName) {
- if (tabName === 'options') {
- $scope.loadDefaultOption();
- }
- $scope.context.currentTab = tabName;
- };
- $rootScope.swipeActions.extentLeftSwipe = function () {
- var tabIndex = tabOrders.indexOf($scope.context.currentTab);
- if (tabIndex < tabOrders.length - 1) {
- $scope.changeTab(tabOrders[tabIndex + 1]);
- return true;
- } else {
- return false;
- }
- };
- $rootScope.swipeActions.extentRightSwipe = function () {
- var tabIndex = tabOrders.indexOf($scope.context.currentTab);
- if (tabIndex > 0) {
- $scope.changeTab(tabOrders[tabIndex - 1]);
- return true;
- } else {
- return false;
- }
- };
- $scope.loadDefaultOption = function () {
- if ($scope.context.globalOptions) {
- return;
- }
- $rootScope.loadPromise = aria2SettingService.getGlobalOption(function (response) {
- if (response.success) {
- $scope.context.globalOptions = response.data;
- }
- });
- };
- $scope.openTorrent = function () {
- ariaNgFileService.openFileContent({
- scope: $scope,
- fileFilter: '.torrent',
- fileType: 'binary'
- }, function (result) {
- $scope.context.uploadFile = result;
- $scope.context.taskType = 'torrent';
- $scope.changeTab('options');
- }, function (error) {
- ariaNgLocalizationService.showError(error);
- }, angular.element('#file-holder'));
- };
- $scope.openMetalink = function () {
- ariaNgFileService.openFileContent({
- scope: $scope,
- fileFilter: '.meta4,.metalink',
- fileType: 'binary'
- }, function (result) {
- $scope.context.uploadFile = result;
- $scope.context.taskType = 'metalink';
- $scope.changeTab('options');
- }, function (error) {
- ariaNgLocalizationService.showError(error);
- }, angular.element('#file-holder'));
- };
- $scope.startDownload = function (pauseOnAdded) {
- var responseCallback = function (response) {
- if (!response.hasSuccess && !response.success) {
- return;
- }
- var firstTask = null;
- if (response.results && response.results.length > 0) {
- firstTask = response.results[0];
- } else if (response) {
- firstTask = response;
- }
- if (ariaNgSettingService.getAfterCreatingNewTask() === 'task-detail' && firstTask && firstTask.data) {
- $location.path('/task/detail/' + firstTask.data);
- } else {
- if (pauseOnAdded) {
- $location.path('/waiting');
- } else {
- $location.path('/downloading');
- }
- }
- };
- if ($scope.context.taskType === 'urls') {
- $rootScope.loadPromise = downloadByLinks(pauseOnAdded, responseCallback);
- } else if ($scope.context.taskType === 'torrent') {
- $rootScope.loadPromise = downloadByTorrent(pauseOnAdded, responseCallback);
- } else if ($scope.context.taskType === 'metalink') {
- $rootScope.loadPromise = downloadByMetalink(pauseOnAdded, responseCallback);
- }
- };
- $scope.setOption = function (key, value, optionStatus) {
- if (value !== '') {
- $scope.context.options[key] = value;
- } else {
- delete $scope.context.options[key];
- }
- optionStatus.setReady();
- };
- $scope.urlTextboxKeyDown = function (event) {
- if (event.keyCode === 13 && event.ctrlKey && $scope.newTaskForm.$valid) {
- $scope.startDownload();
- }
- };
- $scope.getValidUrlsCount = function () {
- var urls = ariaNgCommonService.parseUrlsFromOriginInput($scope.context.urls);
- 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);
- }]);
- }());
|