From e58ffa4531dd5249b564bf59936d7db165eeec67 Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Tue, 24 Jul 2018 13:17:40 +0300 Subject: [PATCH] feat(cleaning): fix cleaning checking and removing torrents (also display cleaning status in more details) #52 --- src/app/filters-page.js | 31 +++++++++++++++++++++++++++++++ src/background/api.js | 22 +++++++++++++++++++--- translations/en.json | 5 ++++- translations/ru.json | 5 ++++- translations/ua.json | 5 ++++- 5 files changed, 62 insertions(+), 6 deletions(-) diff --git a/src/app/filters-page.js b/src/app/filters-page.js index bb2710f..4249a08 100644 --- a/src/app/filters-page.js +++ b/src/app/filters-page.js @@ -33,9 +33,29 @@ export default class ConfigPage extends Page { archive: __('Archives'), disc: __('Discs/ISO') } + this.removed = 0 + this.removedMax = 0 } componentDidMount() { this.loadSettings() + + let time = Date.now() - 1000 + this.cleanTorrent = (removed, max, status) => { + if(Date.now() - time < 400) + return + time = Date.now() + + this.removed = removed + this.removedMax = max + this.realRemove = (status !== 'check') + this.forceUpdate() + }; + window.torrentSocket.on('cleanTorrent', this.cleanTorrent); + } + componentWillUnmount() + { + if(this.cleanTorrent) + window.torrentSocket.off('cleanTorrent', this.cleanTorrent); } loadSettings() { window.torrentSocket.emit('config', window.customLoader((options) => { @@ -228,6 +248,13 @@ export default class ConfigPage extends Page { : null } + { + this.removed > 0 && !this.toRemove && !this.toRemoveProbably + ? +
{this.realRemove ? __('removing') : __('calculation')}...: {this.removed}{this.removedMax > 0 ? '/' + this.removedMax : ''}
+ : + null + } { this.settingsSavedMessage @@ -237,12 +264,16 @@ export default class ConfigPage extends Page {
{ + this.toRemoveProbably = null + this.toRemove = null window.torrentSocket.emit('removeTorrents', true, window.customLoader((toRemove) => { this.toRemoveProbably = toRemove this.forceUpdate() })); }} /> { + this.toRemoveProbably = null + this.toRemove = null window.torrentSocket.emit('removeTorrents', false, window.customLoader((toRemove) => { this.toRemove = toRemove this.forceUpdate() diff --git a/src/background/api.js b/src/background/api.js index bd6db4a..9a3e485 100644 --- a/src/background/api.js +++ b/src/background/api.js @@ -3,6 +3,7 @@ const forBigTable = require('./forBigTable') const compareVersions = require('compare-versions'); const getTorrent = require('./gettorrent') const _ = require('lodash') +const asyncForEach = require('./asyncForEach') module.exports = async ({ sphinx, @@ -809,29 +810,44 @@ module.exports = async ({ }))) }) + let removeProtect = false recive('removeTorrents', (checkOnly = true, callback) => { + if(removeProtect) + return + removeProtect = true + console.log('checktorrents call') const toRemove = [] - const done = () => { + const done = async () => { console.log('torrents to remove founded', toRemove.length) if(checkOnly) { callback(toRemove.length) + removeProtect = false return } - toRemove.forEach(torrent => removeTorrentFromDB(torrent)) + await asyncForEach(toRemove, async (torrent, index) => { + await removeTorrentFromDB(torrent) + send('cleanTorrent', index + 1, toRemove.length, 'clean'); + }) callback(toRemove.length) + removeProtect = false console.log('removed torrents by filter:', toRemove.length) } + let i = 1 forBigTable(sphinx, 'torrents', (torrent) => { setupTorrentRecord(torrent) if(!checkTorrent(torrent)) - toRemove.push(torrent) + { + toRemove.push({hash: torrent.hash}) + // send info about cleaning takes + send('cleanTorrent', i++, 0, 'check'); + } }, done) }) diff --git a/translations/en.json b/translations/en.json index 6bef30b..d728f09 100644 --- a/translations/en.json +++ b/translations/en.json @@ -181,6 +181,9 @@ "* - enabled means ignoring all adult content": "* - enabled means ignoring all adult content", "disable some categories": "disable some categories", "Discs/ISO": "Discs/ISO", - "Torrents to clean": "Torrents to clean" + "Torrents to clean": "Torrents to clean", + "calculation": "calculation", + "removing": "removing", + "Torrents cleaned": "Torrents cleaned" } } \ No newline at end of file diff --git a/translations/ru.json b/translations/ru.json index 155305e..444f182 100644 --- a/translations/ru.json +++ b/translations/ru.json @@ -181,6 +181,9 @@ "* - enabled means ignoring all adult content": "* - означает игнорирование всего контента для взрослых", "disable some categories": "отключить некоторые категории", "Discs/ISO": "Диски/Образы", - "Torrents to clean": "Торрентов для очистки" + "Torrents to clean": "Торрентов для очистки", + "calculation": "подсчитывается", + "removing": "удаляется", + "Torrents cleaned": "Torrents cleaned" } } \ No newline at end of file diff --git a/translations/ua.json b/translations/ua.json index 283113f..a033b45 100644 --- a/translations/ua.json +++ b/translations/ua.json @@ -181,6 +181,9 @@ "* - enabled means ignoring all adult content": "* - enabled means ignoring all adult content", "disable some categories": "disable some categories", "Discs/ISO": "Discs/ISO", - "Torrents to clean": "Torrents to clean" + "Torrents to clean": "Torrents to clean", + "calculation": "calculation", + "removing": "removing", + "Torrents cleaned": "Torrents cleaned" } } \ No newline at end of file