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/app/scripts/services/aria2RpcService.js

304 lines
12 KiB
JavaScript
Raw Normal View History

2016-05-13 18:09:12 +02:00
(function () {
'use strict';
2016-05-31 16:51:12 +02:00
angular.module('ariaNg').factory('aria2RpcService', ['$q', 'aria2RpcConstants', 'ariaNgCommonService', 'ariaNgSettingService', 'aria2HttpRpcService', 'aria2WebSocketRpcService', function ($q, aria2RpcConstants, ariaNgCommonService, ariaNgSettingService, aria2HttpRpcService, aria2WebSocketRpcService) {
var protocol = ariaNgSettingService.getProtocol();
2016-06-04 09:33:40 +02:00
var checkIsSystemMethod = function (methodName) {
return methodName.indexOf(aria2RpcConstants.rpcSystemServiceName + '.') == 0;
};
2016-05-30 19:26:41 +02:00
var getAria2MethodFullName = function (methodName) {
return aria2RpcConstants.rpcServiceName + '.' + methodName;
};
2016-05-13 18:09:12 +02:00
var invoke = function (method, context) {
2016-06-04 09:33:40 +02:00
var isSystemMethod = checkIsSystemMethod(method);
var rpcSecretToken = ariaNgSettingService.getSecret();
var finalParams = [];
if (rpcSecretToken && !isSystemMethod) {
finalParams.push(aria2RpcConstants.rpcTokenPrefix + rpcSecretToken);
}
if (angular.isArray(context.params) && context.params.length > 0) {
for (var i = 0; i < context.params.length; i++) {
finalParams.push(context.params[i]);
}
}
2016-05-31 16:51:12 +02:00
context.uniqueId = ariaNgCommonService.generateUniqueId();
2016-05-13 18:09:12 +02:00
context.requestBody = {
jsonrpc: aria2RpcConstants.rpcServiceVersion,
2016-06-04 09:33:40 +02:00
method: (!isSystemMethod ? getAria2MethodFullName(method) : method),
id: context.uniqueId
2016-05-13 18:09:12 +02:00
};
2016-06-04 09:33:40 +02:00
if (finalParams.length > 0) {
context.requestBody.params = finalParams;
}
2016-06-04 11:08:42 +02:00
if (!context.silent) {
context.errorCallback = function (error) {
if (!error || !error.message) {
return;
}
if (error.message == 'Unauthorized') {
ariaNgCommonService.alert('rpc.error.' + error.message);
return;
}
}
}
2016-06-04 09:41:36 +02:00
if (protocol == 'ws' || protocol == 'wss') {
return aria2WebSocketRpcService.request(context);
} else {
return aria2HttpRpcService.request(context);
}
2016-05-13 18:09:12 +02:00
};
2016-05-30 19:26:41 +02:00
var invokeMulti = function (methodFunc, contexts, keyProperty, callback) {
var promises = [];
var results = [];
2016-05-30 19:26:41 +02:00
for (var i = 0; i < contexts.length; i++) {
contexts[i].callback = function (result) {
results.push(result);
2016-05-30 19:26:41 +02:00
};
promises.push(methodFunc(contexts[i]));
}
return $q.all(promises).finally(function () {
2016-05-30 19:26:41 +02:00
if (callback) {
callback(results);
}
});
};
2016-06-04 09:33:40 +02:00
var buildRequestContext = function () {
var context = {};
if (arguments.length > 0) {
var invokeContext = arguments[0];
context.silent = invokeContext.silent === true;
context.callback = invokeContext.callback;
}
if (arguments.length > 1) {
var params = [];
for (var i = 1; i < arguments.length; i++) {
if (arguments[i] != null && !angular.isUndefined(arguments[i])) {
params.push(arguments[i]);
}
}
if (params.length > 0) {
context.params = params;
}
}
return context;
};
2016-05-13 18:09:12 +02:00
return {
2016-05-30 19:26:41 +02:00
getBasicTaskParams: function () {
return [
'gid',
'totalLength',
'completedLength',
'uploadSpeed',
'downloadSpeed',
'connections',
'numSeeders',
'seeder',
'status'
];
},
getFullTaskParams: function () {
var requestParams = this.getBasicTaskParams();
requestParams.push('files');
requestParams.push('bittorrent');
return requestParams;
},
// addUri: function (context) {
// return invoke('addUri', context);
// },
// addTorrent: function (context) {
// return invoke('addTorrent', context);
// },
// addMetalink: function (context) {
// return invoke('addMetalink', context);
// },
2016-05-13 18:09:12 +02:00
remove: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('remove', buildRequestContext(context, context.gid));
2016-05-13 18:09:12 +02:00
},
forceRemove: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('forceRemove', buildRequestContext(context, context.gid));
2016-05-30 19:26:41 +02:00
},
forceRemoveMulti: function (context) {
var contexts = [];
for (var i = 0; i < context.gids.length; i++) {
contexts.push({
gid: context.gids[i]
});
}
return invokeMulti(this.forceRemove, contexts, 'gid', context.callback);
2016-05-13 18:09:12 +02:00
},
pause: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('pause', buildRequestContext(context, context.gid));
2016-05-13 18:09:12 +02:00
},
pauseAll: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('pauseAll', buildRequestContext(context));
2016-05-13 18:09:12 +02:00
},
forcePause: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('forcePause', buildRequestContext(context, context.gid));
2016-05-30 19:26:41 +02:00
},
forcePauseMulti: function (context) {
var contexts = [];
for (var i = 0; i < context.gids.length; i++) {
contexts.push({
gid: context.gids[i]
});
}
return invokeMulti(this.forcePause, contexts, 'gid', context.callback);
2016-05-13 18:09:12 +02:00
},
forcePauseAll: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('forcePauseAll', buildRequestContext(context));
2016-05-13 18:09:12 +02:00
},
unpause: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('unpause', buildRequestContext(context, context.gid));
2016-05-30 19:26:41 +02:00
},
unpauseMulti: function (context) {
var contexts = [];
for (var i = 0; i < context.gids.length; i++) {
contexts.push({
gid: context.gids[i]
});
}
return invokeMulti(this.unpause, contexts, 'gid', context.callback);
2016-05-13 18:09:12 +02:00
},
unpauseAll: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('unpauseAll', buildRequestContext(context));
2016-05-13 18:09:12 +02:00
},
tellStatus: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('tellStatus', buildRequestContext(context, context.gid));
2016-05-13 18:09:12 +02:00
},
getUris: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('getUris', buildRequestContext(context, context.gid));
2016-05-13 18:09:12 +02:00
},
getFiles: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('getFiles', buildRequestContext(context, context.gid));
2016-05-13 18:09:12 +02:00
},
getPeers: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('getPeers', buildRequestContext(context, context.gid));
2016-05-13 18:09:12 +02:00
},
getServers: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('getServers', buildRequestContext(context, context.gid));
2016-05-13 18:09:12 +02:00
},
tellActive: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('tellActive', buildRequestContext(context,
angular.isUndefined(context.requestParams) ? null : context.requestParams
));
2016-05-13 18:09:12 +02:00
},
tellWaiting: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('tellWaiting', buildRequestContext(context,
angular.isUndefined(context.offset) ? 0 : context.offset,
angular.isUndefined(context.num) ? 1000 : context.num,
angular.isUndefined(context.requestParams) ? null : context.requestParams
));
2016-05-13 18:09:12 +02:00
},
tellStopped: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('tellStopped', buildRequestContext(context,
angular.isUndefined(context.offset) ? 0 : context.offset,
angular.isUndefined(context.num) ? 1000 : context.num,
angular.isUndefined(context.requestParams) ? null : context.requestParams
));
2016-05-30 19:26:41 +02:00
},
changePosition: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('changePosition', buildRequestContext(context, context.gid, context.pos, context.how));
},
2016-05-30 19:26:41 +02:00
// changeUri: function (context) {
// return invoke('changeUri', context);
// },
2016-05-13 18:09:12 +02:00
getOption: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('getOption', buildRequestContext(context, context.gid));
2016-05-13 18:09:12 +02:00
},
changeOption: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('changeOption', buildRequestContext(context, context.gid, context.options));
2016-05-13 18:09:12 +02:00
},
getGlobalOption: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('getGlobalOption', buildRequestContext(context));
2016-05-13 18:09:12 +02:00
},
changeGlobalOption: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('changeGlobalOption', buildRequestContext(context, context.options));
2016-05-13 18:09:12 +02:00
},
getGlobalStat: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('getGlobalStat', buildRequestContext(context));
2016-05-13 18:09:12 +02:00
},
purgeDownloadResult: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('purgeDownloadResult', buildRequestContext(context));
2016-05-13 18:09:12 +02:00
},
removeDownloadResult: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('removeDownloadResult', buildRequestContext(context, context.gid));
2016-05-13 18:09:12 +02:00
},
removeDownloadResultMulti: function (context) {
var contexts = [];
for (var i = 0; i < context.gids.length; i++) {
contexts.push({
gid: context.gids[i]
});
}
return invokeMulti(this.removeDownloadResult, contexts, 'gid', context.callback);
},
2016-05-13 18:09:12 +02:00
getVersion: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('getVersion', buildRequestContext(context));
2016-05-13 18:09:12 +02:00
},
getSessionInfo: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('getSessionInfo', buildRequestContext(context));
2016-05-13 18:09:12 +02:00
},
shutdown: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('shutdown', buildRequestContext(context));
2016-05-13 18:09:12 +02:00
},
forceShutdown: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('forceShutdown', buildRequestContext(context));
2016-05-13 18:09:12 +02:00
},
saveSession: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('saveSession', buildRequestContext(context));
2016-05-13 18:09:12 +02:00
},
multicall: function (context) {
2016-05-30 19:26:41 +02:00
var requestContext = {
params: [],
2016-06-04 09:33:40 +02:00
silent: context.silent === true,
2016-05-30 19:26:41 +02:00
callback: context.callback
};
if (angular.isArray(context.methods) && context.methods.length > 0) {
for (var i = 0; i < context.methods.length; i++) {
var method = context.methods[i];
requestContext.params.push([method]);
}
}
return invoke('system.multicall', requestContext);
2016-05-13 18:09:12 +02:00
},
listMethods: function (context) {
2016-06-04 09:33:40 +02:00
return invoke('system.listMethods', buildRequestContext(context));
2016-05-13 18:09:12 +02:00
}
};
}]);
})();