feat(download): basic torrent download support

This commit is contained in:
Alexey Kasyanchuk 2018-02-08 03:44:57 +03:00
parent 0e46df3dbc
commit 765177d89d
5 changed files with 93 additions and 5 deletions

View File

@ -84,7 +84,8 @@
"react-dom": "^16.2.0", "react-dom": "^16.2.0",
"react-input-range": "^1.3.0", "react-input-range": "^1.3.0",
"react-markdown": "^3.1.5", "react-markdown": "^3.1.5",
"react-tap-event-plugin": "^3.0.2" "react-tap-event-plugin": "^3.0.2",
"webtorrent": "^0.98.21"
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.0.0-beta.38", "@babel/core": "^7.0.0-beta.38",

View File

@ -146,6 +146,9 @@ export default class TorrentPage extends Page {
searchingIndicator: false, searchingIndicator: false,
voting: false, voting: false,
voted: false, voted: false,
askDownloading: false,
downloading: false,
downloadProgress: {}
}; };
this.setTitle('Information about torrent'); this.setTitle('Information about torrent');
} }
@ -236,6 +239,31 @@ export default class TorrentPage extends Page {
this.forceUpdate(); this.forceUpdate();
} }
window.torrentSocket.on('vote', this.onVote); window.torrentSocket.on('vote', this.onVote);
this.downloadMetadata = (hash) => {
if(this.props.hash != hash)
return;
this.setState({downloading: true})
}
window.torrentSocket.on('downloadMetadata', this.downloadMetadata);
this.downloadDone = (hash) => {
if(this.props.hash != hash)
return;
this.setState({downloading: false})
}
window.torrentSocket.on('downloadDone', this.downloadDone);
this.downloadProgress = (hash, progress) => {
if(this.props.hash != hash)
return;
console.log(progress)
this.setState({downloadProgress: progress})
}
window.torrentSocket.on('downloadProgress', this.downloadProgress);
} }
componentWillUnmount() { componentWillUnmount() {
if(this.filesUpdated) if(this.filesUpdated)
@ -247,6 +275,12 @@ export default class TorrentPage extends Page {
if(this.torrent && this.torrent.contentCategory == 'xxx') { if(this.torrent && this.torrent.contentCategory == 'xxx') {
this.removeMetaTag('robots'); this.removeMetaTag('robots');
} }
if(this.downloadMetadata)
window.torrentSocket.off('downloadMetadata', this.downloadMetadata);
if(this.downloadDone)
window.torrentSocket.off('downloadDone', this.downloadDone);
if(this.downloadProgress)
window.torrentSocket.off('downloadProgress', this.downloadProgress);
} }
vote(good) { vote(good) {
if(!this.torrent) if(!this.torrent)
@ -320,6 +354,29 @@ export default class TorrentPage extends Page {
}} }}
icon={<svg fill='white' viewBox="0 0 24 24"><path d="M17.374 20.235c2.444-2.981 6.626-8.157 6.626-8.157l-3.846-3.092s-2.857 3.523-6.571 8.097c-4.312 5.312-11.881-2.41-6.671-6.671 4.561-3.729 8.097-6.57 8.097-6.57l-3.092-3.842s-5.173 4.181-8.157 6.621c-2.662 2.175-3.76 4.749-3.76 7.24 0 5.254 4.867 10.139 10.121 10.139 2.487 0 5.064-1.095 7.253-3.765zm4.724-7.953l-1.699 2.111-1.74-1.397 1.701-2.114 1.738 1.4zm-10.386-10.385l1.4 1.738-2.113 1.701-1.397-1.74 2.11-1.699z"/></svg>} icon={<svg fill='white' viewBox="0 0 24 24"><path d="M17.374 20.235c2.444-2.981 6.626-8.157 6.626-8.157l-3.846-3.092s-2.857 3.523-6.571 8.097c-4.312 5.312-11.881-2.41-6.671-6.671 4.561-3.729 8.097-6.57 8.097-6.57l-3.092-3.842s-5.173 4.181-8.157 6.621c-2.662 2.175-3.76 4.749-3.76 7.24 0 5.254 4.867 10.139 10.121 10.139 2.487 0 5.064-1.095 7.253-3.765zm4.724-7.953l-1.699 2.111-1.74-1.397 1.701-2.114 1.738 1.4zm-10.386-10.385l1.4 1.738-2.113 1.701-1.397-1.74 2.11-1.699z"/></svg>}
/> />
{
!this.state.askDownloading
?
<RaisedButton
href={`magnet:?xt=urn:btih:${this.torrent.hash}`}
target="_self"
label="Download Iternal client"
secondary={true}
onClick={(e) => {
e.preventDefault();
this.setState({askDownloading: true})
window.torrentSocket.emit('download', `magnet:?xt=urn:btih:${this.torrent.hash}`)
}}
icon={<svg fill='white' viewBox="0 0 24 24"><path d="M17.374 20.235c2.444-2.981 6.626-8.157 6.626-8.157l-3.846-3.092s-2.857 3.523-6.571 8.097c-4.312 5.312-11.881-2.41-6.671-6.671 4.561-3.729 8.097-6.57 8.097-6.57l-3.092-3.842s-5.173 4.181-8.157 6.621c-2.662 2.175-3.76 4.749-3.76 7.24 0 5.254 4.867 10.139 10.121 10.139 2.487 0 5.064-1.095 7.253-3.765zm4.724-7.953l-1.699 2.111-1.74-1.397 1.701-2.114 1.738 1.4zm-10.386-10.385l1.4 1.738-2.113 1.701-1.397-1.74 2.11-1.699z"/></svg>}
/>
:
this.state.downloading
&&
<LinearProgress
mode="determinate"
value={this.state.downloadProgress && this.state.downloadProgress.progress * 100}
/>
}
<div className='fs0-75 pad0-75 center column' style={{color: 'rgba(0, 0, 0, 0.541176)'}}><div>BTIH:</div><div>{this.torrent.hash}</div></div> <div className='fs0-75 pad0-75 center column' style={{color: 'rgba(0, 0, 0, 0.541176)'}}><div>BTIH:</div><div>{this.torrent.hash}</div></div>
{ {
this.torrent.seeders || this.torrent.leechers || this.torrent.completed this.torrent.seeders || this.torrent.leechers || this.torrent.completed

View File

@ -39,6 +39,10 @@ let config = {
trafficIgnoreDHT: false, trafficIgnoreDHT: false,
dbPath: '', dbPath: '',
client: {
downloadPath: ''
}
} }
const fs = require('fs'); const fs = require('fs');

View File

@ -23,6 +23,8 @@ const quotaDebug = _debug('main:quota');
const {torrentTypeDetect} = require('../app/content'); const {torrentTypeDetect} = require('../app/content');
const torrentClient = require('./torrentClient')
// Start server // Start server
//server.listen(config.httpPort); //server.listen(config.httpPort);
//console.log('Listening web server on', config.httpPort, 'port') //console.log('Listening web server on', config.httpPort, 'port')
@ -569,6 +571,25 @@ setInterval(() => {
callback(true) callback(true)
}); });
recive('download', (magnet) =>
{
console.log('download', magnet)
torrentClient.add(magnet, {path: config.client.downloadPath}, (torrent) =>{
send('downloadMetadata', torrent.infoHash)
torrent.on('done', () => send('downloadDone', torrent.infoHash))
torrent.on('download', (bytes) => {
send('downloadProgress', torrent.infoHash, {
bytes,
downloaded: torrent.downloaded,
speed: torrent.downloadSpeed,
progress: torrent.progress
})
})
})
});
let socketIPV4 = () => { let socketIPV4 = () => {
let ip = socket.request.connection.remoteAddress; let ip = socket.request.connection.remoteAddress;
if (ipaddr.IPv4.isValid(ip)) { if (ipaddr.IPv4.isValid(ip)) {
@ -967,10 +988,12 @@ if(config.spaceQuota)
this.stop = (callback) => { this.stop = (callback) => {
console.log('closing spider') console.log('closing spider')
torrentClient.destroy(() => {
sphinx.end(() => spider.close(() => { sphinx.end(() => spider.close(() => {
mysqlSingle.destroy() mysqlSingle.destroy()
callback() callback()
})) }))
})
} }
return this return this

View File

@ -0,0 +1,3 @@
const WebTorrent = require('webtorrent')
let torrentClient = new WebTorrent({webSeeds: false})
module.exports = torrentClient