From 1b6a15bcddc7556756aef8a24386c3604153b465 Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Sun, 1 Jan 2017 06:25:04 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B1=D0=B0=D0=B7=D0=BE=D0=B2=D1=8B=D0=B9=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=B8=D1=81=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 58 ++++++++++++++++++++++++++++++++++------------- public/index.html | 1 - src/app.js | 2 ++ src/search.js | 20 ++++++++++++++++ 4 files changed, 64 insertions(+), 17 deletions(-) create mode 100644 src/search.js diff --git a/index.js b/index.js index a84aada..8c49374 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,7 @@ server.listen(8099); var connection = mysql.createConnection({ host : 'localhost', user : 'root', - password : '', + password : 'degitisi', database : 'btsearch' }); @@ -26,18 +26,23 @@ app.use(express.static('build')); io.on('connection', function(socket) { + function baseRowData(row) + { + return { + hash: row.hash, + name: row.name, + size: row.size, + files: row.files, + piecelength: row.piecelength + } + } + socket.on('recentTorrents', function(callback) { connection.query('SELECT * FROM `torrents` ORDER BY added DESC LIMIT 0,10', function (error, rows, fields) { let torrents = []; rows.forEach((row) => { - torrents.push({ - hash: row.hash, - name: row.name, - size: row.size, - files: row.files, - piecelength: row.piecelength, - }); + torrents.push(baseRowData(row)); }); callback(torrents) @@ -52,13 +57,34 @@ io.on('connection', function(socket) return; } - callback({ - hash: rows[0].hash, - name: rows[0].name, - size: rows[0].size, - files: rows[0].files, - piecelength: rows[0].piecelength, - }) + callback(baseRowData(rows[0])) + }); + }); + + socket.on('search', function(text, callback) + { + let search = {}; + + console.log(text); + let q = 2; + connection.query('SELECT * FROM `torrents` WHERE MATCH(`name`) AGAINST(?)', text, function (error, rows, fields) { + rows.forEach((row) => { + search[row.hash] = baseRowData(row); + }); + if(--q == 0) + callback(Object.keys(search).map(function(key) { + return search[key]; + })); + }); + connection.query('SELECT * FROM `files` INNER JOIN torrents ON(torrents.hash = files.hash) WHERE MATCH(`path`) AGAINST(?)', text, function (error, rows, fields) { + rows.forEach((row) => { + search[row.hash] = baseRowData(row); + search[row.hash].path = row.path; + }); + if(--q == 0) + callback(Object.keys(search).map(function(key) { + return search[key]; + })); }); }); }); @@ -126,5 +152,5 @@ connection.connect(function(err) { // spider.on('nodes', (nodes)=>console.log('foundNodes')) - spider.listen(4445) + //spider.listen(4445) }); \ No newline at end of file diff --git a/public/index.html b/public/index.html index ae52128..ef921fe 100644 --- a/public/index.html +++ b/public/index.html @@ -5,7 +5,6 @@ BT Search -
diff --git a/src/app.js b/src/app.js index b88cef7..8241ca2 100644 --- a/src/app.js +++ b/src/app.js @@ -5,12 +5,14 @@ var io = require("socket.io-client"); window.torrentSocket = io('http://localhost:8099/'); import RecentTorrents from './recent-torrents' +import Search from './search' class App extends Component { render() { return (
+
); } diff --git a/src/search.js b/src/search.js new file mode 100644 index 0000000..b6e099b --- /dev/null +++ b/src/search.js @@ -0,0 +1,20 @@ +import React, { Component } from 'react'; + +export default class Search extends Component { + componentDidMount() { + + } + render() { + return ( +
+ { + if (e.key === 'Enter') { + window.torrentSocket.emit('search', e.target.value, (torrents) => { + console.log(torrents); + }); + } + }} /> +
+ ); + } +}