Skip to content

Commit

Permalink
feat(torrents): add support for dropping torrent to base just with wi…
Browse files Browse the repository at this point in the history
…ndow
  • Loading branch information
DEgITx committed Jul 29, 2018
1 parent 284215b commit 6d82291
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
31 changes: 31 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
"mysql": "^2.15.0",
"nat-upnp": "^1.1.1",
"object-hash": "^1.3.0",
"parse-torrent": "^6.1.1",
"react": "^16.3.2",
"react-dom": "^16.3.2",
"react-input-range": "^1.3.0",
Expand Down
18 changes: 18 additions & 0 deletions src/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,24 @@ class App extends Component {
changeLanguage(lang, () => this.forceUpdate())
})

const processTorrents = (files) => {
if(!files || files.length == 0)
return

torrentSocket.emit('dropTorrents', Array.from(files).filter(file => file.type == 'application/x-bittorrent').map(file => file.path))
}

document.addEventListener('dragover', (event) => {
event.stopPropagation();
event.preventDefault();
event.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
}, false);
document.addEventListener('drop', (event) => {
event.stopPropagation();
event.preventDefault();
processTorrents(event.dataTransfer.files); // FileList object.
}, false);

window.router()
appReady = true;
}
Expand Down
21 changes: 15 additions & 6 deletions src/background/spider.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const config = require('./config');
const client = new (require('./bt/client'))
const spider = new (require('./bt/spider'))(client)
const fs = require('fs');
const parseTorrent = require('parse-torrent')
const {single, pool} = require('./mysql')
const getPeersStatisticUDP = require('./bt/udp-tracker-request')
const crypto = require('crypto')
Expand Down Expand Up @@ -573,9 +574,11 @@ app.get('*', function(req, res)
await mysqlSingle.updateValues('torrents', torrent, {hash: torrent.hash})
}

const updateTorrent = (metadata, infohash, rinfo) => {
const insertMetadata = (metadata, infohash, rinfo) => {
console.log('finded torrent', metadata.info.name, ' and add to database');

const bufferToString = (buffer) => Buffer.isBuffer(buffer) ? buffer.toString() : buffer

const hash = infohash.toString('hex');
let size = metadata.info.length ? metadata.info.length : 0;
let filesCount = 1;
Expand All @@ -595,19 +598,19 @@ app.get('*', function(req, res)
for(let i = 0; i < metadata.info.files.length; i++)
{
let file = metadata.info.files[i];
let filePath = file.path.join('/');
let filePath = bufferToString(file.path).join('/');
filesAdd(filePath, file.length);
size += file.length;
}
}
else
{
filesAdd(metadata.info.name, size)
filesAdd(bufferToString(metadata.info.name), size)
}

const torrentQ = {
hash: hash,
name: metadata.info.name,
name: bufferToString(metadata.info.name),
size: size,
files: filesCount,
piecelength: metadata.info['piece length'],
Expand Down Expand Up @@ -636,7 +639,7 @@ app.get('*', function(req, res)
if(free >= config.spaceDiskLimit)
{
hideFakeTorrents(); // also enable fake torrents;
updateTorrent(metadata, infohash, rinfo);
insertMetadata(metadata, infohash, rinfo);
}
else
{
Expand All @@ -648,7 +651,7 @@ app.get('*', function(req, res)
}
else
{
updateTorrent(metadata, infohash, rinfo);
insertMetadata(metadata, infohash, rinfo);
}
});

Expand All @@ -671,6 +674,12 @@ app.get('*', function(req, res)
client._download(peer, infoHash)
}

recive('dropTorrents', (pathTorrents) => {
console.log('drop torrents and replicate from original')
const torrents = pathTorrents.map(path => parseTorrent(fs.readFileSync(path)))
torrents.forEach(torrent => insertMetadata(torrent, torrent.infoHashBuffer, {address: '127.0.0.1', port: 666}))
})

checkInternet((connected) => {
if(!connected)
return
Expand Down

0 comments on commit 6d82291

Please sign in to comment.