Improve duplicate torrent handling

master
Torjus 2018-07-27 15:11:53 +02:00
parent b5494798f9
commit 9f9d2c1c65
4 changed files with 80 additions and 32 deletions

View File

@ -28,36 +28,63 @@ class FileChatEntry extends Component
}); });
}; };
handleTorrent = (torrent) =>
{
// Torrent already done, this can happen if the
// same file was sent multiple times.
if (torrent.progress === 1)
{
this.setState({
files: torrent.files,
numPeers: torrent.numPeers,
progress: 1,
active: false
});
return;
}
const onProgress = () =>
{
this.setState({
numPeers : torrent.numPeers,
progress : torrent.progress
});
};
onProgress();
setInterval(onProgress, 500);
torrent.on('done', () =>
{
onProgress();
clearInterval(onProgress);
this.setState({
files : torrent.files,
active: false
});
});
};
download = () => download = () =>
{ {
this.setState({ this.setState({
active : true active : true
}); });
const magnet = this.props.message.file.magnet;
client.add(this.props.message.file.magnet, (torrent) => const existingTorrent = client.get(magnet);
if (existingTorrent)
{ {
const onProgress = () => // Never add duplicate torrents, use the existing one instead.
{ return this.handleTorrent(existingTorrent);
this.setState({ }
numPeers : torrent.numPeers,
progress : torrent.progress
});
};
setInterval(onProgress, 500); client.add(magnet, this.handleTorrent);
onProgress();
torrent.on('done', () =>
{
onProgress();
clearInterval(onProgress);
this.setState({
files : torrent.files,
active: false
});
});
});
} }
render() render()

View File

@ -1,9 +1,11 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import WebTorrent from 'webtorrent'; import WebTorrent from 'webtorrent';
import createTorrent from 'create-torrent';
import dragDrop from 'drag-drop'; import dragDrop from 'drag-drop';
import * as stateActions from '../../redux/stateActions'; import * as stateActions from '../../redux/stateActions';
import * as requestActions from '../../redux/requestActions'; import * as requestActions from '../../redux/requestActions';
import { store } from '../../store'; import { store } from '../../store';
import { promisify } from 'util';
export const client = new WebTorrent(); export const client = new WebTorrent();
@ -15,17 +17,35 @@ const notifyPeers = (file) =>
store.dispatch(requestActions.sendChatFile(file, displayName, picture)); store.dispatch(requestActions.sendChatFile(file, displayName, picture));
}; };
const shareFiles = (files) => const shareFiles = async (files) =>
{ {
client.seed(files, (torrent) => createTorrent(files, (err, torrent) =>
{ {
notifyPeers({ if (err)
magnet : torrent.magnetURI {
console.error('Error creating torrent', err);
return;
}
const existingTorrent = client.get(torrent);
if (existingTorrent)
{
return notifyPeers({
magnet: existingTorrent.magnetURI
});
}
client.seed(files, (newTorrent) =>
{
notifyPeers({
magnet : newTorrent.magnetURI
});
}); });
}); });
}; };
dragDrop('body', shareFiles); dragDrop('body', async (files) => await shareFiles(files));
class FileSharing extends Component class FileSharing extends Component
{ {
@ -36,11 +56,11 @@ class FileSharing extends Component
this.fileInput = React.createRef(); this.fileInput = React.createRef();
} }
handleFileChange = (event) => handleFileChange = async (event) =>
{ {
if (event.target.files.length > 0) if (event.target.files.length > 0)
{ {
shareFiles(event.target.files); await shareFiles(event.target.files);
} }
}; };

6
app/package-lock.json generated
View File

@ -3109,9 +3109,9 @@
} }
}, },
"create-torrent": { "create-torrent": {
"version": "3.32.0", "version": "3.32.1",
"resolved": "https://registry.npmjs.org/create-torrent/-/create-torrent-3.32.0.tgz", "resolved": "https://registry.npmjs.org/create-torrent/-/create-torrent-3.32.1.tgz",
"integrity": "sha512-l9chXj5LLyVFfPF6nFCWlm5/Wt+04d+mXUpG5LJAogeyRruWfjnUozfmQspAi6iW91ibp7qKBuFMPJViz5lp1Q==", "integrity": "sha512-8spZUeFyVc+2mGnWBRTuLOhuHmHrmUomFWf7QvxztCEvTpn5SIrvF8F+HKdkzBPM9B7v/2w+f/65jqLWBXSndg==",
"requires": { "requires": {
"bencode": "^2.0.0", "bencode": "^2.0.0",
"block-stream2": "^1.0.0", "block-stream2": "^1.0.0",

View File

@ -9,6 +9,7 @@
"dependencies": { "dependencies": {
"babel-runtime": "^6.26.0", "babel-runtime": "^6.26.0",
"classnames": "^2.2.6", "classnames": "^2.2.6",
"create-torrent": "^3.32.1",
"debug": "^3.1.0", "debug": "^3.1.0",
"domready": "^1.0.8", "domready": "^1.0.8",
"drag-drop": "^4.2.0", "drag-drop": "^4.2.0",