Skip to content

Commit

Permalink
feat(drop): support drop torrent file from web version #99
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Sep 28, 2019
1 parent a33b2d8 commit a997929
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
14 changes: 12 additions & 2 deletions src/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,21 @@ class App extends Component {
changeLanguage(lang, () => this.forceUpdate())
})

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

torrentSocket.emit('dropTorrents', Array.from(files).filter(file => (file.type == 'application/x-bittorrent' || file.type == '')).map(file => file.path))
files = await Promise.all(Array.from(files).filter(file => (file.type == 'application/x-bittorrent' || file.type == '')).map(file => {
if(file.path)
return {path: file.path};

return new Promise(resolve => {
const reader = new FileReader();
reader.onload = () => resolve({data: reader.result});
reader.readAsArrayBuffer(file);
})
}));
torrentSocket.emit('dropTorrents', files);
}

document.addEventListener('dragover', (event) => {
Expand Down
27 changes: 18 additions & 9 deletions src/background/spider.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,24 +791,33 @@ module.exports = function (send, recive, dataDirectory, version, env)

recive('dropTorrents', (pathTorrents) => {
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 => {
const addTorrents = (torrents) => {
torrents.map(({data, path}) => {
try {
return ({
torrent: parseTorrent(fs.readFileSync(path)),
torrent: parseTorrent(data),
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')
.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')
}

if(pathTorrents[0].data) {
addTorrents(pathTorrents.map(({data}) => ({data, path: '.'})));
} else {
const torrents = _.flatten(pathTorrents.map(({path}) => directoryFilesRecursive(path)))
.filter(path => mime.getType(path) == 'application/x-bittorrent')
.map(path => ({data: fs.readFileSync(path), path}));
addTorrents(torrents);
}
})

checkInternet((connected) => {
Expand Down

0 comments on commit a997929

Please sign in to comment.