fix(downloading): fix state on some torrent cases

This commit is contained in:
Alexey Kasyanchuk
2018-06-27 18:15:36 +03:00
parent 0d81364ca8
commit ea07872137
4 changed files with 102 additions and 61 deletions

View File

@ -576,12 +576,14 @@ module.exports = async ({
callback(true)
});
torrentClient._add = (torrentObject, savePath) =>
torrentClient._add = (torrentObject, savePath, callback) =>
{
const magnet = `magnet:?xt=urn:btih:${torrentObject.hash}`
console.log('download', magnet)
if(torrentClient.get(magnet)) {
console.log('aready added')
if(callback)
callback(false)
return
}
@ -596,7 +598,6 @@ module.exports = async ({
torrent.on('done', () => {
console.log('download done', torrent.infoHash)
delete torrentClientHashMap[torrent.infoHash]
send('downloadDone', torrent.infoHash)
})
@ -614,6 +615,9 @@ module.exports = async ({
timeRemaining: torrent.timeRemaining
})
})
if(callback)
callback(true)
}
recive('download', torrentClient._add);
@ -623,6 +627,7 @@ module.exports = async ({
const id = torrentClientHashMap[hash]
if(!id)
{
console.log('cant find torrent for removing', hash)
if(callback)
callback(false)
return

View File

@ -4,41 +4,41 @@ 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');
torrents: torrentClient.torrents.map(torrent => ({
infoHash: torrent.infoHash,
path: torrent.path,
torrent: torrent.torrentObject
}))
}, null, 4), 'utf8');
}
torrentClient.loadSession = (sessionFile) => {
if(!fs.existsSync(sessionFile))
if(!fs.existsSync(sessionFile))
{
console.log('no download sessions - ignore')
return
}
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)
})
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