Check if WebRTC is supported before enabling file sharing

master
Torjus 2018-08-01 13:06:56 +02:00
parent 1686e3603f
commit 57fba6c8ed
6 changed files with 55 additions and 20 deletions

View File

@ -172,7 +172,7 @@ module.exports =
'semi': [ 2, 'always' ], 'semi': [ 2, 'always' ],
'semi-spacing': 2, 'semi-spacing': 2,
'space-before-blocks': 2, 'space-before-blocks': 2,
'space-before-function-paren': [ 2, 'never' ], 'space-before-function-paren': [ 2, { anonymous: 'never', named: 'never', 'asyncArrow': 'always'}],
'space-in-parens': [ 2, 'never' ], 'space-in-parens': [ 2, 'never' ],
'spaced-comment': [ 2, 'always' ], 'spaced-comment': [ 2, 'always' ],
'strict': 2, 'strict': 2,

View File

@ -1,14 +1,18 @@
import React from 'react'; import React from 'react';
import WebTorrent from 'webtorrent';
import dragDrop from 'drag-drop'; import dragDrop from 'drag-drop';
import { shareFiles } from './index'; import { shareFiles } from './index';
export const configureDragDrop = () => export const configureDragDrop = () =>
{ {
dragDrop('body', async(files) => await shareFiles(files)); if (WebTorrent.WEBRTC_SUPPORT)
{
dragDrop('body', async (files) => await shareFiles(files));
}
}; };
export const HoldingOverlay = () => ( export const HoldingOverlay = () => (
<div id='holding-overlay'> <div id='holding-overlay'>
Drop files here to share them Drop files here to share them
</div> </div>
); );

View File

@ -2,6 +2,7 @@ import React, { Component, Fragment } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import magnet from 'magnet-uri'; import magnet from 'magnet-uri';
import WebTorrent from 'webtorrent';
import * as requestActions from '../../redux/requestActions'; import * as requestActions from '../../redux/requestActions';
import { saveAs } from 'file-saver/FileSaver'; import { saveAs } from 'file-saver/FileSaver';
import { client } from './index'; import { client } from './index';
@ -106,9 +107,15 @@ class FileEntry extends Component
{!this.state.active && !this.state.files && ( {!this.state.active && !this.state.files && (
<div className='file-info'> <div className='file-info'>
<span className='button' onClick={this.handleDownload}> {WebTorrent.WEBRTC_SUPPORT ? (
<img src='resources/images/download-icon.svg' /> <span className='button' onClick={this.handleDownload}>
</span> <img src='resources/images/download-icon.svg' />
</span>
) : (
<p>
Your browser does not support downloading files using WebTorrent.
</p>
)}
<p>{magnet.decode(this.props.data.file.magnet).dn}</p> <p>{magnet.decode(this.props.data.file.magnet).dn}</p>
</div> </div>

View File

@ -4,13 +4,14 @@ import { connect } from 'react-redux';
import WebTorrent from 'webtorrent'; import WebTorrent from 'webtorrent';
import createTorrent from 'create-torrent'; import createTorrent from 'create-torrent';
import randomString from 'random-string'; import randomString from 'random-string';
import classNames from 'classnames';
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 config from '../../../config'; import config from '../../../config';
import FileEntry, { FileEntryProps } from './FileEntry'; import FileEntry, { FileEntryProps } from './FileEntry';
export const client = new WebTorrent({ export const client = WebTorrent.WEBRTC_SUPPORT && new WebTorrent({
tracker : { tracker : {
rtcConfig : { rtcConfig : {
iceServers : config.turnServers iceServers : config.turnServers
@ -25,7 +26,7 @@ const notifyPeers = (file) =>
store.dispatch(requestActions.sendFile(file, displayName, picture)); store.dispatch(requestActions.sendFile(file, displayName, picture));
}; };
export const shareFiles = async(files) => export const shareFiles = async (files) =>
{ {
const notification = const notification =
{ {
@ -78,7 +79,7 @@ class FileSharing extends Component
this.fileInput = React.createRef(); this.fileInput = React.createRef();
} }
handleFileChange = async(event) => handleFileChange = async (event) =>
{ {
if (event.target.files.length > 0) if (event.target.files.length > 0)
{ {
@ -88,13 +89,19 @@ class FileSharing extends Component
handleClick = () => handleClick = () =>
{ {
// We want to open the file dialog when we click a button if (WebTorrent.WEBRTC_SUPPORT)
// instead of actually rendering the input element itself. {
this.fileInput.current.click(); // We want to open the file dialog when we click a button
// instead of actually rendering the input element itself.
this.fileInput.current.click();
}
}; };
render() render()
{ {
const buttonDescription = WebTorrent.WEBRTC_SUPPORT ?
'Share file' : 'File sharing not supported';
return ( return (
<div data-component='FileSharing'> <div data-component='FileSharing'>
<div className='sharing-toolbar'> <div className='sharing-toolbar'>
@ -109,9 +116,11 @@ class FileSharing extends Component
<div <div
type='button' type='button'
onClick={this.handleClick} onClick={this.handleClick}
className='share-file' className={classNames('share-file', {
disabled : !WebTorrent.WEBRTC_SUPPORT
})}
> >
<span>Share file</span> <span>{buttonDescription}</span>
</div> </div>
</div> </div>

23
app/package-lock.json generated
View File

@ -4969,12 +4969,14 @@
"balanced-match": { "balanced-match": {
"version": "1.0.0", "version": "1.0.0",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"brace-expansion": { "brace-expansion": {
"version": "1.1.11", "version": "1.1.11",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"balanced-match": "^1.0.0", "balanced-match": "^1.0.0",
"concat-map": "0.0.1" "concat-map": "0.0.1"
@ -4989,17 +4991,20 @@
"code-point-at": { "code-point-at": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"concat-map": { "concat-map": {
"version": "0.0.1", "version": "0.0.1",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"console-control-strings": { "console-control-strings": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"core-util-is": { "core-util-is": {
"version": "1.0.2", "version": "1.0.2",
@ -5116,7 +5121,8 @@
"inherits": { "inherits": {
"version": "2.0.3", "version": "2.0.3",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"ini": { "ini": {
"version": "1.3.5", "version": "1.3.5",
@ -5128,6 +5134,7 @@
"version": "1.0.0", "version": "1.0.0",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"number-is-nan": "^1.0.0" "number-is-nan": "^1.0.0"
} }
@ -5142,6 +5149,7 @@
"version": "3.0.4", "version": "3.0.4",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"brace-expansion": "^1.1.7" "brace-expansion": "^1.1.7"
} }
@ -5260,7 +5268,8 @@
"number-is-nan": { "number-is-nan": {
"version": "1.0.1", "version": "1.0.1",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"object-assign": { "object-assign": {
"version": "4.1.1", "version": "4.1.1",
@ -5272,6 +5281,7 @@
"version": "1.4.0", "version": "1.4.0",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"wrappy": "1" "wrappy": "1"
} }
@ -5393,6 +5403,7 @@
"version": "1.0.2", "version": "1.0.2",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"code-point-at": "^1.0.0", "code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0", "is-fullwidth-code-point": "^1.0.0",

View File

@ -8,6 +8,10 @@
padding: 1rem; padding: 1rem;
border-bottom: 5px solid #151515; border-bottom: 5px solid #151515;
border-radius: 3px 3px 0 0; border-radius: 3px 3px 0 0;
&.disabled {
cursor: not-allowed;
}
} }
} }