support add bt/metalink task via file

This commit is contained in:
MaysWind 2016-06-24 00:39:18 +08:00
parent a8a5461d38
commit 90a4de8a87
8 changed files with 173 additions and 10 deletions

View file

@ -301,6 +301,7 @@
<script src="scripts/filters/taskStatus.js"></script> <script src="scripts/filters/taskStatus.js"></script>
<script src="scripts/filters/volumn.js"></script> <script src="scripts/filters/volumn.js"></script>
<script src="scripts/services/ariaNgCommonService.js"></script> <script src="scripts/services/ariaNgCommonService.js"></script>
<script src="scripts/services/ariaNgFileService.js"></script>
<script src="scripts/services/ariaNgSettingService.js"></script> <script src="scripts/services/ariaNgSettingService.js"></script>
<script src="scripts/services/ariaNgMonitorService.js"></script> <script src="scripts/services/ariaNgMonitorService.js"></script>
<script src="scripts/services/aria2TaskService.js"></script> <script src="scripts/services/aria2TaskService.js"></script>

View file

@ -82,6 +82,9 @@
"Open Torrent File": "打开种子文件", "Open Torrent File": "打开种子文件",
"Open Metalink File": "打开 Metalink 文件", "Open Metalink File": "打开 Metalink 文件",
"Support multiple URLs, one URL per line.": "支持多个 URL 地址, 每个地址占一行.", "Support multiple URLs, one URL per line.": "支持多个 URL 地址, 每个地址占一行.",
"Your browser does not support loading file!": "您的浏览器不支持加载文件!",
"The selected file type is invalid!": "选择的文件类型无效!",
"Failed to load file!": "加载文件失败!",
"More Options": "更多选项", "More Options": "更多选项",
"Language": "语言", "Language": "语言",
"Aria2 RPC Host": "Aria2 RPC 主机", "Aria2 RPC Host": "Aria2 RPC 主机",

View file

@ -86,6 +86,9 @@
'Start Download': 'Start Download', 'Start Download': 'Start Download',
'Manual Download': 'Manual Download', 'Manual Download': 'Manual Download',
'Support multiple URLs, one URL per line.': 'Support multiple URLs, one URL per line.', 'Support multiple URLs, one URL per line.': 'Support multiple URLs, one URL per line.',
'Your browser does not support loading file!': 'Your browser does not support loading file!',
'The selected file type is invalid': 'The selected file type is invalid',
'Failed to load file!': 'Failed to load file!',
'More Options': 'More Options', 'More Options': 'More Options',
'Language': 'Language', 'Language': 'Language',
'Aria2 RPC Host': 'Aria2 RPC Host', 'Aria2 RPC Host': 'Aria2 RPC Host',

View file

