fix(downloading): fix state on some torrent cases
This commit is contained in:
parent
0d81364ca8
commit
ea07872137
@ -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={<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 && !this.state.downloading
|
||||
!this.state.downloaded && !this.state.downloading && !this.state.startingDownloading
|
||||
&&
|
||||
<RaisedButton
|
||||
href={`magnet:?xt=urn:btih:${this.torrent.hash}`}
|
||||
@ -373,8 +382,10 @@ export default class TorrentPage extends Page {
|
||||
style={{marginTop: 8}}
|
||||
onClick={(e) => {
|
||||
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={
|
||||
<svg viewBox="0 0 56 56" fill='white'>
|
||||
|
@ -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}
|
||||
/>
|
||||
<div className='pad0-75' style={{marginLeft: 20}} style={{color: 'rgb(0, 188, 212)'}}>{this.state.downloadProgress && (this.state.downloadProgress.progress * 100).toFixed(1)}%</div>
|
||||
<div style={{marginLeft: 5, color: 'rgb(0, 188, 212)'}}>{this.state.downloadProgress && formatBytes(this.state.downloadProgress.downloadSpeed || 0, 0)}/s</div>
|
||||
{
|
||||
this.state.downloadProgress.progress !== 1
|
||||
&&
|
||||
<div style={{marginLeft: 5, color: 'rgb(0, 188, 212)'}}>{this.state.downloadProgress && formatBytes(this.state.downloadProgress.downloadSpeed || 0, 0)}/s</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
@ -326,7 +335,7 @@ export default class Torrent extends Component {
|
||||
rightIcon={
|
||||
<div className='row inline' style={{width: 63}}>
|
||||
{
|
||||
!this.state.askDownloading && !this.state.downloading
|
||||
!this.state.startingDownloading && !this.state.downloading && !this.state.downloaded
|
||||
?
|
||||
<a href={`magnet:?xt=urn:btih:${torrent.hash}`}>
|
||||
<svg style={{
|
||||
@ -336,8 +345,10 @@ export default class Torrent extends Component {
|
||||
}} onClick={(e) => {
|
||||
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">
|
||||
<g>
|
||||
<path d="M35.586,41.586L31,46.172V28c0-1.104-0.896-2-2-2s-2,0.896-2,2v18.172l-4.586-4.586c-0.781-0.781-2.047-0.781-2.828,0
|
||||
@ -360,7 +371,7 @@ export default class Torrent extends Component {
|
||||
</svg>
|
||||
</a>
|
||||
:
|
||||
this.state.askDownloading && !this.state.downloading
|
||||
this.state.startingDownloading && !this.state.downloading
|
||||
?
|
||||
<div className="overlay-loader">
|
||||
<div className="loader">
|
||||
@ -374,19 +385,33 @@ export default class Torrent extends Component {
|
||||
</div>
|
||||
</div>
|
||||
:
|
||||
this.state.askDownloading && this.state.downloading
|
||||
&&
|
||||
<a href={`magnet:?xt=urn:btih:${torrent.hash}`}>
|
||||
<svg style={{
|
||||
height: '24px',
|
||||
fill: torrent.contentCategory != 'xxx' ? (torrent.peer ? '#5643db' : 'black') : (torrent.peer ? '#9083e2' : 'grey')
|
||||
}} onClick={(e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
this.state.downloaded
|
||||
?
|
||||
<a href={`magnet:?xt=urn:btih:${torrent.hash}`}>
|
||||
<svg style={{
|
||||
height: '24px',
|
||||
fill: '#00C853'
|
||||
}} onClick={(e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
window.torrentSocket.emit('downloadCancel', torrent.hash)
|
||||
}} viewBox="0 0 18 18"><path d="M9 1C4.58 1 1 4.58 1 9s3.58 8 8 8 8-3.58 8-8-3.58-8-8-8zm4 10.87L11.87 13 9 10.13 6.13 13 5 11.87 7.87 9 5 6.13 6.13 5 9 7.87 11.87 5 13 6.13 10.13 9 13 11.87z"/></svg>
|
||||
</a>
|
||||
window.torrentSocket.emit('downloadCancel', torrent.hash)
|
||||
}} viewBox="0 0 18 18"><path d="M9 1C4.58 1 1 4.58 1 9s3.58 8 8 8 8-3.58 8-8-3.58-8-8-8zm4 10.87L11.87 13 9 10.13 6.13 13 5 11.87 7.87 9 5 6.13 6.13 5 9 7.87 11.87 5 13 6.13 10.13 9 13 11.87z"/></svg>
|
||||
</a>
|
||||
:
|
||||
this.state.downloading
|
||||
&&
|
||||
<a href={`magnet:?xt=urn:btih:${torrent.hash}`}>
|
||||
<svg style={{
|
||||
height: '24px',
|
||||
fill: torrent.contentCategory != 'xxx' ? (torrent.peer ? '#5643db' : 'black') : (torrent.peer ? '#9083e2' : 'grey')
|
||||
}} onClick={(e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
window.torrentSocket.emit('downloadCancel', torrent.hash)
|
||||
}} viewBox="0 0 18 18"><path d="M9 1C4.58 1 1 4.58 1 9s3.58 8 8 8 8-3.58 8-8-3.58-8-8-8zm4 10.87L11.87 13 9 10.13 6.13 13 5 11.87 7.87 9 5 6.13 6.13 5 9 7.87 11.87 5 13 6.13 10.13 9 13 11.87z"/></svg>
|
||||
</a>
|
||||
}
|
||||
<a style={{float: 'right'}} href={`magnet:?xt=urn:btih:${torrent.hash}`}>
|
||||
<svg style={{
|
||||
|
@ -576,12 +576,14 @@ module.exports = async ({
|
||||
callback(true)
|
||||
});
|
||||
|
||||
torrentClient._add = (torrentObject, savePath) =>
|
||||
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
|
||||
|
@ -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
|
Loading…
Reference in New Issue
Block a user