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

330 lines
13 KiB
JavaScript
Raw Normal View History

2016-05-13 18:09:12 +02:00
(function () {
'use strict';
2016-06-10 13:23:16 +02:00
angular.module('ariaNg').factory('aria2RpcService', ['$q', 'aria2RpcConstants', 'aria2RpcErrors', 'ariaNgCommonService', 'ariaNgSettingService', 'aria2HttpRpcService', 'aria2WebSocketRpcService', function ($q, aria2RpcConstants, aria2RpcErrors, ariaNgCommonService, ariaNgSettingService, aria2HttpRpcService, aria2WebSocketRpcService) {
var protocol = ariaNgSettingService.getProtocol();
var secret = ariaNgSettingService.getSecret();
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-06-10 13:23:16 +02:00
var invoke = function (context) {
var uniqueId = ariaNgCommonService.generateUniqueId();
2016-06-04 09:33:40 +02:00
2016-06-10 13:23:16 +02:00
var requestBody = {
2016-05-13 18:09:12 +02:00
jsonrpc: aria2RpcConstants.rpcServiceVersion,
2016-06-10 13:23:16 +02:00
method: context.methodName,
id: uniqueId,
params: context.params
2016-05-13 18:09:12 +02:00
};
2016-06-10 13:23:16 +02:00
var requestContext = {
uniqueId: uniqueId,
requestBody: requestBody,
successCallback: context.successCallback,
errorCallback: context.errorCallback
};
2016-06-04 11:08:42 +02:00
2016-06-04 09:41:36 +02:00
if (protocol == 'ws' || protocol == 'wss') {
2016-06-10 13:23:16 +02:00
return aria2WebSocketRpcService.request(requestContext);
} else {
2016-06-10 13:23:16 +02:00
return aria2HttpRpcService.request(requestContext);
}
2016-05-13 18:09:12 +02:00
};
2016-06-10 13:23:16 +02:00
var invokeMulti = function (methodFunc, contexts, callback) {
2016-05-30 19:26:41 +02:00
var promises = [];
2016-06-10 13:23:16 +02:00
var hasSuccess = false;
var hasError = false;
var results = [];
2016-05-30 19:26:41 +02:00
for (var i = 0; i < contexts.length; i++) {
2016-06-10 13:23:16 +02:00
contexts[i].callback = function (response) {
results.push(response);
hasSuccess = hasSuccess | response.success;
hasError = hasError | !response.success;
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) {
2016-06-10 13:23:16 +02:00
callback({
hasSuccess: !!hasSuccess,
hasError: !!hasError,
results: results
});
2016-05-30 19:26:41 +02:00
}
});
};
2016-06-10 13:23:16 +02:00
var processError = function (error) {
if (!error || !error.message) {
return false;
}
if (error.message == aria2RpcErrors.Unauthorized.message) {
ariaNgCommonService.showError('rpc.error.' + error.message);
return true;
} else {
ariaNgCommonService.showError(error.message);
return true;
}
};
2016-06-04 09:33:40 +02:00
var buildRequestContext = function () {
2016-06-10 13:23:16 +02:00
var methodName = arguments[0];
var isSystemMethod = checkIsSystemMethod(methodName);
var finalParams = [];
2016-06-04 09:33:40 +02:00
2016-06-10 13:23:16 +02:00
var context = {
methodName: (!isSystemMethod ? getAria2MethodFullName(methodName) : methodName)
};
2016-06-04 09:33:40 +02:00
2016-06-10 13:23:16 +02:00
if (secret && !isSystemMethod) {
finalParams.push(aria2RpcConstants.rpcTokenPrefix + secret);
2016-06-04 09:33:40 +02:00
}
if (arguments.length > 1) {
2016-06-10 13:23:16 +02:00
var invokeContext = arguments[1];
context.successCallback = function (id, result) {
if (invokeContext.callback) {
invokeContext.callback({
id: id,
success: true,
data: result
});
}
};
context.errorCallback = function (id, error) {
var errorProcessed = false;
2016-06-04 09:33:40 +02:00
2016-06-10 13:23:16 +02:00
if (!invokeContext.silent) {
errorProcessed = processError(error);
}
if (invokeContext.callback) {
invokeContext.callback({
id: id,
success: false,
data: error,
errorProcessed: errorProcessed
});
}
};
}
if (arguments.length > 2) {
for (var i = 2; i < arguments.length; i++) {
2016-06-04 09:33:40 +02:00
if (arguments[i] != null && !angular.isUndefined(arguments[i])) {
2016-06-10 13:23:16 +02:00
finalParams.push(arguments[i]);
2016-06-04 09:33:40 +02:00
}
}
2016-06-10 13:23:16 +02:00
}
2016-06-04 09:33:40 +02:00
2016-06-10 13:23:16 +02:00
if (finalParams.length > 0) {
context.params = finalParams;
2016-06-04 09:33:40 +02:00
}
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-10 13:23:16 +02:00
return invoke(buildRequestContext('remove', context, context.gid));
2016-05-13 18:09:12 +02:00
},
forceRemove: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('forceRemove', 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({
2016-06-10 13:23:16 +02:00
silent: !!context.silent,
2016-05-30 19:26:41 +02:00
gid: context.gids[i]
});
}
2016-06-10 13:23:16 +02:00
return invokeMulti(this.forceRemove, contexts, context.callback);
2016-05-13 18:09:12 +02:00
},
pause: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('pause', context, context.gid));
2016-05-13 18:09:12 +02:00
},
pauseAll: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('pauseAll', context));
2016-05-13 18:09:12 +02:00
},
forcePause: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('forcePause', 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({
2016-06-10 13:23:16 +02:00
silent: !!context.silent,
2016-05-30 19:26:41 +02:00
gid: context.gids[i]
});
}
2016-06-10 13:23:16 +02:00
return invokeMulti(this.forcePause, contexts, context.callback);
2016-05-13 18:09:12 +02:00
},
forcePauseAll: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('forcePauseAll', context));
2016-05-13 18:09:12 +02:00
},
unpause: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('unpause', 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({
2016-06-10 13:23:16 +02:00
silent: !!context.silent,
2016-05-30 19:26:41 +02:00
gid: context.gids[i]
});
}
2016-06-10 13:23:16 +02:00
return invokeMulti(this.unpause, contexts, context.callback);
2016-05-13 18:09:12 +02:00
},
unpauseAll: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('unpauseAll', context));
2016-05-13 18:09:12 +02:00
},
tellStatus: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('tellStatus', context, context.gid));
2016-05-13 18:09:12 +02:00
},
getUris: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('getUris', context, context.gid));
2016-05-13 18:09:12 +02:00
},
getFiles: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('getFiles', context, context.gid));
2016-05-13 18:09:12 +02:00
},
getPeers: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('getPeers', context, context.gid));
2016-05-13 18:09:12 +02:00
},
getServers: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('getServers', context, context.gid));
2016-05-13 18:09:12 +02:00
},
tellActive: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('tellActive', context,
2016-06-04 09:33:40 +02:00
angular.isUndefined(context.requestParams) ? null : context.requestParams
));
2016-05-13 18:09:12 +02:00
},
tellWaiting: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('tellWaiting', context,
2016-06-04 09:33:40 +02:00
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-10 13:23:16 +02:00
return invoke(buildRequestContext('tellStopped', context,
2016-06-04 09:33:40 +02:00
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-10 13:23:16 +02:00
return invoke(buildRequestContext('changePosition', 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-10 13:23:16 +02:00
return invoke(buildRequestContext('getOption', context, context.gid));
2016-05-13 18:09:12 +02:00
},
changeOption: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('changeOption', context, context.gid, context.options));
2016-05-13 18:09:12 +02:00
},
getGlobalOption: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('getGlobalOption', context));
2016-05-13 18:09:12 +02:00
},
changeGlobalOption: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('changeGlobalOption', context, context.options));
2016-05-13 18:09:12 +02:00
},
getGlobalStat: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('getGlobalStat', context));
2016-05-13 18:09:12 +02:00
},
purgeDownloadResult: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('purgeDownloadResult', context));
2016-05-13 18:09:12 +02:00
},
removeDownloadResult: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('removeDownloadResult', 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({
2016-06-10 13:23:16 +02:00
silent: !!context.silent,
gid: context.gids[i]
});
}
2016-06-10 13:23:16 +02:00
return invokeMulti(this.removeDownloadResult, contexts, context.callback);
},
2016-05-13 18:09:12 +02:00
getVersion: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('getVersion', context));
2016-05-13 18:09:12 +02:00
},
getSessionInfo: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('getSessionInfo', context));
2016-05-13 18:09:12 +02:00
},
shutdown: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('shutdown', context));
2016-05-13 18:09:12 +02:00
},
forceShutdown: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('forceShutdown', context));
2016-05-13 18:09:12 +02:00
},
saveSession: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('saveSession', context));
2016-05-13 18:09:12 +02:00
},
multicall: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('system.multicall', context, context.methods));
2016-05-13 18:09:12 +02:00
},
listMethods: function (context) {
2016-06-10 13:23:16 +02:00
return invoke(buildRequestContext('system.listMethods', context));
2016-05-13 18:09:12 +02:00
}
};
}]);
})();