Skip to content

Commit

Permalink
feat(search): hash/magnet search support in db
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Jul 26, 2018
1 parent 4109ef9 commit 1e57789
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/background/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,22 @@ module.exports = async ({
}
}

const isSH1Hash = (hash) =>
{
if(hash.length !== 40)
return false
return /[0-9a-f]+/i.test(hash)
}

const magnetParse = (magnet) => {
const match = /magnet:\?xt=urn:btih:([0-9a-f]+)/i.exec(magnet)
if(!match)
return
if(match[1].length === 40)
return match[1].toLowerCase()
return
}

const searchTorrentCall = function(text, navigation, callback)
{
if(typeof callback != 'function')
Expand All @@ -278,6 +294,9 @@ module.exports = async ({
return;
}

// check magnet
text = magnetParse(text) || text

const safeSearch = !!navigation.safeSearch;

const index = navigation.index || 0;
Expand Down Expand Up @@ -316,9 +335,7 @@ module.exports = async ({
}

let searchList = [];
//args.splice(orderBy && orderBy.length > 0 ? 1 : 0, 1);
//sphinx.query('SELECT * FROM `torrents` WHERE `name` like \'%' + text + '%\' ' + where + ' ' + order + ' LIMIT ?,?', args, function (error, rows, fields) {
sphinx.query('SELECT * FROM `torrents` WHERE MATCH(?) ' + where + ' ' + order + ' LIMIT ?,?', args, function (error, rows, fields) {
sphinx.query('SELECT * FROM `torrents` WHERE ' + (isSH1Hash(text) ? 'hash = ?' : 'MATCH(?)') + ' ' + where + ' ' + order + ' LIMIT ?,?', args, function (error, rows, fields) {
if(!rows) {
console.log(error)
callback(undefined)
Expand Down

0 comments on commit 1e57789

Please sign in to comment.