feat(download): more control over torrent download

This commit is contained in:
Alexey Kasyanchuk
2018-02-08 13:41:03 +03:00
parent 075bf7a9da
commit 6b288741d1
2 changed files with 87 additions and 18 deletions

View File

@ -24,6 +24,7 @@ const quotaDebug = _debug('main:quota');
const {torrentTypeDetect} = require('../app/content');
const torrentClient = require('./torrentClient')
let torrentClientHashMap = {}
// Start server
//server.listen(config.httpPort);
@ -326,6 +327,15 @@ setInterval(() => {
{
callback(baseRowData(torrent))
}
if(torrentClientHashMap[hash])
{
const torrent = torrentClient.get(torrentClientHashMap[hash])
if(!torrent)
return
send('downloading', torrent.infoHash)
}
});
});
@ -575,11 +585,20 @@ setInterval(() => {
{
console.log('download', magnet)
torrentClient.add(magnet, {path: config.client.downloadPath}, (torrent) =>{
send('downloadMetadata', torrent.infoHash)
torrentClientHashMap[torrent.infoHash] = magnet
send('downloading', torrent.infoHash)
torrent.on('done', () => send('downloadDone', torrent.infoHash))
torrent.on('done', () => {
delete torrentClientHashMap[torrent.infoHash]
send('downloadDone', torrent.infoHash)
})
let now = Date.now()
torrent.on('download', (bytes) => {
if(Date.now() - now < 100)
return
now = Date.now()
send('downloadProgress', torrent.infoHash, {
bytes,
downloaded: torrent.downloaded,
@ -590,6 +609,28 @@ setInterval(() => {
})
});
recive('downloadCancel', (hash, callback) =>
{
const id = torrentClientHashMap[hash]
if(!id)
{
callback(false)
return
}
client.remove(id, (err) => {
if(err)
{
callback(false)
return
}
delete torrentClientHashMap[hash]
if(callback)
callback(true)
})
})
let socketIPV4 = () => {
let ip = socket.request.connection.remoteAddress;
if (ipaddr.IPv4.isValid(ip)) {