server: migrate add torrents functions to clientGatewayService

This commit is contained in:
Jesse Chan
2020-10-08 19:29:13 +08:00
parent 2940c2379b
commit 79a7c9c6f0
9 changed files with 211 additions and 267 deletions
-108
View File
@@ -6,26 +6,6 @@ import util from 'util';
import {clientSettingsMap} from '../../shared/constants/clientSettingsMap';
import rTorrentPropMap from '../util/rTorrentPropMap';
const addTagsToRequest = (tagsArr, requestParameters) => {
if (tagsArr && tagsArr.length) {
const tags = tagsArr
.reduce((accumulator, currentTag) => {
const tag = encodeURIComponent(currentTag.trim());
if (tag !== '' && accumulator.indexOf(tag) === -1) {
accumulator.push(tag);
}
return accumulator;
}, [])
.join(',');
requestParameters.push(`d.custom1.set="${tags}"`);
}
return requestParameters;
};
const getEnsuredArray = (item) => {
if (!util.isArray(item)) {
return [item];
@@ -111,62 +91,6 @@ class ClientRequest {
this.clientRequestManager.methodCall('system.multicall', [this.requests]).then(handleSuccess).catch(handleError);
}
// TODO: Separate these and add support for additional clients.
// rTorrent method calls.
addFiles(options) {
const {files, path: destinationPath, isBasePath, start, tags: tagsArr} = options;
files.forEach((file) => {
const methodCall = start ? 'load.raw_start' : 'load.raw';
const timeAdded = Math.floor(Date.now() / 1000);
let parameters = ['', Buffer.from(file, 'base64')];
if (destinationPath) {
if (isBasePath) {
parameters.push(`d.directory_base.set="${destinationPath}"`);
} else {
parameters.push(`d.directory.set="${destinationPath}"`);
}
}
parameters = addTagsToRequest(tagsArr, parameters);
// parameters.push(`d.custom.set=x-filename,${file.originalname}`);
parameters.push(`d.custom.set=addtime,${timeAdded}`);
this.requests.push(getMethodCall(methodCall, parameters));
});
}
addURLs(options) {
const {path: destinationPath, isBasePath, start, tags: tagsArr} = options;
const urls = getEnsuredArray(options.urls);
urls.forEach((url) => {
let methodCall = 'load.start';
let parameters = ['', url];
const timeAdded = Math.floor(Date.now() / 1000);
if (destinationPath) {
if (isBasePath) {
parameters.push(`d.directory_base.set="${destinationPath}"`);
} else {
parameters.push(`d.directory.set="${destinationPath}"`);
}
}
parameters = addTagsToRequest(tagsArr, parameters);
parameters.push(`d.custom.set=addtime,${timeAdded}`);
if (!start) {
methodCall = 'load.normal';
}
this.requests.push(getMethodCall(methodCall, parameters));
});
}
fetchSettings(options) {
let {requestedSettings} = options;
@@ -194,38 +118,6 @@ class ClientRequest {
this.requests.push(getMethodCall('t.multicall', trackerParams));
}
getTorrentList(options) {
this.requests.push(getMethodCall('d.multicall2', options.props));
}
getTransferData() {
Object.keys(rTorrentPropMap.transferData).forEach((key) => {
this.requests.push(getMethodCall(rTorrentPropMap.transferData[key]));
});
}
listMethods(options) {
const args = getEnsuredArray(options.args);
this.requests.push(getMethodCall(options.method, [args]));
}
setDownloadPath(options) {
const hashes = getEnsuredArray(options.hashes);
let pathMethod;
if (options.isBasePath) {
pathMethod = 'd.directory_base.set';
} else {
pathMethod = 'd.directory.set';
}
hashes.forEach((hash) => {
this.requests.push(getMethodCall(pathMethod, [hash, options.path]));
this.requests.push(getMethodCall('d.open', [hash]));
this.requests.push(getMethodCall('d.close', [hash]));
});
}
setFilePriority(options) {
const indices = getEnsuredArray(options.indices);
const hashes = getEnsuredArray(options.hashes);
-70
View File
@@ -4,77 +4,15 @@ import sanitize from 'sanitize-filename';
import {series} from 'async';
import tar from 'tar-stream';
import {accessDeniedError, createDirectory, isAllowedPath, sanitizePath} from '../util/fileUtil';
import ClientRequest from './ClientRequest';
import clientResponseUtil from '../util/clientResponseUtil';
import {clientSettingsBiMap} from '../../shared/constants/clientSettingsMap';
import settings from './settings';
import torrentFilePropsMap from '../../shared/constants/torrentFilePropsMap';
import torrentPeerPropsMap from '../../shared/constants/torrentPeerPropsMap';
import torrentFileUtil from '../util/torrentFileUtil';
import torrentTrackerPropsMap from '../../shared/constants/torrentTrackerPropsMap';
const client = {
addFiles(user, services, options, callback) {
const {destination: destinationPath, files, isBasePath, start, tags} = options;
const resolvedPath = sanitizePath(destinationPath);
if (!isAllowedPath(resolvedPath)) {
callback(null, accessDeniedError());
return;
}
createDirectory({path: resolvedPath});
// Each torrent is sent individually because rTorrent accepts a total
// filesize of 524 kilobytes or less. This allows the user to send many
// torrent files reliably.
files.forEach((file, index) => {
const fileRequest = new ClientRequest(user, services);
fileRequest.addFiles({
files: [file],
path: resolvedPath,
isBasePath,
start,
tags,
});
// Set the callback for only the last request.
if (index === files.length - 1) {
fileRequest.onComplete((response, error) => {
services.torrentService.fetchTorrentList();
callback(response, error);
});
}
fileRequest.send();
});
settings.set(user, {id: 'startTorrentsOnLoad', data: start === 'true' || start === true});
},
addUrls(user, services, data, callback) {
const {urls, destination, isBasePath, start, tags} = data;
const request = new ClientRequest(user, services);
const resolvedPath = sanitizePath(destination);
if (!isAllowedPath(resolvedPath)) {
callback(null, accessDeniedError());
return;
}
createDirectory({path: resolvedPath});
request.addURLs({
urls,
path: resolvedPath,
isBasePath,
start,
tags,
});
request.onComplete(callback);
request.send();
settings.set(user, {id: 'startTorrentsOnLoad', data: start});
},
downloadFiles(user, services, hash, fileString, res) {
try {
const selectedTorrent = services.torrentService.getTorrent(hash);
@@ -209,14 +147,6 @@ const client = {
request.send();
},
listMethods(user, services, method, args, callback) {
const request = new ClientRequest(user, services);
request.listMethods({method, args});
request.onComplete(callback);
request.send();
},
setFilePriority(user, services, hashes, data, callback) {
const {indices, priority} = data;
const request = new ClientRequest(user, services);