From fc41449385ec8652ba23ebb218cf4f302a93489c Mon Sep 17 00:00:00 2001 From: John Furrow Date: Mon, 11 Sep 2017 19:54:41 -0400 Subject: [PATCH] When moving files, remember start/stopped status --- server/models/ClientRequest.js | 23 +++++++++++------- server/models/client.js | 36 ++++++++++++++++++---------- shared/constants/torrentStatusMap.js | 4 ++-- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/server/models/ClientRequest.js b/server/models/ClientRequest.js index 6f422440..44caf82c 100644 --- a/server/models/ClientRequest.js +++ b/server/models/ClientRequest.js @@ -12,7 +12,8 @@ let util = require('util'); let clientSettingsMap = require('../../shared/constants/clientSettingsMap'); let rTorrentPropMap = require('../util/rTorrentPropMap'); let scgi = require('../util/scgi'); -let stringUtil = require('../../shared/util/stringUtil'); +const torrentService = require('../services/torrentService'); +const torrentStatusMap = require('../../shared/constants/torrentStatusMap'); class ClientRequest { constructor(options) { @@ -183,11 +184,16 @@ class ClientRequest { } checkHash(options) { - let hashes = this.getEnsuredArray(options.hashes); + const hashes = this.getEnsuredArray(options.hashes); + const hashesToStop = hashes.filter(hash => { + return torrentService.getTorrent(hash).status.includes(torrentStatusMap.stopped); + }); - hashes.forEach((hash) => { + hashes.forEach(hash => { this.requests.push(this.getMethodCall('d.check_hash', [hash])); }); + + this.stopTorrents({hashes: hashesToStop}); } createDirectory(options) { @@ -247,15 +253,14 @@ class ClientRequest { } moveTorrents(options) { - let hashes = this.getEnsuredArray(options.hashes); - let destinationPath = options.destinationPath; - let filenames = this.getEnsuredArray(options.filenames); - let sourcePaths = this.getEnsuredArray(options.sourcePaths); + const destinationPath = options.destinationPath; + const filenames = this.getEnsuredArray(options.filenames); + const sourcePaths = this.getEnsuredArray(options.sourcePaths); sourcePaths.forEach((source, index) => { let callback = () => {}; - let destination = `${destinationPath}${path.sep}${filenames[index]}`; - let isLastRequest = index + 1 === sourcePaths.length; + const destination = `${destinationPath}${path.sep}${filenames[index]}`; + const isLastRequest = index + 1 === sourcePaths.length; if (isLastRequest) { callback = this.handleSuccess.bind(this); diff --git a/server/models/client.js b/server/models/client.js index 1d6c03c6..c48bfd48 100644 --- a/server/models/client.js +++ b/server/models/client.js @@ -11,6 +11,7 @@ const clientSettingsMap = require('../../shared/constants/clientSettingsMap'); const settings = require('./settings'); const torrentFilePropsMap = require('../../shared/constants/torrentFilePropsMap'); const torrentPeerPropsMap = require('../../shared/constants/torrentPeerPropsMap'); +const torrentStatusMap = require('../../shared/constants/torrentStatusMap'); const torrentService = require('../services/torrentService'); const torrentTrackerPropsMap = require('../../shared/constants/torrentTrackerPropsMap'); @@ -18,7 +19,7 @@ var client = { addFiles (req, callback) { let files = req.files; let path = req.body.destination; - let isBasePath = req.body.isBasePath === 'true'; + let isBasePath = req.body.isBasePath; let request = new ClientRequest(); let start = req.body.start; let tags = req.body.tags; @@ -234,36 +235,45 @@ var client = { moveTorrents (data, callback) { let destinationPath = data.destination; - let isBasePath = data.isBasePath === 'true'; + let isBasePath = data.isBasePath; let hashes = data.hashes; let filenames = data.filenames; let moveFiles = data.moveFiles; let sourcePaths = data.sources; let mainRequest = new ClientRequest(); - let startTorrents = () => { - let startTorrentsRequest = new ClientRequest(); - startTorrentsRequest.startTorrents({hashes}); - startTorrentsRequest.onComplete(callback); - startTorrentsRequest.send(); - }; + const hashesToRestart = hashes.filter((hash) => { + return !torrentService.getTorrent(hash).status.includes(torrentStatusMap.stopped); + }); - let checkHash = () => { - let checkHashRequest = new ClientRequest(); + let afterCheckHash; + + if (hashesToRestart.length) { + afterCheckHash = () => { + const startTorrentsRequest = new ClientRequest(); + startTorrentsRequest.startTorrents({hashes: hashesToRestart}); + startTorrentsRequest.onComplete(callback); + startTorrentsRequest.send(); + }; + } else { + afterCheckHash = callback; + } + + const checkHash = () => { + const checkHashRequest = new ClientRequest(); checkHashRequest.checkHash({hashes}); checkHashRequest.onComplete(afterCheckHash); checkHashRequest.send(); }; - let moveTorrents = () => { - let moveTorrentsRequest = new ClientRequest(); + const moveTorrents = () => { + const moveTorrentsRequest = new ClientRequest(); moveTorrentsRequest.onComplete(checkHash); moveTorrentsRequest.moveTorrents({ filenames, sourcePaths, destinationPath }); }; - let afterCheckHash = startTorrents; let afterSetPath = checkHash; if (moveFiles) { diff --git a/shared/constants/torrentStatusMap.js b/shared/constants/torrentStatusMap.js index ff3ff714..0369439c 100644 --- a/shared/constants/torrentStatusMap.js +++ b/shared/constants/torrentStatusMap.js @@ -1,8 +1,8 @@ 'use strict'; -let objectUtil = require('../util/objectUtil'); +const objectUtil = require('../util/objectUtil'); -let torrentStatusMap = objectUtil.reflect({ +const torrentStatusMap = objectUtil.reflect({ ch: 'checking', sd: 'seeding', p: 'paused',