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

100 lines
3.7 KiB
JavaScript
Raw Normal View History

2016-06-25 13:47:02 +02:00
(function () {
'use strict';
2017-03-20 16:35:34 +01:00
angular.module('ariaNg').controller('CommandController', ['$rootScope', '$window', '$location', '$routeParams', 'base64', 'ariaNgDefaultOptions', 'ariaNgCommonService', 'ariaNgSettingService', 'aria2TaskService', 'ariaNgLogService', function ($rootScope, $window, $location, $routeParams, base64, ariaNgDefaultOptions, ariaNgCommonService, ariaNgSettingService, aria2TaskService, ariaNgLogService) {
2016-06-25 13:47:02 +02:00
var path = $location.path();
2017-03-20 16:55:01 +01:00
var doNewTaskCommand = function (url) {
try {
url = base64.urldecode(url);
} catch (ex) {
ariaNgCommonService.showError('URL is not base64 encoded!');
return false;
}
$rootScope.loadPromise = aria2TaskService.newUriTask({
2016-06-25 13:47:02 +02:00
urls: [url],
options: {}
}, false, function (response) {
if (!response.success) {
2017-03-20 16:55:01 +01:00
return false;
2016-06-25 13:47:02 +02:00
}
$location.path('/downloading');
});
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')) {
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) {
if (path.indexOf('/new') === 0) {
2017-03-20 16:55:01 +01:00
return doNewTaskCommand(params.url);
} 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;
}
};
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
}());