diff --git a/src/app/torrent-page.js b/src/app/torrent-page.js index 0064b15..a2d4e16 100644 --- a/src/app/torrent-page.js +++ b/src/app/torrent-page.js @@ -144,8 +144,9 @@ export default class TorrentPage extends Page { searchingIndicator: false, voting: false, voted: false, - askDownloading: false, downloading: false, + downloaded: false, + startingDownloading: false, downloadProgress: {} }; this.setTitle('Information about torrent'); @@ -242,7 +243,10 @@ export default class TorrentPage extends Page { if(this.props.hash != hash) return; - this.setState({downloading: true}) + this.setState({ + downloading: true, + startingDownloading: false + }) } window.torrentSocket.on('downloading', this.downloading); @@ -252,7 +256,8 @@ export default class TorrentPage extends Page { this.setState({ downloading: false, - askDownloading: !canceled + startingDownloading: false, + downloaded: !canceled }) } window.torrentSocket.on('downloadDone', this.downloadDone); @@ -261,7 +266,11 @@ export default class TorrentPage extends Page { if(this.props.hash != hash) return; - this.setState({downloadProgress: progress}) + this.setState({ + downloading: true, + startingDownloading: false, + downloadProgress: progress + }) } window.torrentSocket.on('downloadProgress', this.downloadProgress); @@ -362,7 +371,7 @@ export default class TorrentPage extends Page { icon={} /> { - !this.state.askDownloading && !this.state.downloading + !this.state.downloaded && !this.state.downloading && !this.state.startingDownloading && { e.preventDefault(); - this.setState({askDownloading: true}) - window.torrentSocket.emit('download', this.torrent) + window.torrentSocket.emit('download', this.torrent, null, (added) => { + if(added) + this.setState({startingDownloading: true}) + }) }} icon={ diff --git a/src/app/torrent.js b/src/app/torrent.js index e6212f2..ddd827b 100644 --- a/src/app/torrent.js +++ b/src/app/torrent.js @@ -159,7 +159,8 @@ export {contentIcon} export default class Torrent extends Component { state = { downloading: false, - askDownloading: false, + downloaded: false, + startingDownloading: false, downloadProgress: {} } constructor(props) @@ -180,7 +181,10 @@ export default class Torrent extends Component { if(this.props.torrent.hash != hash) return; - this.setState({downloading: true}) + this.setState({ + downloading: true, + startingDownloading: false + }) } window.torrentSocket.on('downloading', this.downloading); @@ -190,7 +194,8 @@ export default class Torrent extends Component { this.setState({ downloading: false, - askDownloading: !canceled + downloaded: !canceled, + startingDownloading: false }) } window.torrentSocket.on('downloadDone', this.downloadDone); @@ -201,7 +206,7 @@ export default class Torrent extends Component { this.setState({ downloading: true, - askDownloading: true, + startingDownloading: false, downloadProgress: progress }) } @@ -316,7 +321,11 @@ export default class Torrent extends Component { value={this.state.downloadProgress && (this.state.downloadProgress.progress ? this.state.downloadProgress.progress : 0) * 100} />
{this.state.downloadProgress && (this.state.downloadProgress.progress * 100).toFixed(1)}%
-
{this.state.downloadProgress && formatBytes(this.state.downloadProgress.downloadSpeed || 0, 0)}/s
+ { + this.state.downloadProgress.progress !== 1 + && +
{this.state.downloadProgress && formatBytes(this.state.downloadProgress.downloadSpeed || 0, 0)}/s
+ } } @@ -326,7 +335,7 @@ export default class Torrent extends Component { rightIcon={
{ - !this.state.askDownloading && !this.state.downloading + !this.state.startingDownloading && !this.state.downloading && !this.state.downloaded ? { e.preventDefault(); e.stopPropagation(); - this.setState({askDownloading: true}) - window.torrentSocket.emit('download', torrent) + window.torrentSocket.emit('download', torrent, null, (added) => { + if(added) + this.setState({startingDownloading: true}) + }) }} viewBox="0 0 56 56">
@@ -374,19 +385,33 @@ export default class Torrent extends Component {
: - this.state.askDownloading && this.state.downloading - && - - { - e.preventDefault(); - e.stopPropagation(); + this.state.downloaded + ? + + { + e.preventDefault(); + e.stopPropagation(); - window.torrentSocket.emit('downloadCancel', torrent.hash) - }} viewBox="0 0 18 18"> - + window.torrentSocket.emit('downloadCancel', torrent.hash) + }} viewBox="0 0 18 18"> + + : + this.state.downloading + && + + { + e.preventDefault(); + e.stopPropagation(); + + window.torrentSocket.emit('downloadCancel', torrent.hash) + }} viewBox="0 0 18 18"> + } + torrentClient._add = (torrentObject, savePath, callback) => { const magnet = `magnet:?xt=urn:btih:${torrentObject.hash}` console.log('download', magnet) if(torrentClient.get(magnet)) { console.log('aready added') + if(callback) + callback(false) return } @@ -596,7 +598,6 @@ module.exports = async ({ torrent.on('done', () => { console.log('download done', torrent.infoHash) - delete torrentClientHashMap[torrent.infoHash] send('downloadDone', torrent.infoHash) }) @@ -614,6 +615,9 @@ module.exports = async ({ timeRemaining: torrent.timeRemaining }) }) + + if(callback) + callback(true) } recive('download', torrentClient._add); @@ -623,6 +627,7 @@ module.exports = async ({ const id = torrentClientHashMap[hash] if(!id) { + console.log('cant find torrent for removing', hash) if(callback) callback(false) return diff --git a/src/background/torrentClient.js b/src/background/torrentClient.js index d328d20..9cbf56a 100644 --- a/src/background/torrentClient.js +++ b/src/background/torrentClient.js @@ -4,41 +4,41 @@ const fs = require('fs') const torrentClient = new WebTorrent({webSeeds: false}) torrentClient.saveSession = (sessionFile) => { fs.writeFileSync(sessionFile, JSON.stringify({ - torrents: torrentClient.torrents.map(torrent => ({ - infoHash: torrent.infoHash, - path: torrent.path, - torrent: torrent.torrentObject - })) - }, null, 4), 'utf8'); + torrents: torrentClient.torrents.map(torrent => ({ + infoHash: torrent.infoHash, + path: torrent.path, + torrent: torrent.torrentObject + })) + }, null, 4), 'utf8'); } torrentClient.loadSession = (sessionFile) => { - if(!fs.existsSync(sessionFile)) + if(!fs.existsSync(sessionFile)) { - console.log('no download sessions - ignore') - return - } + console.log('no download sessions - ignore') + return + } const data = fs.readFileSync(sessionFile, 'utf8') - const obj = JSON.parse(data); - if(!obj.torrents) - { - console.log('no torrents list for loading session') - return - } - if(!torrentClient._add) - { - console.log('no overriden _add() method') - return - } - const {torrents} = obj - torrents.forEach(({torrent, infoHash, path}) => { - if(!torrent || !infoHash || !path) - { - console.log('no info for starting download this torrent') - return - } - console.log('restore download session:', torrent.name) - torrentClient._add(torrent, path) - }) + const obj = JSON.parse(data); + if(!obj.torrents) + { + console.log('no torrents list for loading session') + return + } + if(!torrentClient._add) + { + console.log('no overriden _add() method') + return + } + const {torrents} = obj + torrents.forEach(({torrent, infoHash, path}) => { + if(!torrent || !infoHash || !path) + { + console.log('no info for starting download this torrent') + return + } + console.log('restore download session:', torrent.name) + torrentClient._add(torrent, path) + }) } module.exports = torrentClient \ No newline at end of file