Skip to content

Commit

Permalink
feat(downloading): save download session support
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Jun 27, 2018
1 parent 97e80a7 commit 652f9a0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/background/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
})

Expand All @@ -614,7 +614,9 @@ module.exports = async ({
timeRemaining: torrent.timeRemaining
})
})
});
}

recive('download', torrentClient._add);

recive('downloadCancel', (hash, callback) =>
{
Expand Down
8 changes: 8 additions & 0 deletions src/background/spider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
43 changes: 42 additions & 1 deletion src/background/torrentClient.js
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 652f9a0

Please sign in to comment.