Initial work for file sharing using WebTorrent
parent
4f8c896a43
commit
7efaf092c8
|
|
@ -4,6 +4,7 @@ import PropTypes from 'prop-types';
|
||||||
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 MessageList from './MessageList';
|
import MessageList from './MessageList';
|
||||||
|
import FileSharing from './FileSharing';
|
||||||
|
|
||||||
class Chat extends Component
|
class Chat extends Component
|
||||||
{
|
{
|
||||||
|
|
@ -21,6 +22,9 @@ class Chat extends Component
|
||||||
return (
|
return (
|
||||||
<div data-component='Chat'>
|
<div data-component='Chat'>
|
||||||
<MessageList />
|
<MessageList />
|
||||||
|
|
||||||
|
<FileSharing />
|
||||||
|
|
||||||
<form
|
<form
|
||||||
data-component='Sender'
|
data-component='Sender'
|
||||||
onSubmit={(e) => { onSendMessage(e, displayName, picture); }}
|
onSubmit={(e) => { onSendMessage(e, displayName, picture); }}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,61 @@
|
||||||
|
import React, { Component } from 'react';
|
||||||
|
import { connect } from 'react-redux';
|
||||||
|
import WebTorrent from 'webtorrent';
|
||||||
|
import dragDrop from 'drag-drop';
|
||||||
|
import * as stateActions from '../../redux/stateActions';
|
||||||
|
import * as requestActions from '../../redux/requestActions';
|
||||||
|
|
||||||
|
class FileSharing extends Component {
|
||||||
|
notifyPeers = (file) =>
|
||||||
|
{
|
||||||
|
this.props.notifyPeers(
|
||||||
|
file,
|
||||||
|
this.props.displayName,
|
||||||
|
this.props.picture
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
componentDidMount()
|
||||||
|
{
|
||||||
|
this.client = new WebTorrent();
|
||||||
|
|
||||||
|
dragDrop('body', (files) =>
|
||||||
|
{
|
||||||
|
this.client.seed(files, (torrent) => {
|
||||||
|
this.notifyPeers({
|
||||||
|
magnet: torrent.magnetURI
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
render()
|
||||||
|
{
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
drag & drop files to share them!!!
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const mapStateToProps = (state) =>
|
||||||
|
({
|
||||||
|
displayName: state.me.displayName,
|
||||||
|
picture: state.me.picture
|
||||||
|
});
|
||||||
|
|
||||||
|
const mapDispatchToProps = (dispatch) =>
|
||||||
|
({
|
||||||
|
notifyPeers: (file, displayName, picture) =>
|
||||||
|
{
|
||||||
|
console.log(file)
|
||||||
|
dispatch(stateActions.addUserFile(file));
|
||||||
|
dispatch(requestActions.sendChatFile(file, displayName, picture));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default connect(
|
||||||
|
mapStateToProps,
|
||||||
|
mapDispatchToProps
|
||||||
|
)(FileSharing);
|
||||||
|
|
@ -48,6 +48,8 @@ class MessageList extends Component
|
||||||
{
|
{
|
||||||
chatmessages.map((message, i) =>
|
chatmessages.map((message, i) =>
|
||||||
{
|
{
|
||||||
|
console.log(message);
|
||||||
|
|
||||||
const messageTime = new Date(message.time);
|
const messageTime = new Date(message.time);
|
||||||
|
|
||||||
const picture = (message.sender === 'response' ?
|
const picture = (message.sender === 'response' ?
|
||||||
|
|
@ -59,19 +61,27 @@ class MessageList extends Component
|
||||||
<img className='message-avatar' src={picture} />
|
<img className='message-avatar' src={picture} />
|
||||||
|
|
||||||
<div className='message-content'>
|
<div className='message-content'>
|
||||||
<div
|
{message.type === 'message' && (
|
||||||
className='message-text'
|
<div
|
||||||
// eslint-disable-next-line react/no-danger
|
className='message-text'
|
||||||
dangerouslySetInnerHTML={{ __html : marked.parse(
|
// eslint-disable-next-line react/no-danger
|
||||||
message.text,
|
dangerouslySetInnerHTML={{ __html : marked.parse(
|
||||||
{ sanitize: true, renderer: linkRenderer }
|
message.text,
|
||||||
) }}
|
{ sanitize: true, renderer: linkRenderer }
|
||||||
/>
|
) }}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
<span className='message-time'>
|
{message.type === 'file' && (
|
||||||
{message.name} - {this.getTimeString(messageTime)}
|
<div>
|
||||||
</span>
|
{message.file.magnet}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<span className='message-time'>
|
||||||
|
{message.name} - {this.getTimeString(messageTime)}
|
||||||
|
</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import
|
import
|
||||||
{
|
{
|
||||||
createNewMessage
|
createNewMessage,
|
||||||
|
createNewFile
|
||||||
} from './helper';
|
} from './helper';
|
||||||
|
|
||||||
const chatmessages = (state = [], action) =>
|
const chatmessages = (state = [], action) =>
|
||||||
|
|
@ -11,7 +12,16 @@ const chatmessages = (state = [], action) =>
|
||||||
{
|
{
|
||||||
const { text } = action.payload;
|
const { text } = action.payload;
|
||||||
|
|
||||||
const message = createNewMessage(text, 'client', 'Me');
|
const message = createNewMessage(text, 'client', 'Me', undefined);
|
||||||
|
|
||||||
|
return [ ...state, message ];
|
||||||
|
}
|
||||||
|
|
||||||
|
case 'ADD_NEW_USER_FILE':
|
||||||
|
{
|
||||||
|
const { file } = action.payload;
|
||||||
|
|
||||||
|
const message = createNewFile(file, 'client', 'Me', undefined);
|
||||||
|
|
||||||
return [ ...state, message ];
|
return [ ...state, message ];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,3 +9,15 @@ export function createNewMessage(text, sender, name, picture)
|
||||||
picture
|
picture
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function createNewFile(file, sender, name, picture)
|
||||||
|
{
|
||||||
|
return {
|
||||||
|
type: 'file',
|
||||||
|
file,
|
||||||
|
time: Date.now(),
|
||||||
|
name,
|
||||||
|
sender,
|
||||||
|
picture
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -2,7 +2,8 @@ import randomString from 'random-string';
|
||||||
import * as stateActions from './stateActions';
|
import * as stateActions from './stateActions';
|
||||||
import
|
import
|
||||||
{
|
{
|
||||||
createNewMessage
|
createNewMessage,
|
||||||
|
createNewFile
|
||||||
} from './reducers/helper';
|
} from './reducers/helper';
|
||||||
|
|
||||||
export const joinRoom = (
|
export const joinRoom = (
|
||||||
|
|
@ -213,6 +214,16 @@ export const sendChatMessage = (text, name, picture) =>
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const sendChatFile = (magnet, name, picture) =>
|
||||||
|
{
|
||||||
|
const message = createNewFile(magnet, 'response', name, picture);
|
||||||
|
|
||||||
|
return {
|
||||||
|
type: 'SEND_CHAT_MESSAGE',
|
||||||
|
payload: { message }
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
// This returns a redux-thunk action (a function).
|
// This returns a redux-thunk action (a function).
|
||||||
export const notify = ({ type = 'info', text, timeout }) =>
|
export const notify = ({ type = 'info', text, timeout }) =>
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -410,6 +410,14 @@ export const addUserMessage = (text) =>
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const addUserFile = (file) =>
|
||||||
|
{
|
||||||
|
return {
|
||||||
|
type: 'ADD_NEW_USER_FILE',
|
||||||
|
payload: { file }
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export const addResponseMessage = (message) =>
|
export const addResponseMessage = (message) =>
|
||||||
{
|
{
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -11,6 +11,7 @@
|
||||||
"classnames": "^2.2.6",
|
"classnames": "^2.2.6",
|
||||||
"debug": "^3.1.0",
|
"debug": "^3.1.0",
|
||||||
"domready": "^1.0.8",
|
"domready": "^1.0.8",
|
||||||
|
"drag-drop": "^4.2.0",
|
||||||
"fscreen": "^1.0.2",
|
"fscreen": "^1.0.2",
|
||||||
"hark": "^1.2.2",
|
"hark": "^1.2.2",
|
||||||
"js-cookie": "^2.2.0",
|
"js-cookie": "^2.2.0",
|
||||||
|
|
@ -33,7 +34,8 @@
|
||||||
"redux-thunk": "^2.3.0",
|
"redux-thunk": "^2.3.0",
|
||||||
"resize-observer-polyfill": "^1.5.0",
|
"resize-observer-polyfill": "^1.5.0",
|
||||||
"riek": "^1.1.0",
|
"riek": "^1.1.0",
|
||||||
"url-parse": "^1.4.1"
|
"url-parse": "^1.4.1",
|
||||||
|
"webtorrent": "^0.101.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"babel-core": "^6.26.3",
|
"babel-core": "^6.26.3",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue