From 603798cbce2c1b5fb62a2b500d60b8ed9cc076c7 Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Thu, 8 Feb 2018 15:12:37 +0300 Subject: [PATCH] feat(download): torrent progress on torrent element --- src/app/torrent-page.js | 1 - src/app/torrent.js | 282 ++++++++++++++++++++++++++------------- src/background/spider.js | 4 + 3 files changed, 195 insertions(+), 92 deletions(-) diff --git a/src/app/torrent-page.js b/src/app/torrent-page.js index 92c190f..23f48d4 100644 --- a/src/app/torrent-page.js +++ b/src/app/torrent-page.js @@ -259,7 +259,6 @@ export default class TorrentPage extends Page { if(this.props.hash != hash) return; - console.log(progress) this.setState({downloadProgress: progress}) } window.torrentSocket.on('downloadProgress', this.downloadProgress); diff --git a/src/app/torrent.js b/src/app/torrent.js index cf6f217..c319113 100644 --- a/src/app/torrent.js +++ b/src/app/torrent.js @@ -3,6 +3,9 @@ import formatBytes from './format-bytes' import {ListItem} from 'material-ui/List'; import Divider from 'material-ui/Divider'; +import Spinner24 from './images/spinner_24.gif' +import LinearProgress from 'material-ui/LinearProgress'; + const contentIcon = (type, category) => { if(category == 'xxx') { @@ -150,98 +153,195 @@ const contentIcon = (type, category) => { }; export {contentIcon} -export default (props) => { - -const torrent = props.torrent; -return ( -
- { - const link = '/torrent/' + torrent.hash; - if(e.button === 1) - return false; +export default class Torrent extends Component { + state = { + downloading: false, + askDownloading: false, + downloadProgress: {} + } + componentDidMount() + { + this.downloading = (hash) => { + if(this.props.torrent.hash != hash) + return; - if(e.ctrlKey && e.button === 0) { - let win = window.open(link, '_blank'); - //win.focus(); - return true; - } + this.setState({downloading: true}) + } + window.torrentSocket.on('downloading', this.downloading); - window.router(link) - }} - primaryText={ - { - if(node) - node.onclick = () => { return false } - }}> - { + if(this.props.torrent.hash != hash) + return; + + this.setState({downloading: false}) + } + window.torrentSocket.on('downloadDone', this.downloadDone); + + this.downloadProgress = (hash, progress) => { + if(this.props.torrent.hash != hash) + return; + + this.setState({ + downloading: true, + downloadProgress: progress + }) + } + window.torrentSocket.on('downloadProgress', this.downloadProgress); + } + componentWillUnmount() + { + if(this.downloading) + window.torrentSocket.off('downloading', this.downloading); + if(this.downloadDone) + window.torrentSocket.off('downloadDone', this.downloadDone); + if(this.downloadProgress) + window.torrentSocket.off('downloadProgress', this.downloadProgress); + } + render() + { + const torrent = this.props.torrent; + return ( +
+ { + const link = '/torrent/' + torrent.hash; + if(e.button === 1) + return false; + + if(e.ctrlKey && e.button === 0) { + let win = window.open(link, '_blank'); + //win.focus(); + return true; + } + + window.router(link) + }} + primaryText={ + { + if(node) + node.onclick = () => { return false } }}> - {torrent.name} - - - } - secondaryText={ - { - if(node) - node.onclick = () => { return false } - }}> -
-
- { - formatBytes(torrent.size, 1) + ' (' + torrent.files + ' files)' - } -
- { - torrent.path && torrent.path.length > 0 - ? - torrent.path.map((path, index) => { - return
{path}
- }) - : - null - } - { - torrent.seeders || torrent.leechers || torrent.completed - ? -
- 0 ? '#00C853' : 'grey')}}>{torrent.seeders} seeders - 0 ? '#AA00FF' : 'grey'), marginLeft: '12px'}}>{torrent.leechers} leechers - 0 ? '#FF6D00' : 'grey'), marginLeft: '12px'}}>{torrent.completed} completed -
- : - null - } -
-
- } - leftIcon={contentIcon(torrent.contentType, torrent.contentCategory)} - rightIcon={ - - { - e.preventDefault(); - e.stopPropagation(); - var win = window.open(`magnet:?xt=urn:btih:${torrent.hash}`, '_self'); - }} viewBox="0 0 24 24"> - + + {torrent.name} + - } - /> - -
- ) -} + } + secondaryText={ + { + if(node) + node.onclick = () => { return false } + }}> +
+
+
+ { + formatBytes(torrent.size, 1) + ' (' + torrent.files + ' files)' + } +
+ { + this.state.downloading + && + + } +
+ { + torrent.path && torrent.path.length > 0 + ? + torrent.path.map((path, index) => { + return
{path}
+ }) + : + null + } + { + torrent.seeders || torrent.leechers || torrent.completed + ? +
+ 0 ? '#00C853' : 'grey')}}>{torrent.seeders} seeders + 0 ? '#AA00FF' : 'grey'), marginLeft: '12px'}}>{torrent.leechers} leechers + 0 ? '#FF6D00' : 'grey'), marginLeft: '12px'}}>{torrent.completed} completed +
+ : + null + } +
+
+ } + leftIcon={contentIcon(torrent.contentType, torrent.contentCategory)} + rightIcon={ +
+ { + !this.state.askDownloading && !this.state.downloading + ? + + { + e.preventDefault(); + e.stopPropagation(); + this.setState({askDownloading: true}) + window.torrentSocket.emit('download', `magnet:?xt=urn:btih:${torrent.hash}`) + }} viewBox="0 0 56 56"> + + + + + + + + : + this.state.askDownloading && !this.state.downloading + && + + } + + { + e.preventDefault(); + e.stopPropagation(); + var win = window.open(`magnet:?xt=urn:btih:${torrent.hash}`, '_self'); + }} viewBox="0 0 24 24"> + + +
+ } + /> + +
+ ) + } +} \ No newline at end of file diff --git a/src/background/spider.js b/src/background/spider.js index 55c7752..90f709b 100644 --- a/src/background/spider.js +++ b/src/background/spider.js @@ -584,11 +584,15 @@ setInterval(() => { recive('download', (magnet) => { console.log('download', magnet) + if(torrentClient.get(magnet)) + return + torrentClient.add(magnet, {path: config.client.downloadPath}, (torrent) =>{ torrentClientHashMap[torrent.infoHash] = magnet send('downloading', torrent.infoHash) torrent.on('done', () => { + console.log('download done', torrent.infoHash) delete torrentClientHashMap[torrent.infoHash] send('downloadDone', torrent.infoHash) })