From 34e964126b8829fcbb3292de742c2132b07d55ba Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Wed, 18 Apr 2018 23:19:42 +0300 Subject: [PATCH] feat(replication): replicate torrent on open --- src/background/api.js | 4 +++- src/background/spider.js | 24 +++++++++++++----------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/background/api.js b/src/background/api.js index db6459a..c2659bd 100644 --- a/src/background/api.js +++ b/src/background/api.js @@ -91,8 +91,10 @@ module.exports = ({ } delete options.peer; peer.emit('torrent', {hash, options}, (data) => { - console.log('remote torrent result') + console.log('remote torrent result', hash) callback(data) + if(data) + insertTorrentToDB(data, true) // copy torrent to our db }) return; } diff --git a/src/background/spider.js b/src/background/spider.js index dc0d4ab..89b7b36 100644 --- a/src/background/spider.js +++ b/src/background/spider.js @@ -390,7 +390,7 @@ const checkTorrent = (torrent) => { return true } -const insertTorrentToDB = (torrent) => { +const insertTorrentToDB = (torrent, silent) => { if(!torrent) return @@ -432,15 +432,16 @@ const insertTorrentToDB = (torrent) => { mysqlSingle.insertValues('torrents', torrent, function(err, result) { if(result) { - send('newTorrent', { - hash: torrent.hash, - name: torrent.name, - size: torrent.size, - files: torrent.files, - piecelength: torrent.piecelength, - contentType: torrent.contentType, - contentCategory: torrent.contentCategory, - }); + if(!silent) + send('newTorrent', { + hash: torrent.hash, + name: torrent.name, + size: torrent.size, + files: torrent.files, + piecelength: torrent.piecelength, + contentType: torrent.contentType, + contentCategory: torrent.contentCategory, + }); updateTorrentTrackers(torrent.hash); } else @@ -475,7 +476,8 @@ const insertTorrentToDB = (torrent) => { return } if(--filesToAdd === 0) { - send('filesReady', torrent.hash); + if(!silent) + send('filesReady', torrent.hash); } }); });