роутинг, исправления в занесении торрентов
This commit is contained in:
parent
2b861167cd
commit
6fc2d2efb4
49
index.js
49
index.js
@ -33,7 +33,8 @@ io.on('connection', function(socket)
|
|||||||
name: row.name,
|
name: row.name,
|
||||||
size: row.size,
|
size: row.size,
|
||||||
files: row.files,
|
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) {
|
connection.query('SELECT * FROM `torrents` WHERE `hash` = ?', hash, function (error, rows, fields) {
|
||||||
if(rows.length == 0) {
|
if(rows.length == 0) {
|
||||||
callback(undefined);
|
callback(undefined);
|
||||||
return;
|
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;
|
filesCount = metadata.info.files.length;
|
||||||
size = 0;
|
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++)
|
for(let i = 0; i < metadata.info.files.length; i++)
|
||||||
{
|
{
|
||||||
let file = metadata.info.files[i];
|
let file = metadata.info.files[i];
|
||||||
|
let filePath = file.path.join('/');
|
||||||
let fileQ = {
|
let fileQ = {
|
||||||
hash: hash,
|
hash: hash,
|
||||||
path: file.path,
|
path: filePath,
|
||||||
size: file.length,
|
size: file.length,
|
||||||
};
|
};
|
||||||
let query = connection.query('INSERT INTO files SET ?', fileQ, function(err, result) {
|
let query = connection.query('INSERT INTO files SET ?', fileQ, function(err, result) {
|
||||||
// Neat!
|
if(!result) {
|
||||||
|
console.log(fileQ);
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
size += file.length;
|
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 = {
|
var torrentQ = {
|
||||||
hash: hash,
|
hash: hash,
|
||||||
@ -137,7 +167,7 @@ connection.connect(function(err) {
|
|||||||
ipv4: rinfo.address,
|
ipv4: rinfo.address,
|
||||||
port: rinfo.port
|
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) {
|
if(result) {
|
||||||
io.sockets.emit('newTorrent', {
|
io.sockets.emit('newTorrent', {
|
||||||
hash: hash,
|
hash: hash,
|
||||||
@ -147,6 +177,11 @@ connection.connect(function(err) {
|
|||||||
piecelength: metadata.info['piece length']
|
piecelength: metadata.info['piece length']
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
console.log(torrentQ);
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -51,6 +51,7 @@
|
|||||||
"bitfield": "^1.1.2",
|
"bitfield": "^1.1.2",
|
||||||
"express": "^4.14.0",
|
"express": "^4.14.0",
|
||||||
"mysql": "^2.12.0",
|
"mysql": "^2.12.0",
|
||||||
|
"page": "^1.7.1",
|
||||||
"react": "^15.4.1",
|
"react": "^15.4.1",
|
||||||
"react-dom": "^15.4.1",
|
"react-dom": "^15.4.1",
|
||||||
"socket.io": "^1.7.2"
|
"socket.io": "^1.7.2"
|
||||||
|
11
src/app.js
11
src/app.js
@ -1,18 +1,19 @@
|
|||||||
import React, { Component } from 'react';
|
import React, { Component } from 'react';
|
||||||
import './app.css';
|
import './app.css';
|
||||||
|
import './router';
|
||||||
|
import PagesPie from './pages-pie.js';
|
||||||
|
|
||||||
var io = require("socket.io-client");
|
var io = require("socket.io-client");
|
||||||
window.torrentSocket = io('http://localhost:8099/');
|
window.torrentSocket = io('http://localhost:8099/');
|
||||||
|
|
||||||
import RecentTorrents from './recent-torrents'
|
|
||||||
import Search from './search'
|
|
||||||
|
|
||||||
class App extends Component {
|
class App extends Component {
|
||||||
|
componentDidMount() {
|
||||||
|
window.router()
|
||||||
|
}
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div className="App">
|
<div className="App">
|
||||||
<RecentTorrents />
|
<PagesPie />
|
||||||
<Search />
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
15
src/index-page.js
Normal file
15
src/index-page.js
Normal 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
89
src/pages-pie.js
Normal 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
21
src/router.js
Normal 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,
|
||||||
|
});
|
||||||
|
});
|
@ -3,7 +3,7 @@ import React, { Component } from 'react';
|
|||||||
export default class SearchResults extends Component {
|
export default class SearchResults extends Component {
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div className="column">
|
<div className="list column">
|
||||||
{
|
{
|
||||||
this.props.results && this.props.results.length > 0
|
this.props.results && this.props.results.length > 0
|
||||||
?
|
?
|
||||||
|
26
src/torrent-page.js
Normal file
26
src/torrent-page.js
Normal 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>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user