code refactor

This commit is contained in:
MaysWind 2017-03-20 23:55:01 +08:00
parent b8815e2cf7
commit ce5aa46f0d

View file

@ -4,49 +4,45 @@
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) { 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) {
var path = $location.path(); var path = $location.path();
var newUrlDownload = function (url) { var doNewTaskCommand = function (url) {
return aria2TaskService.newUriTask({ try {
url = base64.urldecode(url);
} catch (ex) {
ariaNgCommonService.showError('URL is not base64 encoded!');
return false;
}
$rootScope.loadPromise = aria2TaskService.newUriTask({
urls: [url], urls: [url],
options: {} options: {}
}, false, function (response) { }, false, function (response) {
if (!response.success) { if (!response.success) {
return; return false;
} }
$location.path('/downloading'); $location.path('/downloading');
}); });
ariaNgLogService.info('[CommandController] new download: ' + url);
return true;
}; };
if (path.indexOf('/new/') === 0) { var doSetRpcCommand = function (rpcProtocol, rpcHost, rpcPort, rpcInterface, secret) {
var base64Url = $routeParams.url; rpcPort = rpcPort || ariaNgDefaultOptions.rpcPort;
var url = base64Url; rpcInterface = rpcInterface || ariaNgDefaultOptions.rpcInterface;
secret = secret || ariaNgDefaultOptions.secret;
try {
url = base64.urldecode(base64Url);
} catch (ex) {
ariaNgCommonService.showError('URL is not base64 encoded!');
return;
}
$rootScope.loadPromise = newUrlDownload(url);
ariaNgLogService.info('[CommandController] new download: ' + url);
} else if (path.indexOf('/settings/rpc/set/') === 0) {
var rpcProtocol = $routeParams.protocol;
var rpcHost = $routeParams.host;
var rpcPort = $routeParams.port || ariaNgDefaultOptions.rpcPort;
var rpcInterface =$routeParams.interface || ariaNgDefaultOptions.rpcInterface;
var secret = $routeParams.secret || ariaNgDefaultOptions.secret;
ariaNgLogService.info('[CommandController] set rpc: ' + rpcProtocol + '://' + rpcHost + ':' + rpcPort + '/' + rpcInterface + ', secret: ' + secret); ariaNgLogService.info('[CommandController] set rpc: ' + rpcProtocol + '://' + rpcHost + ':' + rpcPort + '/' + rpcInterface + ', secret: ' + secret);
if (!rpcProtocol || (rpcProtocol !== 'http' && rpcProtocol !== 'https' && rpcProtocol !== 'ws' && rpcProtocol !== 'wss')) { if (!rpcProtocol || (rpcProtocol !== 'http' && rpcProtocol !== 'https' && rpcProtocol !== 'ws' && rpcProtocol !== 'wss')) {
ariaNgCommonService.showError('Protocol is invalid!'); ariaNgCommonService.showError('Protocol is invalid!');
return; return false;
} }
if (!rpcHost) { if (!rpcHost) {
ariaNgCommonService.showError('RPC host cannot be empty!'); ariaNgCommonService.showError('RPC host cannot be empty!');
return; return false;
} }
if (secret) { if (secret) {
@ -54,7 +50,7 @@
secret = base64.urldecode(secret); secret = base64.urldecode(secret);
} catch (ex) { } catch (ex) {
ariaNgCommonService.showError('RPC secret is not base64 encoded!'); ariaNgCommonService.showError('RPC secret is not base64 encoded!');
return; return false;
} }
} }
@ -79,8 +75,23 @@
$location.path('/downloading'); $location.path('/downloading');
$window.location.reload(); $window.location.reload();
} }
return true;
};
var doCommand = function (path, params) {
if (path.indexOf('/new/') === 0) {
return doNewTaskCommand(params.url);
} else if (path.indexOf('/settings/rpc/set/') === 0) {
return doSetRpcCommand(params.protocol, params.host, params.port, params.interface, params.secret);
} else { } else {
ariaNgCommonService.showError('Parameter is invalid!'); ariaNgCommonService.showError('Parameter is invalid!');
return false;
}
};
if (!doCommand(path, $routeParams)) {
$location.path('/downloading');
} }
}]); }]);
}()); }());