feat(filter): torrents filters (basic maxFiles filter)

This commit is contained in:
Alexey Kasyanchuk
2018-04-01 22:36:30 +03:00
parent 534999d1cb
commit 5348d1f88f
7 changed files with 264 additions and 2 deletions

View File

@ -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)) {

View File

@ -36,6 +36,10 @@ let config = {
timeout: 5000
},
filters: {
maxFiles: 0,
},
cleanup: true,
cleanupDiscLimit: 7 * 1024 * 1024 * 1024,
spaceQuota: false,

View File

@ -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')
}
}
]
};

View File

@ -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) {