роутинг, исправления в занесении торрентов

This commit is contained in:
Alexey Kasyanchuk 2017-01-01 09:51:02 +03:00
parent 2b861167cd
commit 6fc2d2efb4
8 changed files with 218 additions and 30 deletions

View File

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

View File

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

View File

@ -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 (
<div className="App">
<RecentTorrents />
<Search />
</div>
);
}
componentDidMount() {
window.router()
}
render() {
return (
<div className="App">
<PagesPie />
</div>
);
}
}
export default App;

15
src/index-page.js Normal file
View File

@ -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 (
<div className="column">
<RecentTorrents />
<Search />
</div>
);
}
}

89
src/pages-pie.js Normal file
View File

@ -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 (
<div
className={'pie full-size ' + (this.props.className || '')}
>
{
this.pie.map(({Page, params}, index) => {
let focus = false;
if (index === this.pie.length-1) {
focus = true;
}
return (
<Page
focused={focus}
closeHandler={() => { index> 0 ? this.close() : null}}
index={index}
key={index}
ref={index}
{...params}
>
</Page>
)
})
}
</div>
)
} else {
return null
}
}
}

21
src/router.js Normal file
View File

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

View File

@ -3,19 +3,19 @@ import React, { Component } from 'react';
export default class SearchResults extends Component {
render() {
return (
<div className="column">
<div className="list column">
{
this.props.results && this.props.results.length > 0
?
this.props.results.map((torrent, index) => {
return (
<div key={index}>
{torrent.name}
</div>
);
})
:
null
this.props.results && this.props.results.length > 0
?
this.props.results.map((torrent, index) =>{
return(
<div key={index}>
{torrent.name}
</div>
);
})
:
null
}
</div>
);

26
src/torrent-page.js Normal file
View File

@ -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 (
<div className="column">
{
this.torrent
?
<div>
{this.torrent.name}
</div>
:
null
}
</div>
);
}
}