fix(db): moving content type to uint values

This commit is contained in:
Alexey Kasyanchuk 2021-09-03 12:10:29 +03:00
parent d65f214d4d
commit f7166103df
5 changed files with 42 additions and 19 deletions

View File

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

View File

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

View File

@ -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++

View File

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

View File

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