Improve duplicate torrent handling
parent
b5494798f9
commit
9f9d2c1c65
|
|
@ -28,14 +28,22 @@ class FileChatEntry extends Component
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
download = () =>
|
handleTorrent = (torrent) =>
|
||||||
|
{
|
||||||
|
// Torrent already done, this can happen if the
|
||||||
|
// same file was sent multiple times.
|
||||||
|
if (torrent.progress === 1)
|
||||||
{
|
{
|
||||||
this.setState({
|
this.setState({
|
||||||
active : true
|
files: torrent.files,
|
||||||
|
numPeers: torrent.numPeers,
|
||||||
|
progress: 1,
|
||||||
|
active: false
|
||||||
});
|
});
|
||||||
|
|
||||||
client.add(this.props.message.file.magnet, (torrent) =>
|
return;
|
||||||
{
|
}
|
||||||
|
|
||||||
const onProgress = () =>
|
const onProgress = () =>
|
||||||
{
|
{
|
||||||
this.setState({
|
this.setState({
|
||||||
|
|
@ -44,9 +52,10 @@ class FileChatEntry extends Component
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
setInterval(onProgress, 500);
|
|
||||||
onProgress();
|
onProgress();
|
||||||
|
|
||||||
|
setInterval(onProgress, 500);
|
||||||
|
|
||||||
torrent.on('done', () =>
|
torrent.on('done', () =>
|
||||||
{
|
{
|
||||||
onProgress();
|
onProgress();
|
||||||
|
|
@ -57,7 +66,25 @@ class FileChatEntry extends Component
|
||||||
active: false
|
active: false
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
download = () =>
|
||||||
|
{
|
||||||
|
this.setState({
|
||||||
|
active : true
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const magnet = this.props.message.file.magnet;
|
||||||
|
|
||||||
|
const existingTorrent = client.get(magnet);
|
||||||
|
|
||||||
|
if (existingTorrent)
|
||||||
|
{
|
||||||
|
// Never add duplicate torrents, use the existing one instead.
|
||||||
|
return this.handleTorrent(existingTorrent);
|
||||||
|
}
|
||||||
|
|
||||||
|
client.add(magnet, this.handleTorrent);
|
||||||
}
|
}
|
||||||
|
|
||||||
render()
|
render()
|
||||||
|
|
|
||||||
|
|
@ -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) =>
|
||||||
|
{
|
||||||
|
if (err)
|
||||||
|
{
|
||||||
|
console.error('Error creating torrent', err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const existingTorrent = client.get(torrent);
|
||||||
|
|
||||||
|
if (existingTorrent)
|
||||||
|
{
|
||||||
|
return notifyPeers({
|
||||||
|
magnet: existingTorrent.magnetURI
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
client.seed(files, (newTorrent) =>
|
||||||
{
|
{
|
||||||
notifyPeers({
|
notifyPeers({
|
||||||
magnet : torrent.magnetURI
|
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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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",
|
||||||
|
|
|
||||||
|
|
@ -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",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue