This repository has been archived on 2022-01-02. You can view files and clone it, but cannot push or open issues or pull requests.
AriaNg/src/scripts/controllers/command.js

123 lines
4.5 KiB
JavaScript
Raw Normal View History

2016-06-25 13:47:02 +02:00
(function () {
'use strict';
2018-08-12 14:26:26 +02:00
angular.module('ariaNg').controller('CommandController', ['$rootScope', '$window', '$location', '$routeParams', 'ariaNgDefaultOptions', 'ariaNgCommonService', 'ariaNgLocalizationService', 'ariaNgLogService', 'ariaNgSettingService', 'aria2TaskService', 'aria2SettingService', function ($rootScope, $window, $location, $routeParams, ariaNgDefaultOptions, ariaNgCommonService, ariaNgLocalizationService, ariaNgLogService, ariaNgSettingService, aria2TaskService, aria2SettingService) {
2016-06-25 13:47:02 +02:00
var path = $location.path();
var doNewTaskCommand = function (url, params) {
2017-03-20 16:55:01 +01:00
try {
2018-08-12 10:59:21 +02:00
url = ariaNgCommonService.base64UrlDecode(url);
2017-03-20 16:55:01 +01:00
} catch (ex) {
2018-08-12 14:26:26 +02:00
ariaNgLocalizationService.showError('URL is not base64 encoded!');
2017-03-20 16:55:01 +01:00
return false;
}
var options = {};
2018-05-19 10:22:32 +02:00
var isPaused = false;
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-03-20 16:55:01 +01:00
$rootScope.loadPromise = aria2TaskService.newUriTask({
2016-06-25 13:47:02 +02:00
urls: [url],
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:55:01 +01:00
return true;
};
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')) {
2018-08-12 14:26:26 +02:00
ariaNgLocalizationService.showError('Protocol is invalid!');
2017-03-20 16:55:01 +01:00
return false;
2017-03-20 16:35:34 +01:00
}
if (!rpcHost) {
2018-08-12 14:26:26 +02:00
ariaNgLocalizationService.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 {
2018-08-12 10:59:21 +02:00
secret = ariaNgCommonService.base64UrlDecode(secret);
2017-03-20 16:35:34 +01:00
} catch (ex) {
2018-08-12 14:26:26 +02:00
ariaNgLocalizationService.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) {
if (path.indexOf('/new') === 0) {
return doNewTaskCommand(params.url, params);
} 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 {
2018-08-12 14:26:26 +02:00
ariaNgLocalizationService.showError('Parameter is invalid!');
2017-03-20 16:55:01 +01:00
return false;
}
};
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
}());