2016-06-25 13:47:02 +02:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
2017-12-09 15:05:15 +01:00
|
|
|
angular.module('ariaNg').controller('CommandController', ['$rootScope', '$window', '$location', '$routeParams', 'base64', 'ariaNgDefaultOptions', 'ariaNgCommonService', 'ariaNgSettingService', 'aria2SettingService', 'aria2TaskService', 'ariaNgLogService', function ($rootScope, $window, $location, $routeParams, base64, ariaNgDefaultOptions, ariaNgCommonService, ariaNgSettingService, aria2SettingService, aria2TaskService, ariaNgLogService) {
|
2016-06-25 13:47:02 +02:00
|
|
|
var path = $location.path();
|
|
|
|
|
2017-12-09 15:05:15 +01:00
|
|
|
var doNewTaskCommand = function (url, params) {
|
2017-03-20 16:55:01 +01:00
|
|
|
try {
|
|
|
|
url = base64.urldecode(url);
|
|
|
|
} catch (ex) {
|
|
|
|
ariaNgCommonService.showError('URL is not base64 encoded!');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-09 15:05:15 +01:00
|
|
|
var options = {};
|
2018-05-19 10:22:32 +02:00
|
|
|
var isPaused = false;
|
2017-12-09 15:05:15 +01:00
|
|
|
|
|
|
|
if (params) {
|
|
|
|
for (var key in params) {
|
|
|
|
if (!params.hasOwnProperty(key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aria2SettingService.isOptionKeyValid(key)) {
|
|
|
|
options[key] = params[key];
|
|
|
|
}
|
|
|
|
}
|
2018-05-19 10:22:32 +02:00
|
|
|
|
|
|
|
if (params.pause === 'true') {
|
|
|
|
isPaused = true;
|
|
|
|
}
|
2017-12-09 15:05:15 +01:00
|
|
|
}
|
|
|
|
|
2017-03-20 16:55:01 +01:00
|
|
|
$rootScope.loadPromise = aria2TaskService.newUriTask({
|
2016-06-25 13:47:02 +02:00
|
|
|
urls: [url],
|
2017-12-09 15:05:15 +01:00
|
|
|
options: options
|
2018-05-19 10:22:32 +02:00
|
|
|
}, isPaused, function (response) {
|
2016-06-25 13:47:02 +02:00
|
|
|
if (!response.success) {
|
2017-03-20 16:55:01 +01:00
|
|
|
return false;
|
2016-06-25 13:47:02 +02:00
|
|
|
}
|
|
|
|
|
2018-05-19 10:22:32 +02:00
|
|
|
if (isPaused) {
|
|
|
|
$location.path('/waiting');
|
|
|
|
} else {
|
|
|
|
$location.path('/downloading');
|
|
|
|
}
|
2016-06-25 13:47:02 +02:00
|
|
|
});
|
|
|
|
|
2017-03-20 16:55:01 +01:00
|
|
|
ariaNgLogService.info('[CommandController] new download: ' + url);
|
2017-03-20 16:44:59 +01:00
|
|
|
|
2017-03-20 16:55:01 +01:00
|
|
|
return true;
|
|
|
|
};
|
2017-03-20 16:44:59 +01:00
|
|
|
|
2017-03-20 16:55:01 +01:00
|
|
|
var doSetRpcCommand = function (rpcProtocol, rpcHost, rpcPort, rpcInterface, secret) {
|
|
|
|
rpcPort = rpcPort || ariaNgDefaultOptions.rpcPort;
|
|
|
|
rpcInterface = rpcInterface || ariaNgDefaultOptions.rpcInterface;
|
|
|
|
secret = secret || ariaNgDefaultOptions.secret;
|
2017-03-20 16:35:34 +01:00
|
|
|
|
|
|
|
ariaNgLogService.info('[CommandController] set rpc: ' + rpcProtocol + '://' + rpcHost + ':' + rpcPort + '/' + rpcInterface + ', secret: ' + secret);
|
|
|
|
|
|
|
|
if (!rpcProtocol || (rpcProtocol !== 'http' && rpcProtocol !== 'https' && rpcProtocol !== 'ws' && rpcProtocol !== 'wss')) {
|
|
|
|
ariaNgCommonService.showError('Protocol is invalid!');
|
2017-03-20 16:55:01 +01:00
|
|
|
return false;
|
2017-03-20 16:35:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!rpcHost) {
|
|
|
|
ariaNgCommonService.showError('RPC host cannot be empty!');
|
2017-03-20 16:55:01 +01:00
|
|
|
return false;
|
2017-03-20 16:35:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (secret) {
|
|
|
|
try {
|
|
|
|
secret = base64.urldecode(secret);
|
|
|
|
} catch (ex) {
|
|
|
|
ariaNgCommonService.showError('RPC secret is not base64 encoded!');
|
2017-03-20 16:55:01 +01:00
|
|
|
return false;
|
2017-03-20 16:35:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var newSetting = {
|
|
|
|
rpcAlias: '',
|
|
|
|
rpcHost: rpcHost,
|
|
|
|
rpcPort: rpcPort,
|
|
|
|
rpcInterface: rpcInterface,
|
|
|
|
protocol: rpcProtocol,
|
|
|
|
httpMethod: ariaNgDefaultOptions.httpMethod,
|
|
|
|
secret: secret
|
|
|
|
};
|
|
|
|
|
|
|
|
if (ariaNgSettingService.isRpcSettingEqualsDefault(newSetting)) {
|
|
|
|
$location.path('/downloading');
|
|
|
|
} else {
|
|
|
|
ariaNgSettingService.setDefaultRpcSetting(newSetting, {
|
|
|
|
keepCurrent: false,
|
|
|
|
forceSet: true
|
|
|
|
});
|
|
|
|
|
|
|
|
$location.path('/downloading');
|
|
|
|
$window.location.reload();
|
|
|
|
}
|
2017-03-20 16:55:01 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
var doCommand = function (path, params) {
|
2017-10-14 14:08:07 +02:00
|
|
|
if (path.indexOf('/new') === 0) {
|
2017-12-09 15:05:15 +01:00
|
|
|
return doNewTaskCommand(params.url, params);
|
2017-10-14 14:08:07 +02:00
|
|
|
} else if (path.indexOf('/settings/rpc/set') === 0) {
|
2017-03-20 16:55:01 +01:00
|
|
|
return doSetRpcCommand(params.protocol, params.host, params.port, params.interface, params.secret);
|
|
|
|
} else {
|
|
|
|
ariaNgCommonService.showError('Parameter is invalid!');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-14 14:08:07 +02:00
|
|
|
var allParameters = angular.extend({}, $routeParams, $location.search());
|
|
|
|
|
|
|
|
if (!doCommand(path, allParameters)) {
|
2017-03-20 16:55:01 +01:00
|
|
|
$location.path('/downloading');
|
2016-06-25 13:47:02 +02:00
|
|
|
}
|
|
|
|
}]);
|
2016-08-01 16:49:16 +02:00
|
|
|
}());
|