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', APPLICATION: 'application',
ARCHIVE: 'archive', ARCHIVE: 'archive',
DISC: 'disc', DISC: 'disc',
BAD: 'bad'
}
const ContentCategories = {
XXX: 'xxx',
} }
const ContentTypesColors = { const ContentTypesColors = {
@ -326,4 +331,18 @@ const torrentTypeDetect = (torrent, files) => {
return typesPriority; 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 cpuUsage = require('./bt/cpu-usage-global')
const magnetParse = require('./magnetParse') const magnetParse = require('./magnetParse')
const parseTorrentFiles = require('./parsetTorrentFiles') const parseTorrentFiles = require('./parsetTorrentFiles')
const {torrentTypeId, torrentIdToType, torrentCategoryId, torrentIdToCategory} = require('../app/content');
module.exports = async ({ module.exports = async ({
sphinx, sphinx,
@ -308,11 +309,11 @@ module.exports = async ({
} }
if(safeSearch) if(safeSearch)
{ {
where += " and contentCategory != 'xxx' "; where += ` and contentCategory != ${torrentCategoryId('xxx')} `;
} }
if(navigation.type && navigation.type.length > 0) if(navigation.type && navigation.type.length > 0)
{ {
where += ' and contentType = ' + sphinx.escape(navigation.type) + ' '; where += ' and contentType = ' + torrentTypeId(navigation.type) + ' ';
} }
if(navigation.size) if(navigation.size)
{ {
@ -525,7 +526,7 @@ module.exports = async ({
if(type && type.length > 0) if(type && type.length > 0)
{ {
where += ' and contentType = ' + sphinx.escape(type) + ' '; where += ' and contentType = ' + torrentTypeId(type) + ' ';
} }
if(time) 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]) if(topCache[query])
{ {
callback(topCache[query]); callback(topCache[query]);

View File

@ -7,7 +7,7 @@ const fs = require('fs')
const glob = require("glob") const glob = require("glob")
const asyncForEach = require('./asyncForEach') const asyncForEach = require('./asyncForEach')
const {torrentTypeDetect} = require('../app/content'); const {torrentTypeDetect, torrentTypeId, torrentIdToType, torrentCategoryId, torrentIdToCategory} = require('../app/content');
const startSphinx = require('./sphinx') const startSphinx = require('./sphinx')
@ -295,7 +295,7 @@ module.exports = async (callback, mainWindow, sphinxApp) => {
delete torrent.contenttype delete torrent.contenttype
torrent.filesList = (await sphinx.query(`SELECT * FROM files WHERE hash = '${torrent.hash}'`)) || [] torrent.filesList = (await sphinx.query(`SELECT * FROM files WHERE hash = '${torrent.hash}'`)) || []
torrentTypeDetect(torrent, torrent.filesList) torrentTypeDetect(torrent, torrent.filesList)
if(torrent.contentType == 'bad') if(torrentIdToType(torrent.contentType) == 'bad')
{ {
logT('patcher', 'remove bad torrent', torrent.name) logT('patcher', 'remove bad torrent', torrent.name)
bad++ bad++

View File

@ -50,8 +50,8 @@ const writeSphinxConfig = async (rootPath, dbPath, params = {}) => {
rt_attr_timestamp = added rt_attr_timestamp = added
rt_field = ipv4 rt_field = ipv4
rt_attr_uint = port rt_attr_uint = port
rt_field = contentType rt_attr_uint = contentType
rt_field = contentCategory rt_attr_uint = contentCategory
rt_attr_uint = seeders rt_attr_uint = seeders
rt_attr_uint = leechers rt_attr_uint = leechers
rt_attr_uint = completed rt_attr_uint = completed
@ -60,7 +60,7 @@ const writeSphinxConfig = async (rootPath, dbPath, params = {}) => {
rt_attr_uint = bad rt_attr_uint = bad
rt_attr_json = info rt_attr_json = info
stored_only_fields = contentType, contentCategory, ipv4 stored_only_fields = ipv4
ngram_len = 1 ngram_len = 1
ngram_chars = U+3000..U+2FA1F ngram_chars = U+3000..U+2FA1F

View File

@ -28,7 +28,7 @@ const quotaDebug = _debug('main:quota');
const checkInternet = require('./checkInternet') const checkInternet = require('./checkInternet')
const {torrentTypeDetect} = require('../app/content'); const {torrentTypeDetect, torrentTypeId, torrentIdToType, torrentCategoryId, torrentIdToCategory} = require('../app/content');
const torrentClient = require('./torrentClient') const torrentClient = require('./torrentClient')
const directoryFilesRecursive = require('./directoryFilesRecursive') const directoryFilesRecursive = require('./directoryFilesRecursive')
@ -237,8 +237,8 @@ module.exports = function (send, recive, dataDirectory, version, env)
filesList: row.filesList, filesList: row.filesList,
piecelength: row.piecelength, piecelength: row.piecelength,
added: row.added ? (typeof row.added === 'object' ? row.added.getTime() : row.added) : (new Date()).getTime(), added: row.added ? (typeof row.added === 'object' ? row.added.getTime() : row.added) : (new Date()).getTime(),
contentType: row.contentType || row.contenttype, contentType: torrentIdToType(row.contentType || row.contenttype),
contentCategory: row.contentCategory || row.contentcategory, contentCategory: torrentIdToCategory(row.contentCategory || row.contentcategory),
seeders: row.seeders, seeders: row.seeders,
completed: row.completed, completed: row.completed,
leechers: row.leechers, 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') logT('check', 'ignore torrent', torrent.name, 'because this is a bad thing')
return false 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') logT('check', 'ignore torrent', torrent.name, 'because adult filter')
return false return false
@ -498,17 +498,20 @@ module.exports = function (send, recive, dataDirectory, version, env)
const setupTorrentRecord = (torrent) => { const setupTorrentRecord = (torrent) => {
// fix cases for low cases letters // fix cases for low cases letters
if(torrent.contentcategory) if(typeof torrent.contentcategory != 'undefined')
{ {
torrent.contentCategory = torrent.contentcategory; torrent.contentCategory = torrent.contentcategory;
delete torrent.contentcategory; delete torrent.contentcategory;
} }
if(torrent.contenttype) if(typeof torrent.contenttype != 'undefined')
{ {
torrent.contentType = torrent.contenttype; torrent.contentType = torrent.contenttype;
delete torrent.contenttype; delete torrent.contenttype;
} }
torrent.contentType = torrentTypeId(torrent.contentType);
torrent.contentCategory = torrentCategoryId(torrent.contentCategory);
if(torrent.info && typeof torrent.info == 'string') if(torrent.info && typeof torrent.info == 'string')
{ {
try { try {
@ -653,8 +656,8 @@ module.exports = function (send, recive, dataDirectory, version, env)
size: torrent.size, size: torrent.size,
files: torrent.files, files: torrent.files,
piecelength: torrent.piecelength, piecelength: torrent.piecelength,
contentType: torrent.contentType, contentType: torrentIdToType(torrent.contentType),
contentCategory: torrent.contentCategory, contentCategory: torrentIdToCategory(torrent.contentCategory),
info: torrent.info, info: torrent.info,
}); });
updateTorrentTrackers(torrent.hash); updateTorrentTrackers(torrent.hash);