diff --git a/package.json b/package.json
index 7fe3a75..53cee24 100644
--- a/package.json
+++ b/package.json
@@ -84,7 +84,8 @@
"react-dom": "^16.2.0",
"react-input-range": "^1.3.0",
"react-markdown": "^3.1.5",
- "react-tap-event-plugin": "^3.0.2"
+ "react-tap-event-plugin": "^3.0.2",
+ "webtorrent": "^0.98.21"
},
"devDependencies": {
"@babel/core": "^7.0.0-beta.38",
diff --git a/src/app/torrent-page.js b/src/app/torrent-page.js
index 04e1505..d91a87b 100644
--- a/src/app/torrent-page.js
+++ b/src/app/torrent-page.js
@@ -146,6 +146,9 @@ export default class TorrentPage extends Page {
searchingIndicator: false,
voting: false,
voted: false,
+ askDownloading: false,
+ downloading: false,
+ downloadProgress: {}
};
this.setTitle('Information about torrent');
}
@@ -236,6 +239,31 @@ export default class TorrentPage extends Page {
this.forceUpdate();
}
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() {
if(this.filesUpdated)
@@ -247,6 +275,12 @@ export default class TorrentPage extends Page {
if(this.torrent && this.torrent.contentCategory == 'xxx') {
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) {
if(!this.torrent)
@@ -320,6 +354,29 @@ export default class TorrentPage extends Page {
}}
icon={}
/>
+ {
+ !this.state.askDownloading
+ ?
+ {
+ e.preventDefault();
+ this.setState({askDownloading: true})
+ window.torrentSocket.emit('download', `magnet:?xt=urn:btih:${this.torrent.hash}`)
+ }}
+ icon={}
+ />
+ :
+ this.state.downloading
+ &&
+
+ }
{
this.torrent.seeders || this.torrent.leechers || this.torrent.completed
diff --git a/src/background/config.js b/src/background/config.js
index 62881a6..8884d70 100644
--- a/src/background/config.js
+++ b/src/background/config.js
@@ -39,6 +39,10 @@ let config = {
trafficIgnoreDHT: false,
dbPath: '',
+
+ client: {
+ downloadPath: ''
+ }
}
const fs = require('fs');
diff --git a/src/background/spider.js b/src/background/spider.js
index 7d37ba3..6652f9b 100644
--- a/src/background/spider.js
+++ b/src/background/spider.js
@@ -23,6 +23,8 @@ const quotaDebug = _debug('main:quota');
const {torrentTypeDetect} = require('../app/content');
+const torrentClient = require('./torrentClient')
+
// Start server
//server.listen(config.httpPort);
//console.log('Listening web server on', config.httpPort, 'port')
@@ -569,6 +571,25 @@ setInterval(() => {
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 ip = socket.request.connection.remoteAddress;
if (ipaddr.IPv4.isValid(ip)) {
@@ -967,10 +988,12 @@ if(config.spaceQuota)
this.stop = (callback) => {
console.log('closing spider')
- sphinx.end(() => spider.close(() => {
- mysqlSingle.destroy()
- callback()
- }))
+ torrentClient.destroy(() => {
+ sphinx.end(() => spider.close(() => {
+ mysqlSingle.destroy()
+ callback()
+ }))
+ })
}
return this
diff --git a/src/background/torrentClient.js b/src/background/torrentClient.js
new file mode 100644
index 0000000..9c087b2
--- /dev/null
+++ b/src/background/torrentClient.js
@@ -0,0 +1,3 @@
+const WebTorrent = require('webtorrent')
+let torrentClient = new WebTorrent({webSeeds: false})
+module.exports = torrentClient
\ No newline at end of file