fix(downloading): fix state on some torrent cases
This commit is contained in:
@ -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
|
||||
|
@ -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
|
Reference in New Issue
Block a user