diff --git a/src/langs/zh_CN.txt b/src/langs/zh_CN.txt index b4b3b3f..ad79856 100644 --- a/src/langs/zh_CN.txt +++ b/src/langs/zh_CN.txt @@ -67,6 +67,9 @@ Remain Time=剩余时间 Download Speed=下载速度 Upload Speed=上传速度 Links=链接 +Torrent File=种子文件 +Metalink File=Metalink 文件 +File Name:=文件名: Options=选项 Overview=总览 Blocks=区块信息 diff --git a/src/scripts/config/defaultLanguage.js b/src/scripts/config/defaultLanguage.js index 3b5e0d5..a5de56a 100644 --- a/src/scripts/config/defaultLanguage.js +++ b/src/scripts/config/defaultLanguage.js @@ -71,6 +71,9 @@ 'Download Speed': 'Download Speed', 'Upload Speed': 'Upload Speed', 'Links': 'Links', + 'Torrent File': 'Torrent File', + 'Metalink File': 'Metalink File', + 'File Name:': 'File Name:', 'Options': 'Options', 'Overview': 'Overview', 'Blocks': 'Blocks', diff --git a/src/scripts/controllers/new.js b/src/scripts/controllers/new.js index 7584dfb..0ddb556 100644 --- a/src/scripts/controllers/new.js +++ b/src/scripts/controllers/new.js @@ -1,12 +1,51 @@ (function () { 'use strict'; - angular.module('ariaNg').controller('NewTaskController', ['$rootScope', '$scope', '$location', '$timeout', 'aria2SettingService', 'aria2TaskService', 'ariaNgFileService', function ($rootScope, $scope, $location, $timeout, aria2SettingService, aria2TaskService, ariaNgFileService) { + angular.module('ariaNg').controller('NewTaskController', ['$rootScope', '$scope', '$location', '$timeout', 'ariaNgCommonService', 'ariaNgFileService', 'aria2SettingService', 'aria2TaskService', function ($rootScope, $scope, $location, $timeout, ariaNgCommonService, ariaNgFileService, aria2SettingService, aria2TaskService) { var tabOrders = ['links', 'options']; + var downloadByLinks = function (pauseOnAdded, responseCallback) { + var urls = $scope.context.urls.split('\n'); + 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 + }); + } + + return aria2TaskService.newUriTasks(tasks, pauseOnAdded, responseCallback); + }; + + var downloadByTorrent = function (pauseOnAdded, responseCallback) { + var task = { + content: $scope.context.uploadFile.base64Content, + options: angular.copy($scope.context.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) + }; + + return aria2TaskService.newMetalinkTask(task, pauseOnAdded, responseCallback); + }; + $scope.context = { currentTab: 'links', + taskType: 'urls', urls: '', + uploadFile: null, availableOptions: (function () { var keys = aria2SettingService.getNewTaskOptionKeys(); @@ -67,56 +106,27 @@ $scope.openTorrent = function () { ariaNgFileService.openFileContent('.torrent', function (result) { - var task = { - content: result.base64Content, - options: angular.copy($scope.context.options) - }; - - $rootScope.loadPromise = aria2TaskService.newTorrentTask(task, true, function (response) { - if (!response.success) { - return; - } - - $location.path('/task/detail/' + response.data); - }); + $scope.context.uploadFile = result; + $scope.context.taskType = 'torrent'; + $scope.changeTab('options'); + }, function (error) { + ariaNgCommonService.showError(error); }); }; $scope.openMetalink = function () { ariaNgFileService.openFileContent('.meta4,.metalink', function (result) { - var task = { - content: result.base64Content, - options: angular.copy($scope.context.options) - }; - - $rootScope.loadPromise = aria2TaskService.newMetalinkTask(task, true, function (response) { - if (!response.success) { - return; - } - - $location.path('/task/detail/' + response.data); - }); + $scope.context.uploadFile = result; + $scope.context.taskType = 'metalink'; + $scope.changeTab('options'); + }, function (error) { + ariaNgCommonService.showError(error); }); }; $scope.startDownload = function (pauseOnAdded) { - var urls = $scope.context.urls.split('\n'); - 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 - }); - } - - $rootScope.loadPromise = aria2TaskService.newUriTasks(tasks, pauseOnAdded, function (response) { - if (!response.hasSuccess) { + var responseCallback = function (response) { + if (!response.hasSuccess && !response.success) { return; } @@ -125,7 +135,15 @@ } 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) { diff --git a/src/scripts/services/ariaNgFileService.js b/src/scripts/services/ariaNgFileService.js index 6655f5c..37b9848 100644 --- a/src/scripts/services/ariaNgFileService.js +++ b/src/scripts/services/ariaNgFileService.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - angular.module('ariaNg').factory('ariaNgFileService', ['$window', 'ariaNgCommonService', function ($window, ariaNgCommonService) { + angular.module('ariaNg').factory('ariaNgFileService', ['$window', function ($window) { var isSupportFileReader = !!$window.FileReader; var getAllowedExtensions = function (fileFilter) { @@ -46,9 +46,12 @@ }; return { - openFileContent: function (fileFilter, callback) { + openFileContent: function (fileFilter, successCallback, errorCallback) { if (!isSupportFileReader) { - ariaNgCommonService.showError('Your browser does not support loading file!'); + if (errorCallback) { + errorCallback('Your browser does not support loading file!'); + } + return; } @@ -63,7 +66,10 @@ var fileName = file.name; if (!checkFileExtension(fileName, allowedExtensions)) { - ariaNgCommonService.showError('The selected file type is invalid!'); + if (errorCallback) { + errorCallback('The selected file type is invalid!'); + } + return; } @@ -75,13 +81,15 @@ base64Content: this.result.replace(/.*?base64,/, '') }; - if (callback) { - callback(result); + if (successCallback) { + successCallback(result); } }; reader.onerror = function () { - ariaNgCommonService.showError('Failed to load file!'); + if (errorCallback) { + errorCallback('Failed to load file!'); + } }; reader.readAsDataURL(file); diff --git a/src/views/new.html b/src/views/new.html index 4a7f3d4..36277cc 100644 --- a/src/views/new.html +++ b/src/views/new.html @@ -3,7 +3,7 @@