From 070119c352727ed0d18245c9f62d370624696ec0 Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Sat, 30 Jun 2018 04:15:12 +0300 Subject: [PATCH] feat(downloading): torrent pause feature --- src/app/torrent.js | 45 +++++++++++++++++++++---- src/background/api.js | 60 +++++++++++++++++++++++++++++++-- src/background/torrentClient.js | 7 +++- translations/en.json | 3 +- translations/ru.json | 3 +- translations/ua.json | 3 +- 6 files changed, 107 insertions(+), 14 deletions(-) diff --git a/src/app/torrent.js b/src/app/torrent.js index ad4f715..7080519 100644 --- a/src/app/torrent.js +++ b/src/app/torrent.js @@ -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 (
@@ -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={ -
+
+ { + // mark delete after finish + canPause + && + + + { + e.preventDefault(); + e.stopPropagation(); + window.torrentSocket.emit('downloadUpdate', torrent.hash, {pause: 'switch'}) + }} viewBox="0 0 438.536 438.536"> + + + + + + + + } { // mark delete after finish canDeleteDownloadAfterFinish @@ -364,7 +395,7 @@ export default class Torrent extends Component { { e.preventDefault(); e.stopPropagation(); diff --git a/src/background/api.js b/src/background/api.js index b0b1eec..02084ed 100644 --- a/src/background/api.js +++ b/src/background/api.js @@ -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 }))) }) diff --git a/src/background/torrentClient.js b/src/background/torrentClient.js index 7b78fda..3885b0b 100644 --- a/src/background/torrentClient.js +++ b/src/background/torrentClient.js @@ -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 + } } }) } diff --git a/translations/en.json b/translations/en.json index 9165d81..de809cf 100644 --- a/translations/en.json +++ b/translations/en.json @@ -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" } } \ No newline at end of file diff --git a/translations/ru.json b/translations/ru.json index c1c0fda..ea6eac8 100644 --- a/translations/ru.json +++ b/translations/ru.json @@ -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": "Пауза скачки торрента" } } \ No newline at end of file diff --git a/translations/ua.json b/translations/ua.json index 8fdba95..19277d7 100644 --- a/translations/ua.json +++ b/translations/ua.json @@ -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" } } \ No newline at end of file