From 5348d1f88f4581440fada516b697165d6e51b8ee Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Sun, 1 Apr 2018 22:36:30 +0300 Subject: [PATCH] feat(filter): torrents filters (basic maxFiles filter) --- src/app/filters-page.js | 128 ++++++++++++++++++++ src/app/header.js | 54 +++++++++ src/app/router.js | 6 + src/background/api.js | 42 ++++++- src/background/config.js | 4 + src/background/menu/config_menu_template.js | 7 ++ src/background/spider.js | 25 +++- 7 files changed, 264 insertions(+), 2 deletions(-) create mode 100644 src/app/filters-page.js diff --git a/src/app/filters-page.js b/src/app/filters-page.js new file mode 100644 index 0000000..146bc35 --- /dev/null +++ b/src/app/filters-page.js @@ -0,0 +1,128 @@ +import React from 'react'; +import Page from './page'; + +import Toggle from 'material-ui/Toggle'; +import RaisedButton from 'material-ui/RaisedButton'; +import TextField from 'material-ui/TextField' +import Slider from 'material-ui/Slider' + +import fs from 'fs' +const {dialog} = require('electron').remote + +export default class ConfigPage extends Page { + constructor(props) { + super(props) + this.setTitle('Rats filters'); + this.options = {} + } + componentDidMount() { + this.loadSettings() + } + loadSettings() { + window.torrentSocket.emit('config', window.customLoader((options) => { + this.options = options; + console.log(this.options) + this.forceUpdate(); + })); + } + saveSettings() { + window.torrentSocket.emit('setConfig', this.options) + this.settingsSavedMessage = true + this.forceUpdate() + setTimeout(() => { + this.settingsSavedMessage = false + this.forceUpdate() + }, 1000) + } + render() { + return ( +
+
+ { + window.router('/') + }} /> +
+ +
+ +
+
+
Max files per torrent (current: {this.options.filters && this.options.filters.maxFiles})
+ { + this.options.filters.maxFiles = value + this.forceUpdate() + }} + /> + { + if(!this.options.filters) + return + + this.options.filters.maxFiles = parseInt(value) + this.forceUpdate() + }} + /> +
+
* 0 - Disabled. +
+
+ + +
+ { + window.torrentSocket.emit('removeTorrents', true, window.customLoader((toRemove) => { + this.toRemoveProbably = toRemove + this.forceUpdate() + })); + }} /> + { + window.torrentSocket.emit('removeTorrents', false, window.customLoader((toRemove) => { + this.toRemove = toRemove + this.forceUpdate() + })); + }} /> +
+ + { + this.toRemoveProbably && this.toRemoveProbably > 0 + ? +
Torrents to clean: {this.toRemoveProbably}
+ : + null + } + { + this.toRemove && this.toRemove > 0 + ? +
Torrents cleaned: {this.toRemove}
+ : + null + } + + { + this.settingsSavedMessage + && +
Settings saved
+ } + +
+ { + this.saveSettings() + }} /> +
+ +
+ +
+ ); + } +} diff --git a/src/app/header.js b/src/app/header.js index 1447ce1..f311ccf 100644 --- a/src/app/header.js +++ b/src/app/header.js @@ -49,6 +49,60 @@ const Header = (props) => { C302.1,704.538,332,675.438,368.3,675.837z"/> + { + window.router('/filters') + }} + fill='white' style={{height: 45, margin: 4}} viewBox="0 0 512 512"> + + + + + + + + + + + + + + + + + + + + + + + + + + Welcome to ROTB! This is file search engine based on the torrents from the internet. diff --git a/src/app/router.js b/src/app/router.js index cabdee2..77d72bb 100644 --- a/src/app/router.js +++ b/src/app/router.js @@ -8,6 +8,7 @@ import ConfigPage from './config-page.js' import TopPage from './top-page.js' import DownloadPage from './download-page.js' import ChangelogPage from './changelog-page.js' +import FiltersPage from './filters-page.js' let routers = {} const router = (page, callback) => { @@ -75,6 +76,11 @@ router('/config', () => { PagesPie.instance().open(ConfigPage, {replace: 'all'}); }); +router('/filters', () => { + //singleton + PagesPie.instance().open(FiltersPage, {replace: 'all'}); +}); + router('/top', () => { //singleton PagesPie.instance().open(TopPage, {replace: 'all'}); diff --git a/src/background/api.js b/src/background/api.js index 979d4e4..93d8e39 100644 --- a/src/background/api.js +++ b/src/background/api.js @@ -11,7 +11,9 @@ module.exports = ({ spider, upnp, crypto, - insertTorrentToDB + insertTorrentToDB, + removeTorrentFromDB, + checkTorrent }) => { let torrentClientHashMap = {} @@ -601,6 +603,44 @@ module.exports = ({ }))) }) + recive('removeTorrents', (checkOnly = true, callback) => + { + console.log('checktorrents call') + + const toRemove = [] + + const done = () => { + console.log('torrents to remove founded', toRemove.length) + if(checkOnly) + { + callback(toRemove.length) + return + } + + toRemove.forEach(torrent => removeTorrentFromDB(torrent)) + callback(toRemove.length) + console.log('removed torrents by filter:', toRemove.length) + } + + const checker = (index = 0) => { + sphinx.query(`SELECT * FROM torrents LIMIT ${index},50000`, (err, torrents) => { + if(err || torrents.length == 0) + { + done() + return + } + + torrents.forEach((torrent) => { + if(!checkTorrent(torrent)) + toRemove.push(torrent) + }) + + checker(index + torrents.length) + }); + } + checker() + }) + let socketIPV4 = () => { let ip = socket.request.connection.remoteAddress; if (ipaddr.IPv4.isValid(ip)) { diff --git a/src/background/config.js b/src/background/config.js index 21bac4e..7d60d1a 100644 --- a/src/background/config.js +++ b/src/background/config.js @@ -36,6 +36,10 @@ let config = { timeout: 5000 }, + filters: { + maxFiles: 0, + }, + cleanup: true, cleanupDiscLimit: 7 * 1024 * 1024 * 1024, spaceQuota: false, diff --git a/src/background/menu/config_menu_template.js b/src/background/menu/config_menu_template.js index 62e447d..152acf3 100644 --- a/src/background/menu/config_menu_template.js +++ b/src/background/menu/config_menu_template.js @@ -9,6 +9,13 @@ export const settingsMenuTemplate = { click: () => { BrowserWindow.getFocusedWindow().webContents.send('url', '/config') } + }, + { + label: "Torrents filters", + accelerator: "CmdOrCtrl+\\", + click: () => { + BrowserWindow.getFocusedWindow().webContents.send('url', '/filters') + } } ] }; diff --git a/src/background/spider.js b/src/background/spider.js index 3af102e..5f3898c 100644 --- a/src/background/spider.js +++ b/src/background/spider.js @@ -447,6 +447,16 @@ const cleanupTorrents = (cleanTorrents = 1) => { */ } +const checkTorrent = (torrent) => { + if(config.filters.maxFiles > 0 && torrent.files > config.filters.maxFiles) + { + console.log('ignore', torrent.name, 'because files', torrent.files, '>', config.filters.maxFiles) + return false + } + + return true +} + const insertTorrentToDB = (torrent) => { if(!torrent) return @@ -463,6 +473,11 @@ const insertTorrentToDB = (torrent) => { delete torrent.contenttype; } + if(!checkTorrent(torrent)) + { + return + } + const { filesList } = torrent delete torrent.filesList; @@ -533,6 +548,12 @@ const insertTorrentToDB = (torrent) => { }) } +const removeTorrentFromDB = (torrent) => { + const {hash} = torrent + mysqlSingle.query('DELETE FROM torrents WHERE hash = ?', hash) + mysqlSingle.query('DELETE FROM files WHERE hash = ?', hash) +} + const updateTorrent = (metadata, infohash, rinfo) => { console.log('finded torrent', metadata.info.name, ' and add to database'); @@ -713,7 +734,9 @@ API({ spider, upnp, crypto, - insertTorrentToDB + insertTorrentToDB, + removeTorrentFromDB, + checkTorrent }) if(config.indexer) {