diff --git a/index.js b/index.js index 8c49374..b803468 100644 --- a/index.js +++ b/index.js @@ -33,7 +33,8 @@ io.on('connection', function(socket) name: row.name, size: row.size, files: row.files, - piecelength: row.piecelength + filesList: row.filesList, + piecelength: row.piecelength, } } @@ -49,15 +50,26 @@ io.on('connection', function(socket) }); }); - socket.on('torrent', function(hash, callback) + socket.on('torrent', function(hash, options, callback) { connection.query('SELECT * FROM `torrents` WHERE `hash` = ?', hash, function (error, rows, fields) { if(rows.length == 0) { callback(undefined); return; } + let torrent = rows[0]; - callback(baseRowData(rows[0])) + if(options.files) + { + connection.query('SELECT * FROM `files` WHERE `hash` = ?', hash, function (error, rows, fields) { + torrent.filesList = rows; + callback(baseRowData(torrent)) + }); + } + else + { + callback(baseRowData(torrent)) + } }); }); @@ -109,24 +121,42 @@ connection.connect(function(err) { filesCount = metadata.info.files.length; size = 0; - connection.query('DELETE FROM files WHERE hash = :hash', {hash: hash}, function (err, result) { + connection.query('DELETE FROM files WHERE hash = ?', hash, function (err, result) { }) for(let i = 0; i < metadata.info.files.length; i++) { let file = metadata.info.files[i]; + let filePath = file.path.join('/'); let fileQ = { hash: hash, - path: file.path, + path: filePath, size: file.length, }; let query = connection.query('INSERT INTO files SET ?', fileQ, function(err, result) { - // Neat! + if(!result) { + console.log(fileQ); + console.error(err); + } }); size += file.length; } } + else + { + let fileQ = { + hash: hash, + path: metadata.info.name, + size: size, + }; + let query = connection.query('INSERT INTO files SET ?', fileQ, function(err, result) { + if(!result) { + console.log(fileQ); + console.error(err); + } + }); + } var torrentQ = { hash: hash, @@ -137,7 +167,7 @@ connection.connect(function(err) { ipv4: rinfo.address, port: rinfo.port }; - var query = connection.query('INSERT INTO torrents SET ?', torrentQ, function(err, result) { + var query = connection.query('INSERT INTO torrents SET ? ON DUPLICATE KEY UPDATE hash=hash', torrentQ, function(err, result) { if(result) { io.sockets.emit('newTorrent', { hash: hash, @@ -147,6 +177,11 @@ connection.connect(function(err) { piecelength: metadata.info['piece length'] }); } + else + { + console.log(torrentQ); + console.error(err); + } }); }); diff --git a/package.json b/package.json index 9adb368..4fda971 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "bitfield": "^1.1.2", "express": "^4.14.0", "mysql": "^2.12.0", + "page": "^1.7.1", "react": "^15.4.1", "react-dom": "^15.4.1", "socket.io": "^1.7.2" diff --git a/src/app.js b/src/app.js index 8241ca2..477ad3c 100644 --- a/src/app.js +++ b/src/app.js @@ -1,21 +1,22 @@ import React, { Component } from 'react'; import './app.css'; +import './router'; +import PagesPie from './pages-pie.js'; 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 ( -
- - -
- ); - } + componentDidMount() { + window.router() + } + render() { + return ( +
+ +
+ ); + } } export default App; diff --git a/src/index-page.js b/src/index-page.js new file mode 100644 index 0000000..b6ca3a1 --- /dev/null +++ b/src/index-page.js @@ -0,0 +1,15 @@ +import React, { Component } from 'react'; + +import RecentTorrents from './recent-torrents' +import Search from './search' + +export default class IndexPage extends Component { + render() { + return ( +
+ + +
+ ); + } +} diff --git a/src/pages-pie.js b/src/pages-pie.js new file mode 100644 index 0000000..848217f --- /dev/null +++ b/src/pages-pie.js @@ -0,0 +1,89 @@ +import React, { Component } from 'react'; + +export default class PagesPie extends Component { + constructor(props) { + super(props); + // синглтон + if ( PagesPie.instance ) { + return PagesPie.instance; + } + PagesPie.instance = this; + this.pie = []; + } + open(pages, params) { + if (params && params.replace) { + if (params.replace === 'all') { + this.pie = []; + } else + if (params.replace === 'last') { + this.pie.pop(); + } + this.forceUpdate(); + delete params.replace; + } + setTimeout(() => { + if (Array.isArray(pages)) { + for (let i in pages) { + this.pie.push({ + Page: pages[i], + params: params + }); + } + } else { + this.pie.push({ + Page: pages, + params: params + }); + } + this.forceUpdate(); + }, 0); + } + close(count) { + if (count && typeof count === 'number') { + for (let i = 0; i < count; i++) { + this.pie.pop(); + } + } else { + this.pie.pop(); + } + this.forceUpdate(); + } + findOpened(windowType) { + for (let i in this.refs) { + if(this.refs[i] instanceof windowType) + return this.refs[i]; + } + } + // ОТРИСОВКА + render() { + if (this.pie.length > 0) { + return ( +
+ { + this.pie.map(({Page, params}, index) => { + let focus = false; + if (index === this.pie.length-1) { + focus = true; + } + return ( + { index> 0 ? this.close() : null}} + index={index} + key={index} + ref={index} + {...params} + > + + ) + }) + } +
+ ) + } else { + return null + } + } +} \ No newline at end of file diff --git a/src/router.js b/src/router.js new file mode 100644 index 0000000..d9de5ef --- /dev/null +++ b/src/router.js @@ -0,0 +1,21 @@ +import router from 'page'; +window.router = router; +import PagesPie from './pages-pie.js'; + +import IndexPage from './index-page.js' +import TorrentPage from './torrent-page.js' + +router('/', () => { + //singleton + let pie = new PagesPie; + pie.open(IndexPage, {replace: 'all'}); +}); + +router('/torrent/:hash', (e) => { + //singleton + let pie = new PagesPie; + pie.open(TorrentPage, { + replace: 'all', + hash: e.params.hash, + }); +}); diff --git a/src/search-results.js b/src/search-results.js index f698750..e94d8ca 100644 --- a/src/search-results.js +++ b/src/search-results.js @@ -3,19 +3,19 @@ import React, { Component } from 'react'; export default class SearchResults extends Component { render() { return ( -
+
{ - this.props.results && this.props.results.length > 0 - ? - this.props.results.map((torrent, index) => { - return ( -
- {torrent.name} -
- ); - }) - : - null + this.props.results && this.props.results.length > 0 + ? + this.props.results.map((torrent, index) =>{ + return( +
+ {torrent.name} +
+ ); + }) + : + null }
); diff --git a/src/torrent-page.js b/src/torrent-page.js new file mode 100644 index 0000000..d69637d --- /dev/null +++ b/src/torrent-page.js @@ -0,0 +1,26 @@ +import React, { Component } from 'react'; + +export default class TorrentPage extends Component { + componentDidMount() { + window.torrentSocket.emit('torrent', this.props.hash, {files: true}, (data) => { + console.log(data); + this.torrent = data + this.forceUpdate(); + }); + } + render() { + return ( +
+ { + this.torrent + ? +
+ {this.torrent.name} +
+ : + null + } +
+ ); + } +}