From 0d81364ca82268b43833313a583b350466a2fdc2 Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Wed, 27 Jun 2018 04:47:22 +0300 Subject: [PATCH] feat(downloading): save download session support --- src/background/api.js | 10 +++++--- src/background/spider.js | 8 ++++++ src/background/torrentClient.js | 43 ++++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/background/api.js b/src/background/api.js index bf9eabf..33de6b6 100644 --- a/src/background/api.js +++ b/src/background/api.js @@ -576,7 +576,7 @@ module.exports = async ({ callback(true) }); - recive('download', (torrentObject) => + torrentClient._add = (torrentObject, savePath) => { const magnet = `magnet:?xt=urn:btih:${torrentObject.hash}` console.log('download', magnet) @@ -585,12 +585,12 @@ module.exports = async ({ return } - const torrent = torrentClient.add(magnet, {path: config.client.downloadPath}) + const torrent = torrentClient.add(magnet, {path: savePath || config.client.downloadPath}) torrentClientHashMap[torrent.infoHash] = magnet torrent.torrentObject = torrentObject torrent.on('ready', () => { - console.log('start downloading', torrent.infoHash) + console.log('start downloading', torrent.infoHash, 'to', torrent.path) send('downloading', torrent.infoHash) }) @@ -614,7 +614,9 @@ module.exports = async ({ timeRemaining: torrent.timeRemaining }) }) - }); + } + + recive('download', torrentClient._add); recive('downloadCancel', (hash, callback) => { diff --git a/src/background/spider.js b/src/background/spider.js index c87f038..406a4e5 100644 --- a/src/background/spider.js +++ b/src/background/spider.js @@ -771,12 +771,20 @@ setInterval(() => { quotaDebug('disk quota enabled'); } + // load torrents sessions + console.log('restore downloading sessions') + torrentClient.loadSession(dataDirectory + '/downloads.json') + this.stop = async (callback) => { this.closing = true console.log('spider closing...') if(upnp) upnp.ratsUnmap() + // save torrents sessions + console.log('save torrents downloads sessions') + torrentClient.saveSession(dataDirectory + '/downloads.json') + // save feed await feed.save() diff --git a/src/background/torrentClient.js b/src/background/torrentClient.js index 9c087b2..d328d20 100644 --- a/src/background/torrentClient.js +++ b/src/background/torrentClient.js @@ -1,3 +1,44 @@ const WebTorrent = require('webtorrent') -let torrentClient = new WebTorrent({webSeeds: false}) +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'); +} +torrentClient.loadSession = (sessionFile) => { + if(!fs.existsSync(sessionFile)) + { + 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) + }) +} module.exports = torrentClient \ No newline at end of file