feat(drop): support torrents folder drag and drop with recursive scan torrents files

This commit is contained in:
Alexey Kasyanchuk 2018-08-11 04:35:45 +03:00
parent ae7b6fe793
commit 82e77640d6
3 changed files with 49 additions and 6 deletions

View File

@ -151,7 +151,7 @@ class App extends Component {
if(!files || files.length == 0) if(!files || files.length == 0)
return 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) => { document.addEventListener('dragover', (event) => {

View File

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

View File

@ -31,6 +31,9 @@ const checkInternet = require('./checkInternet')
const {torrentTypeDetect} = require('../app/content'); const {torrentTypeDetect} = require('../app/content');
const torrentClient = require('./torrentClient') const torrentClient = require('./torrentClient')
const directoryFilesRecursive = require('./directoryFilesRecursive')
const _ = require('lodash')
const mime = require('mime');
// Start server // Start server
//server.listen(config.httpPort); //server.listen(config.httpPort);
@ -495,10 +498,10 @@ module.exports = function (send, recive, dataDirectory, version, env)
} }
const insertMetadata = (metadata, infohash, rinfo) => { 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 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'); const hash = infohash.toString('hex');
let size = metadata.info.length ? metadata.info.length : 0; let size = metadata.info.length ? metadata.info.length : 0;
let filesCount = 1; let filesCount = 1;
@ -595,9 +598,25 @@ module.exports = function (send, recive, dataDirectory, version, env)
} }
recive('dropTorrents', (pathTorrents) => { recive('dropTorrents', (pathTorrents) => {
logT('drop', 'drop torrents and replicate from original') logT('drop', 'drop torrents and replicate from original torrent files')
const torrents = pathTorrents.map(path => parseTorrent(fs.readFileSync(path))) const torrents = _.flatten(pathTorrents.map(path => directoryFilesRecursive(path)))
torrents.forEach(torrent => insertMetadata(torrent, torrent.infoHashBuffer, {address: '127.0.0.1', port: 666})) .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) => { checkInternet((connected) => {