diff --git a/src/app/app.js b/src/app/app.js index 8d1ecdc..a4a6223 100644 --- a/src/app/app.js +++ b/src/app/app.js @@ -151,7 +151,7 @@ class App extends Component { if(!files || files.length == 0) return - torrentSocket.emit('dropTorrents', Array.from(files).filter(file => file.type == 'application/x-bittorrent').map(file => file.path)) + torrentSocket.emit('dropTorrents', Array.from(files).filter(file => (file.type == 'application/x-bittorrent' || file.type == '')).map(file => file.path)) } document.addEventListener('dragover', (event) => { diff --git a/src/background/directoryFilesRecursive.js b/src/background/directoryFilesRecursive.js new file mode 100644 index 0000000..13dec06 --- /dev/null +++ b/src/background/directoryFilesRecursive.js @@ -0,0 +1,24 @@ +const fs = require('fs') + +function directoryFilesRecursive (directory, filesList = []) { + let files; + try { + files = fs.readdirSync(directory) + } catch(err) { + if(err.code !== 'ENOTDIR') + throw err + else + return [directory] // if file, return file + } + for (const file of files) { + const filePath = `${directory}/${file}` + if (fs.statSync(filePath).isDirectory()) { + directoryFilesRecursive(filePath, filesList) + } else { + filesList.push(filePath) + } + } + return filesList +} + +module.exports = directoryFilesRecursive diff --git a/src/background/spider.js b/src/background/spider.js index cc02ea8..36ad716 100644 --- a/src/background/spider.js +++ b/src/background/spider.js @@ -31,6 +31,9 @@ const checkInternet = require('./checkInternet') const {torrentTypeDetect} = require('../app/content'); const torrentClient = require('./torrentClient') +const directoryFilesRecursive = require('./directoryFilesRecursive') +const _ = require('lodash') +const mime = require('mime'); // Start server //server.listen(config.httpPort); @@ -495,10 +498,10 @@ module.exports = function (send, recive, dataDirectory, version, env) } const insertMetadata = (metadata, infohash, rinfo) => { - logT('spider', 'finded torrent', metadata.info.name, ' and add to database'); - const bufferToString = (buffer) => Buffer.isBuffer(buffer) ? buffer.toString() : buffer + logT('spider', 'finded torrent', bufferToString(metadata.info.name), 'and add to database'); + const hash = infohash.toString('hex'); let size = metadata.info.length ? metadata.info.length : 0; let filesCount = 1; @@ -595,9 +598,25 @@ module.exports = function (send, recive, dataDirectory, version, env) } recive('dropTorrents', (pathTorrents) => { - logT('drop', '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})) + logT('drop', 'drop torrents and replicate from original torrent files') + const torrents = _.flatten(pathTorrents.map(path => directoryFilesRecursive(path))) + .filter(path => mime.getType(path) == 'application/x-bittorrent') + .map(path => { + try { + return ({ + torrent: parseTorrent(fs.readFileSync(path)), + path + }) + } catch(err) { + logT('drop', 'error on parse torrent:', path) + } + }) + .filter(torrent => torrent) + torrents.forEach(({torrent, path}) => { + insertMetadata(torrent, torrent.infoHashBuffer, {address: '127.0.0.1', port: 666}) + logT('drop', 'copied torrent to db:', path) + }) + logT('drop', 'torrent finish adding to db') }) checkInternet((connected) => {