From 6b288741d19803cb8e99eec16bb993b4b88ace83 Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Thu, 8 Feb 2018 13:41:03 +0300 Subject: [PATCH] feat(download): more control over torrent download --- src/app/torrent-page.js | 60 +++++++++++++++++++++++++++++----------- src/background/spider.js | 45 ++++++++++++++++++++++++++++-- 2 files changed, 87 insertions(+), 18 deletions(-) diff --git a/src/app/torrent-page.js b/src/app/torrent-page.js index d91a87b..92c190f 100644 --- a/src/app/torrent-page.js +++ b/src/app/torrent-page.js @@ -182,7 +182,7 @@ export default class TorrentPage extends Page { window.torrentSocket.emit('torrent', this.props.hash, {files: true}, window.customLoader((data) => { if(data) { this.torrent = data - this.setTitle(this.torrent.name + ' - RatsOnTheBoat.org'); + this.setTitle(this.torrent.name + ' - Rats On TheBoat'); if(this.torrent.contentCategory == 'xxx') { this.setMetaTag('robots', 'noindex'); } @@ -206,7 +206,6 @@ export default class TorrentPage extends Page { componentDidMount() { super.componentDidMount(); - this.getTorrentInfo(); this.filesUpdated = (hash) => { if(this.props.hash != hash) return; @@ -240,13 +239,13 @@ export default class TorrentPage extends Page { } window.torrentSocket.on('vote', this.onVote); - this.downloadMetadata = (hash) => { + this.downloading = (hash) => { if(this.props.hash != hash) return; this.setState({downloading: true}) } - window.torrentSocket.on('downloadMetadata', this.downloadMetadata); + window.torrentSocket.on('downloading', this.downloading); this.downloadDone = (hash) => { if(this.props.hash != hash) @@ -264,6 +263,8 @@ export default class TorrentPage extends Page { this.setState({downloadProgress: progress}) } window.torrentSocket.on('downloadProgress', this.downloadProgress); + + this.getTorrentInfo(); } componentWillUnmount() { if(this.filesUpdated) @@ -275,8 +276,8 @@ 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.downloading) + window.torrentSocket.off('downloading', this.downloading); if(this.downloadDone) window.torrentSocket.off('downloadDone', this.downloadDone); if(this.downloadProgress) @@ -346,7 +347,7 @@ export default class TorrentPage extends Page { { e.preventDefault(); @@ -355,28 +356,55 @@ export default class TorrentPage extends Page { icon={} /> { - !this.state.askDownloading - ? + !this.state.askDownloading && !this.state.downloading + && { e.preventDefault(); this.setState({askDownloading: true}) window.torrentSocket.emit('download', `magnet:?xt=urn:btih:${this.torrent.hash}`) }} - icon={} + icon={ + + + + + + + } /> - : - this.state.downloading - && + } + { + this.state.downloading + && +
+
downloading {this.state.downloadProgress && (this.state.downloadProgress.progress * 100).toFixed(1)}%
- } +
+ }
BTIH:
{this.torrent.hash}
{ this.torrent.seeders || this.torrent.leechers || this.torrent.completed diff --git a/src/background/spider.js b/src/background/spider.js index 6652f9b..55c7752 100644 --- a/src/background/spider.js +++ b/src/background/spider.js @@ -24,6 +24,7 @@ const quotaDebug = _debug('main:quota'); const {torrentTypeDetect} = require('../app/content'); const torrentClient = require('./torrentClient') +let torrentClientHashMap = {} // Start server //server.listen(config.httpPort); @@ -326,6 +327,15 @@ setInterval(() => { { callback(baseRowData(torrent)) } + + if(torrentClientHashMap[hash]) + { + const torrent = torrentClient.get(torrentClientHashMap[hash]) + if(!torrent) + return + + send('downloading', torrent.infoHash) + } }); }); @@ -575,11 +585,20 @@ setInterval(() => { { console.log('download', magnet) torrentClient.add(magnet, {path: config.client.downloadPath}, (torrent) =>{ - send('downloadMetadata', torrent.infoHash) + torrentClientHashMap[torrent.infoHash] = magnet + send('downloading', torrent.infoHash) - torrent.on('done', () => send('downloadDone', torrent.infoHash)) + torrent.on('done', () => { + delete torrentClientHashMap[torrent.infoHash] + send('downloadDone', torrent.infoHash) + }) + let now = Date.now() torrent.on('download', (bytes) => { + if(Date.now() - now < 100) + return + now = Date.now() + send('downloadProgress', torrent.infoHash, { bytes, downloaded: torrent.downloaded, @@ -590,6 +609,28 @@ setInterval(() => { }) }); + recive('downloadCancel', (hash, callback) => + { + const id = torrentClientHashMap[hash] + if(!id) + { + callback(false) + return + } + + client.remove(id, (err) => { + if(err) + { + callback(false) + return + } + + delete torrentClientHashMap[hash] + if(callback) + callback(true) + }) + }) + let socketIPV4 = () => { let ip = socket.request.connection.remoteAddress; if (ipaddr.IPv4.isValid(ip)) {