feat(downloading): torrent pause feature

This commit is contained in:
Alexey Kasyanchuk 2018-06-30 04:15:12 +03:00
parent c086cd2087
commit 070119c352
6 changed files with 107 additions and 14 deletions

View File

@ -163,7 +163,8 @@ export default class Torrent extends Component {
downloaded: false,
startingDownloading: false,
downloadProgress: {},
downloadRemoveOnDone: false
downloadRemoveOnDone: false,
downloadPaused: false,
}
constructor(props)
{
@ -172,13 +173,14 @@ export default class Torrent extends Component {
const download = props.download || props.torrent.download
if(download)
{
const { progress, downloaded, downloadSpeed, removeOnDone } = download
const { progress, downloaded, downloadSpeed, removeOnDone, paused } = download
this.state.downloadProgress = {
progress, downloaded, downloadSpeed
}
this.state.downloading = progress < 1
this.state.downloaded = progress === 1
this.state.downloadRemoveOnDone = removeOnDone
this.state.downloadRemoveOnDone = removeOnDone
this.state.downloadPaused = paused
}
}
@ -224,7 +226,8 @@ export default class Torrent extends Component {
return;
this.setState({
downloadRemoveOnDone: options.removeOnDone
downloadRemoveOnDone: options.removeOnDone,
downloadPaused: options.paused
})
}
window.torrentSocket.on('downloadUpdate', this.downloadUpdate);
@ -250,7 +253,8 @@ export default class Torrent extends Component {
if(torrent.good > 0 || torrent.bad > 0)
torrentRating = Math.round(rating(torrent.good, torrent.bad) * 100);
const canDeleteDownloadAfterFinish = (this.state.downloading || this.state.startingDownloading) && !this.state.downloaded
const canDeleteDownloadAfterFinish = (this.state.downloading || this.state.startingDownloading) && !this.state.downloaded
const canPause = (this.state.downloading || this.state.startingDownloading)
return (
<div>
@ -354,7 +358,34 @@ export default class Torrent extends Component {
}
leftIcon={contentIcon(torrent.contentType, torrent.contentCategory, torrent.contentCategory != 'xxx' ? (torrent.peer ? '#6f5ee0' : 'grey') : (torrent.peer ? '#9083e2' : '#d3d3d3'))}
rightIcon={
<div className='row inline' style={{width: 63 + (canDeleteDownloadAfterFinish ? 40 : 0)}}>
<div className='row inline' style={{width: 63 + (canDeleteDownloadAfterFinish ? 40 : 0) + (canPause ? 40 : 0)}}>
{
// mark delete after finish
canPause
&&
<ToolTip hint={__('Pause torrent downloading')} right={true}>
<a href={`magnet:?xt=urn:btih:${torrent.hash}`}>
<svg style={{
height: '24px',
marginRight: 16,
fill: this.state.downloadPaused ? '#42f47a' : 'black'
}} onClick={(e) => {
e.preventDefault();
e.stopPropagation();
window.torrentSocket.emit('downloadUpdate', torrent.hash, {pause: 'switch'})
}} viewBox="0 0 438.536 438.536">
<g>
<path d="M164.453,0H18.276C13.324,0,9.041,1.807,5.425,5.424C1.808,9.04,0.001,13.322,0.001,18.271v401.991
c0,4.948,1.807,9.233,5.424,12.847c3.619,3.617,7.902,5.428,12.851,5.428h146.181c4.949,0,9.231-1.811,12.847-5.428
c3.617-3.613,5.424-7.898,5.424-12.847V18.271c0-4.952-1.807-9.231-5.428-12.847C173.685,1.807,169.402,0,164.453,0z"/>
<path d="M433.113,5.424C429.496,1.807,425.215,0,420.267,0H274.086c-4.949,0-9.237,1.807-12.847,5.424
c-3.621,3.615-5.432,7.898-5.432,12.847v401.991c0,4.948,1.811,9.233,5.432,12.847c3.609,3.617,7.897,5.428,12.847,5.428h146.181
c4.948,0,9.229-1.811,12.847-5.428c3.614-3.613,5.421-7.898,5.421-12.847V18.271C438.534,13.319,436.73,9.04,433.113,5.424z"/>
</g>
</svg>
</a>
</ToolTip>
}
{
// mark delete after finish
canDeleteDownloadAfterFinish
@ -364,7 +395,7 @@ export default class Torrent extends Component {
<svg style={{
height: '24px',
marginRight: 16,
fill: this.state.downloadRemoveOnDone ? 'red' : 'black'
fill: this.state.downloadRemoveOnDone ? '#f45c41' : 'black'
}} onClick={(e) => {
e.preventDefault();
e.stopPropagation();

View File

@ -48,7 +48,8 @@ module.exports = async ({
progress: download.progress,
downloadSpeed: download.downloadSpeed,
removeOnDone: download.removeOnDone
removeOnDone: download.removeOnDone,
paused: torrent.paused || torrent._paused
}
}
}
@ -642,6 +643,11 @@ module.exports = async ({
console.log('start downloading', torrent.infoHash, 'to', torrent.path)
send('downloading', torrent.infoHash)
progress(0) // immediately display progress
if(torrent._paused)
{
delete torrent._paused
torrent._pause()
}
})
torrent.on('done', () => {
@ -676,6 +682,43 @@ module.exports = async ({
progress(bytes)
})
//custom api pause
torrent._pause = () => {
console.log('pause torrent downloading', torrent.infoHash)
torrent.pause()
torrent.wires = [];
setTimeout(() => {
if(torrent.paused)
torrent.wires = [];
}, 100) // make sure no more wires appears
}
torrent._restoreWires = () => {
for(const p in torrent._peers){
const wire = torrent._peers[p].wire
if(wire)
torrent.wires.push(wire);
}
}
torrent._resume = () => {
console.log('resume torrent downloading', torrent.infoHash)
torrent._restoreWires()
torrent.resume()
}
// fix wires after pause
const _destroy = torrent._destroy
torrent._destroy = (...args) => {
// fix pause wires closing
if(torrent.paused)
{
torrent._restoreWires()
}
return _destroy.call(torrent, ...args)
}
if(callback)
callback(true)
@ -704,8 +747,18 @@ module.exports = async ({
torrent.removeOnDone = options.removeOnDone == 'switch' ? !torrent.removeOnDone : options.removeOnDone
}
if(typeof options.pause !== 'undefined')
{
const pause = options.pause == 'switch' ? !torrent.paused : options.pause
if(pause)
torrent._pause()
else
torrent._resume()
}
send('downloadUpdate', torrent.infoHash, {
removeOnDone: torrent.removeOnDone
removeOnDone: torrent.removeOnDone,
paused: torrent.paused || torrent._paused
})
})
@ -746,7 +799,8 @@ module.exports = async ({
progress: torrent.progress,
downloadSpeed: torrent.downloadSpeed,
removeOnDone: torrent.removeOnDone
removeOnDone: torrent.removeOnDone,
paused: torrent.paused || torrent._paused
})))
})

View File

@ -10,6 +10,7 @@ torrentClient.saveSession = (sessionFile) => {
torrent: torrent.torrentObject,
removeOnDone: torrent.removeOnDone,
paused: torrent.paused || torrent._paused
}))
}, null, 4), 'utf8');
}
@ -33,7 +34,7 @@ torrentClient.loadSession = (sessionFile) => {
return
}
const {torrents} = obj
torrents.forEach(({torrent, infoHash, path, removeOnDone}) => {
torrents.forEach(({torrent, infoHash, path, removeOnDone, paused}) => {
if(!torrent || !infoHash || !path)
{
console.log('no info for starting download this torrent')
@ -46,6 +47,10 @@ torrentClient.loadSession = (sessionFile) => {
console.log('restore options')
// restore options
download.removeOnDone = removeOnDone
if(paused)
{
download._paused = true
}
}
})
}

View File

@ -174,6 +174,7 @@
"Open torrent in external torrent client": "Open torrent in external torrent client",
"Dont start to seed torrent after download finish": "Dont start to seed torrent after download finish",
"Delete download (files saved)": "Delete download (files saved)",
"Serching metadata in progress... Click will delete this torrent.": "Serching metadata in progress... Click will delete this torrent."
"Serching metadata in progress... Click will delete this torrent.": "Serching metadata in progress... Click will delete this torrent.",
"Pause torrent downloading": "Pause torrent downloading"
}
}

View File

@ -174,6 +174,7 @@
"Open torrent in external torrent client": "Открыть торрент во внешнем торрент-клиенте",
"Dont start to seed torrent after download finish": "Не начинать сидировать торрент после окончания загрузки",
"Delete download (files saved)": "Удалить закачку (сохранив файлы)",
"Serching metadata in progress... Click will delete this torrent.": "Поиск методанных в процессе... Клик удалит этот торрент."
"Serching metadata in progress... Click will delete this torrent.": "Поиск методанных в процессе... Клик удалит этот торрент.",
"Pause torrent downloading": "Пауза скачки торрента"
}
}

View File

@ -174,6 +174,7 @@
"Open torrent in external torrent client": "Open torrent in external torrent client",
"Dont start to seed torrent after download finish": "Dont start to seed torrent after download finish",
"Delete download (files saved)": "Delete download (files saved)",
"Serching metadata in progress... Click will delete this torrent.": "Serching metadata in progress... Click will delete this torrent."
"Serching metadata in progress... Click will delete this torrent.": "Serching metadata in progress... Click will delete this torrent.",
"Pause torrent downloading": "Pause torrent downloading"
}
}