feat(torrents): add support for dropping torrent to base just with window

This commit is contained in:
Alexey Kasyanchuk 2018-07-29 21:21:19 +03:00
parent 01a72e551f
commit 1442a65c6e
4 changed files with 65 additions and 6 deletions

31
package-lock.json generated
View File

@ -13172,6 +13172,37 @@
"integrity": "sha1-bVuTSkVpk7I9N/QKOC1vFmao5cY=",
"dev": true
},
"parse-torrent": {
"version": "6.1.1",
"resolved": "https://registry.npmjs.org/parse-torrent/-/parse-torrent-6.1.1.tgz",
"integrity": "sha512-VOQseFSgXOJE1tnwLJHA6GAILC62GaXRtoCkf3cOiTxMt2P/Xjz1Oe6TVJB7BCm4WkgOY7QS22bjqA7Z4ryuJA==",
"requires": {
"bencode": "^2.0.0",
"blob-to-buffer": "^1.2.6",
"get-stdin": "^6.0.0",
"magnet-uri": "^5.1.3",
"simple-get": "^3.0.1",
"simple-sha1": "^2.0.0",
"uniq": "^1.0.1"
},
"dependencies": {
"get-stdin": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/get-stdin/-/get-stdin-6.0.0.tgz",
"integrity": "sha512-jp4tHawyV7+fkkSKyvjuLZswblUtz+SQKzSWnBbii16BuZksJlU1wuBYXY75r+duh/llF1ur6oNwi+2ZzjKZ7g=="
},
"simple-get": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/simple-get/-/simple-get-3.0.2.tgz",
"integrity": "sha512-dU3TBVIGkP5Hzw6o74hJx+VzTBTX2rqIiLfugs0HdmdVQCQp76XGg2jlBCqfRJfW/n6/mUKTi+s3rnzX7SgbBA==",
"requires": {
"decompress-response": "^3.3.0",
"once": "^1.3.1",
"simple-concat": "^1.0.0"
}
}
}
},
"parseqs": {
"version": "0.0.5",
"resolved": "https://registry.npmjs.org/parseqs/-/parseqs-0.0.5.tgz",

View File

@ -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",

View File

@ -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;
}

View File

@ -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')
@ -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;
@ -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'],
@ -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
{
@ -648,7 +651,7 @@ app.get('*', function(req, res)
}
else
{
updateTorrent(metadata, infohash, rinfo);
insertMetadata(metadata, infohash, rinfo);
}
});
@ -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