@ -1,7 +1,7 @@
(function () { (function () {
'use strict'; 'use strict';
angular.module('ariaNg').controller('NewTaskController', ['$rootScope', '$scope', '$location', '$timeout', 'aria2SettingService', 'aria2TaskService', function ($rootScope, $scope, $location, $timeout, aria2SettingService, aria2TaskService) { angular.module('ariaNg').controller('NewTaskController', ['$rootScope', '$scope', '$location', '$timeout', 'aria2SettingService', 'aria2TaskService', 'ariaNgFileService', function ($rootScope, $scope, $location, $timeout, aria2SettingService, aria2TaskService, ariaNgFileService) {
var tabOrders = ['links', 'options']; var tabOrders = ['links', 'options'];
$scope.context = { $scope.context = {
@ -58,6 +58,40 @@
}); });
}; };
$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.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.startDownload = function (pauseOnAdded) { $scope.startDownload = function (pauseOnAdded) {
var urls = $scope.context.urls.split('\n'); var urls = $scope.context.urls.split('\n');
var options = angular.copy($scope.context.options); var options = angular.copy($scope.context.options);

View file

@ -186,12 +186,26 @@
return invokeMulti(this.addUri, contexts, context.callback); return invokeMulti(this.addUri, contexts, context.callback);
}, },
// addTorrent: function (context) { addTorrent: function (context) {
// return invoke('addTorrent', context); var content = context.task.content;
// }, var options = angular.copy(context.task.options);
// addMetalink: function (context) {
// return invoke('addMetalink', context); if (context.pauseOnAdded) {
// }, options.pause = 'true';
}
return invoke(buildRequestContext('addTorrent', context, content, [], options));
},
addMetalink: function (context) {
var content = context.task.content;
var options = angular.copy(context.task.options);
if (context.pauseOnAdded) {
options.pause = 'true';
}
return invoke(buildRequestContext('addMetalink', context, content, [], options));
},
remove: function (context) { remove: function (context) {
return invoke(buildRequestContext('remove', context, context.gid)); return invoke(buildRequestContext('remove', context, context.gid));
}, },

View file

@ -266,6 +266,22 @@
callback: callback callback: callback
}); });
}, },
newTorrentTask: function (task, pauseOnAdded, callback, silent) {
return aria2RpcService.addTorrent({
task: task,
pauseOnAdded: !!pauseOnAdded,
silent: !!silent,
callback: callback
});
},
newMetalinkTask: function (task, pauseOnAdded, callback, silent) {
return aria2RpcService.addMetalink({
task: task,
pauseOnAdded: !!pauseOnAdded,
silent: !!silent,
callback: callback
});
},
startTasks: function (gids, callback, silent) { startTasks: function (gids, callback, silent) {
return aria2RpcService.unpauseMulti({ return aria2RpcService.unpauseMulti({
gids: gids, gids: gids,

View file

@ -0,0 +1,92 @@
(function () {
'use strict';
angular.module('ariaNg').factory('ariaNgFileService', ['ariaNgCommonService', function (ariaNgCommonService) {
var isSupportFileReader = !!FileReader;
var getAllowedExtensions = function (fileFilter) {
var extensions = [];
if (!fileFilter || fileFilter.length < 1) {
extensions.push(/.+$/);
return extensions;
}
var fileFilters = fileFilter.split(',');
for (var i = 0; i < fileFilters.length; i++) {
var extension = fileFilters[i];
if (extension == '*.*') {
extensions.push(/.+$/);
continue;
}
extension = extension.replace('.', '\\.');
extension = extension + '$';
extensions.push(new RegExp(extension));
}
return extensions;
};
var checkFileExtension = function (fileName, extensions) {
if (!extensions || extensions.length < 1) {
return true;
}
for (var i = 0; i < extensions.length; i++) {
if (extensions[i].test(fileName)) {
return true;
}
}
return false;
};
return {
openFileContent: function (fileFilter, callback) {
if (!isSupportFileReader) {
ariaNgCommonService.showError('Your browser does not support loading file!');
return;
}
var allowedExtensions = getAllowedExtensions(fileFilter);
angular.element('<input type="file" style="display: none"/>').change(function () {
if (!this.files || this.files.length < 1) {
return;
}
var file = this.files[0];
var fileName = file.name;
if (!checkFileExtension(fileName, allowedExtensions)) {
ariaNgCommonService.showError('The selected file type is invalid!');
return;
}
var reader = new FileReader();
reader.onload = function () {
var result = {
fileName: fileName,
base64Content: this.result.replace(/.*?base64,/, '')
};
if (callback) {
callback(result);
}
};
reader.onerror = function () {
ariaNgCommonService.showError('Failed to load file!');
};
reader.readAsDataURL(file);
}).trigger('click');
}
}
}]);
})();

View file

@ -28,9 +28,9 @@
</div> </div>
<div class="row"> <div class="row">
<div class="col-xs-6"> <div class="col-xs-6">
<div class="new-task-toollink hide"> <div class="new-task-toollink">
<a class="pointer-cursor" translate>Open Torrent File</a> <a class="pointer-cursor" ng-click="openTorrent()" translate>Open Torrent File</a>
<a class="pointer-cursor" translate>Open Metalink File</a> <a class="pointer-cursor" ng-click="openMetalink()" translate>Open Metalink File</a>
</div> </div>
</div> </div>
<div class="col-xs-6"> <div class="col-xs-6">