-
-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(downloading): save download session support
- Loading branch information
Showing
3 changed files
with
56 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |