From f7166103dfe68649de1027bc6e5f47de0f57376a Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Fri, 3 Sep 2021 12:10:29 +0300 Subject: [PATCH] fix(db): moving content type to uint values --- src/app/content.js | 21 ++++++++++++++++++++- src/background/api.js | 9 +++++---- src/background/dbPatcher.js | 4 ++-- src/background/sphinx.js | 6 +++--- src/background/spider.js | 21 ++++++++++++--------- 5 files changed, 42 insertions(+), 19 deletions(-) diff --git a/src/app/content.js b/src/app/content.js index 134404c..2e26099 100644 --- a/src/app/content.js +++ b/src/app/content.js @@ -6,6 +6,11 @@ const ContentTypes = { APPLICATION: 'application', ARCHIVE: 'archive', DISC: 'disc', + BAD: 'bad' +} + +const ContentCategories = { + XXX: 'xxx', } const ContentTypesColors = { @@ -326,4 +331,18 @@ const torrentTypeDetect = (torrent, files) => { return typesPriority; } -module.exports = {torrentTypeDetect, fileTypeDetect, niceTypeColor}; +module.exports = { + torrentTypeDetect, + fileTypeDetect, + niceTypeColor, + torrentTypeId: (type) => { + const id = Object.values(ContentTypes).indexOf(type); + return id == -1 ? 0 : id + 1; + }, + torrentIdToType: (id) => Object.values(ContentTypes)[id - 1], + torrentCategoryId: (type) => { + const id = Object.values(ContentCategories).indexOf(type); + return id == -1 ? 0 : id + 1; + }, + torrentIdToCategory: (id) => Object.values(ContentCategories)[id - 1], +}; diff --git a/src/background/api.js b/src/background/api.js index 59e4561..a77db4d 100644 --- a/src/background/api.js +++ b/src/background/api.js @@ -7,6 +7,7 @@ const asyncForEach = require('./asyncForEach') const cpuUsage = require('./bt/cpu-usage-global') const magnetParse = require('./magnetParse') const parseTorrentFiles = require('./parsetTorrentFiles') +const {torrentTypeId, torrentIdToType, torrentCategoryId, torrentIdToCategory} = require('../app/content'); module.exports = async ({ sphinx, @@ -308,11 +309,11 @@ module.exports = async ({ } if(safeSearch) { - where += " and contentCategory != 'xxx' "; + where += ` and contentCategory != ${torrentCategoryId('xxx')} `; } if(navigation.type && navigation.type.length > 0) { - where += ' and contentType = ' + sphinx.escape(navigation.type) + ' '; + where += ' and contentType = ' + torrentTypeId(navigation.type) + ' '; } if(navigation.size) { @@ -525,7 +526,7 @@ module.exports = async ({ if(type && type.length > 0) { - where += ' and contentType = ' + sphinx.escape(type) + ' '; + where += ' and contentType = ' + torrentTypeId(type) + ' '; } if(time) @@ -544,7 +545,7 @@ module.exports = async ({ } } - const query = `SELECT * FROM torrents WHERE seeders > 0 and contentCategory != 'xxx' ${where} ORDER BY seeders DESC LIMIT ${index},${limit}`; + const query = `SELECT * FROM torrents WHERE seeders > 0 and contentCategory != ${torrentCategoryId('xxx')} ${where} ORDER BY seeders DESC LIMIT ${index},${limit}`; if(topCache[query]) { callback(topCache[query]); diff --git a/src/background/dbPatcher.js b/src/background/dbPatcher.js index a562a73..9d98b0e 100644 --- a/src/background/dbPatcher.js +++ b/src/background/dbPatcher.js @@ -7,7 +7,7 @@ const fs = require('fs') const glob = require("glob") const asyncForEach = require('./asyncForEach') -const {torrentTypeDetect} = require('../app/content'); +const {torrentTypeDetect, torrentTypeId, torrentIdToType, torrentCategoryId, torrentIdToCategory} = require('../app/content'); const startSphinx = require('./sphinx') @@ -295,7 +295,7 @@ module.exports = async (callback, mainWindow, sphinxApp) => { delete torrent.contenttype torrent.filesList = (await sphinx.query(`SELECT * FROM files WHERE hash = '${torrent.hash}'`)) || [] torrentTypeDetect(torrent, torrent.filesList) - if(torrent.contentType == 'bad') + if(torrentIdToType(torrent.contentType) == 'bad') { logT('patcher', 'remove bad torrent', torrent.name) bad++ diff --git a/src/background/sphinx.js b/src/background/sphinx.js index f6ccd90..c964a4e 100644 --- a/src/background/sphinx.js +++ b/src/background/sphinx.js @@ -50,8 +50,8 @@ const writeSphinxConfig = async (rootPath, dbPath, params = {}) => { rt_attr_timestamp = added rt_field = ipv4 rt_attr_uint = port - rt_field = contentType - rt_field = contentCategory + rt_attr_uint = contentType + rt_attr_uint = contentCategory rt_attr_uint = seeders rt_attr_uint = leechers rt_attr_uint = completed @@ -60,7 +60,7 @@ const writeSphinxConfig = async (rootPath, dbPath, params = {}) => { rt_attr_uint = bad rt_attr_json = info - stored_only_fields = contentType, contentCategory, ipv4 + stored_only_fields = ipv4 ngram_len = 1 ngram_chars = U+3000..U+2FA1F diff --git a/src/background/spider.js b/src/background/spider.js index 73281a9..1f7ed08 100644 --- a/src/background/spider.js +++ b/src/background/spider.js @@ -28,7 +28,7 @@ const quotaDebug = _debug('main:quota'); const checkInternet = require('./checkInternet') -const {torrentTypeDetect} = require('../app/content'); +const {torrentTypeDetect, torrentTypeId, torrentIdToType, torrentCategoryId, torrentIdToCategory} = require('../app/content'); const torrentClient = require('./torrentClient') const directoryFilesRecursive = require('./directoryFilesRecursive') @@ -237,8 +237,8 @@ module.exports = function (send, recive, dataDirectory, version, env) filesList: row.filesList, piecelength: row.piecelength, added: row.added ? (typeof row.added === 'object' ? row.added.getTime() : row.added) : (new Date()).getTime(), - contentType: row.contentType || row.contenttype, - contentCategory: row.contentCategory || row.contentcategory, + contentType: torrentIdToType(row.contentType || row.contenttype), + contentCategory: torrentIdToCategory(row.contentCategory || row.contentcategory), seeders: row.seeders, completed: row.completed, leechers: row.leechers, @@ -469,13 +469,13 @@ module.exports = function (send, recive, dataDirectory, version, env) } } - if(torrent.contentType === 'bad') + if(torrentIdToType(torrent.contentType) === 'bad') { logT('check', 'ignore torrent', torrent.name, 'because this is a bad thing') return false } - if(config.filters.adultFilter && torrent.contentCategory === 'xxx') + if(config.filters.adultFilter && torrentIdToCategory(torrent.contentCategory) === 'xxx') { logT('check', 'ignore torrent', torrent.name, 'because adult filter') return false @@ -498,17 +498,20 @@ module.exports = function (send, recive, dataDirectory, version, env) const setupTorrentRecord = (torrent) => { // fix cases for low cases letters - if(torrent.contentcategory) + if(typeof torrent.contentcategory != 'undefined') { torrent.contentCategory = torrent.contentcategory; delete torrent.contentcategory; } - if(torrent.contenttype) + if(typeof torrent.contenttype != 'undefined') { torrent.contentType = torrent.contenttype; delete torrent.contenttype; } + torrent.contentType = torrentTypeId(torrent.contentType); + torrent.contentCategory = torrentCategoryId(torrent.contentCategory); + if(torrent.info && typeof torrent.info == 'string') { try { @@ -653,8 +656,8 @@ module.exports = function (send, recive, dataDirectory, version, env) size: torrent.size, files: torrent.files, piecelength: torrent.piecelength, - contentType: torrent.contentType, - contentCategory: torrent.contentCategory, + contentType: torrentIdToType(torrent.contentType), + contentCategory: torrentIdToCategory(torrent.contentCategory), info: torrent.info, }); updateTorrentTrackers(torrent.hash);