feat(drop): support torrents folder drag and drop with recursive scan torrents files
This commit is contained in:
parent
ae7b6fe793
commit
82e77640d6
@ -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) => {
|
||||
|
24
src/background/directoryFilesRecursive.js
Normal file
24
src/background/directoryFilesRecursive.js
Normal 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
|
@ -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) => {
|
||||
|
Loading…
Reference in New Issue
Block a user