From 7a4f9a7e5156d093fd760a91972df3b984acd86c Mon Sep 17 00:00:00 2001 From: John Furrow Date: Sun, 8 Nov 2015 16:02:51 -0800 Subject: [PATCH] Improve handling of multiple torrent file URLs --- server/models/client.js | 49 ++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/server/models/client.js b/server/models/client.js index d6c02a88..2161a9d6 100644 --- a/server/models/client.js +++ b/server/models/client.js @@ -10,28 +10,37 @@ function client() { }; client.prototype.add = function(data, callback) { - var commands = [ - [ - { - methodName: 'execute', - params: [ - 'mkdir', - '-p', - data.destination - ] - }, - { - methodName: 'load.start', - params: [ - '', - data.url, - 'd.directory.set="' + data.destination + '"' - ] - } - ] + var multicall = [ + [] ]; - rTorrent.get('system.multicall', commands).then(function(data) { + if (data.destination !== null && data.destination !== '') { + multicall[0].push({ + methodName: 'execute', + params: [ + 'mkdir', + '-p', + data.destination + ] + }); + } + + var torrentsAdded = 0; + + while (torrentsAdded < data.urls.length) { + multicall[0].push({ + methodName: 'load.start', + params: [ + '', + data.urls[torrentsAdded], + 'd.directory.set="' + data.destination + '"' + ] + }); + + torrentsAdded++; + } + + rTorrent.get('system.multicall', multicall).then(function(data) { callback(null, data); }, function(error) { callback(error, null);