Add notification when Blob converting fails
parent
734bb1eb0a
commit
088f3ccc7c
|
|
@ -1,90 +1,102 @@
|
||||||
import React, { Component, Fragment } from 'react';
|
import React, { Component, Fragment } from 'react';
|
||||||
|
import { connect } from 'react-redux';
|
||||||
|
import { notifyAction } from '../../redux/stateActions';
|
||||||
import { saveAs } from 'file-saver/FileSaver';
|
import { saveAs } from 'file-saver/FileSaver';
|
||||||
import { client } from './FileSharing';
|
import { client } from './FileSharing';
|
||||||
|
|
||||||
const saveFile = (file) =>
|
|
||||||
{
|
|
||||||
file.getBlob((err, blob) =>
|
|
||||||
{
|
|
||||||
if (err)
|
|
||||||
{
|
|
||||||
console.error('WebTorrent error');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('TRYING TO SAVE BLOB', blob)
|
|
||||||
saveAs(blob, file.name);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
class FileChatEntry extends Component
|
class FileChatEntry extends Component
|
||||||
{
|
{
|
||||||
state = {
|
state = {
|
||||||
active: false,
|
active : false,
|
||||||
numPeers: 0,
|
numPeers : 0,
|
||||||
progress: 0,
|
progress : 0,
|
||||||
files: null
|
files : null
|
||||||
};
|
};
|
||||||
|
|
||||||
download = () =>
|
saveFile = (file) =>
|
||||||
{
|
{
|
||||||
this.setState({
|
file.getBlob((err, blob) =>
|
||||||
active: true
|
{
|
||||||
});
|
if (err)
|
||||||
|
{
|
||||||
|
return this.props.notify({
|
||||||
|
text : 'An error occurred while saving a file'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
saveAs(blob, file.name);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
client.add(this.props.message.file.magnet, (torrent) =>
|
download = () =>
|
||||||
{
|
{
|
||||||
const onProgress = () =>
|
this.setState({
|
||||||
{
|
active : true
|
||||||
this.setState({
|
});
|
||||||
numPeers: torrent.numPeers,
|
|
||||||
progress: Math.round(torrent.progress * 100 * 100) / 100
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
setInterval(onProgress, 500);
|
client.add(this.props.message.file.magnet, (torrent) =>
|
||||||
onProgress();
|
{
|
||||||
|
const onProgress = () =>
|
||||||
|
{
|
||||||
|
this.setState({
|
||||||
|
numPeers : torrent.numPeers,
|
||||||
|
progress : Math.round(torrent.progress * 100 * 100) / 100
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
torrent.on('done', () => {
|
setInterval(onProgress, 500);
|
||||||
onProgress();
|
onProgress();
|
||||||
clearInterval(onProgress);
|
|
||||||
|
|
||||||
this.setState({
|
torrent.on('done', () =>
|
||||||
files: torrent.files
|
{
|
||||||
});
|
onProgress();
|
||||||
});
|
clearInterval(onProgress);
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
render()
|
this.setState({
|
||||||
{
|
files : torrent.files
|
||||||
return (
|
});
|
||||||
<Fragment>
|
});
|
||||||
<div>
|
});
|
||||||
<button onClick={this.download}>
|
}
|
||||||
append shared file to body
|
|
||||||
</button>
|
|
||||||
|
|
||||||
{this.state.active && (
|
render()
|
||||||
<div>
|
{
|
||||||
peers: {this.state.numPeers}
|
return (
|
||||||
progress: {this.state.progress}
|
<Fragment>
|
||||||
</div>
|
<div>
|
||||||
)}
|
<button onClick={this.download}>
|
||||||
|
append shared file to body
|
||||||
|
</button>
|
||||||
|
|
||||||
{this.state.files && (
|
{this.state.active && (
|
||||||
<div>
|
<div>
|
||||||
{this.state.files.map((file, i) => (
|
peers: {this.state.numPeers}
|
||||||
<div key={i}>
|
progress: {this.state.progress}
|
||||||
<button onClick={() => saveFile(file)}>download {file.name}</button>
|
</div>
|
||||||
</div>
|
)}
|
||||||
))}
|
|
||||||
</div>
|
{this.state.files && (
|
||||||
)}
|
<div>
|
||||||
</div>
|
{this.state.files.map((file, i) => (
|
||||||
</Fragment>
|
<div key={i}>
|
||||||
);
|
<button onClick={() => this.saveFile(file)}>
|
||||||
}
|
download {file.name}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</Fragment>
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default FileChatEntry;
|
const mapDispatchToProps = {
|
||||||
|
notify : notifyAction
|
||||||
|
};
|
||||||
|
|
||||||
|
export default connect(
|
||||||
|
undefined,
|
||||||
|
mapDispatchToProps
|
||||||
|
)(FileChatEntry);
|
||||||
|
|
@ -1,51 +1,53 @@
|
||||||
import React, { Component } from 'react';
|
import React, { Component } from 'react';
|
||||||
import { connect } from 'react-redux';
|
|
||||||
import WebTorrent from 'webtorrent';
|
import WebTorrent from 'webtorrent';
|
||||||
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';
|
||||||
|
|
||||||
export const client = new WebTorrent();
|
export const client = new WebTorrent();
|
||||||
|
|
||||||
const notifyPeers = (file) =>
|
const notifyPeers = (file) =>
|
||||||
{
|
{
|
||||||
const { displayName, picture } = store.getState().me;
|
const { displayName, picture } = store.getState().me;
|
||||||
store.dispatch(stateActions.addUserFile(file));
|
|
||||||
store.dispatch(requestActions.sendChatFile(file, displayName, picture));
|
store.dispatch(stateActions.addUserFile(file));
|
||||||
|
store.dispatch(requestActions.sendChatFile(file, displayName, picture));
|
||||||
};
|
};
|
||||||
|
|
||||||
const shareFiles = (files) =>
|
const shareFiles = (files) =>
|
||||||
{
|
{
|
||||||
client.seed(files, (torrent) => {
|
client.seed(files, (torrent) =>
|
||||||
notifyPeers({
|
{
|
||||||
magnet: torrent.magnetURI
|
notifyPeers({
|
||||||
});
|
magnet : torrent.magnetURI
|
||||||
});
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
dragDrop('body', shareFiles);
|
dragDrop('body', shareFiles);
|
||||||
|
|
||||||
class FileSharing extends Component {
|
class FileSharing extends Component
|
||||||
constructor(props)
|
{
|
||||||
{
|
constructor(props)
|
||||||
super(props);
|
{
|
||||||
}
|
super(props);
|
||||||
|
}
|
||||||
|
|
||||||
handleFileChange = (event) =>
|
handleFileChange = (event) =>
|
||||||
{
|
{
|
||||||
if (event.target.files.length > 0)
|
if (event.target.files.length > 0)
|
||||||
{
|
{
|
||||||
shareFiles(event.target.files);
|
shareFiles(event.target.files);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
render()
|
render()
|
||||||
{
|
{
|
||||||
return (
|
return (
|
||||||
<input type="file" onChange={this.handleFileChange} />
|
<input type='file' onChange={this.handleFileChange} />
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default FileSharing;
|
export default FileSharing;
|
||||||
|
|
@ -13,9 +13,9 @@ export function createNewMessage(text, sender, name, picture)
|
||||||
export function createNewFile(file, sender, name, picture)
|
export function createNewFile(file, sender, name, picture)
|
||||||
{
|
{
|
||||||
return {
|
return {
|
||||||
type: 'file',
|
type : 'file',
|
||||||
file,
|
file,
|
||||||
time: Date.now(),
|
time : Date.now(),
|
||||||
name,
|
name,
|
||||||
sender,
|
sender,
|
||||||
picture
|
picture
|
||||||
|
|
|
||||||
|
|
@ -219,8 +219,8 @@ export const sendChatFile = (magnet, name, picture) =>
|
||||||
const message = createNewFile(magnet, 'response', name, picture);
|
const message = createNewFile(magnet, 'response', name, picture);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
type: 'SEND_CHAT_MESSAGE',
|
type : 'SEND_CHAT_MESSAGE',
|
||||||
payload: { message }
|
payload : { message }
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -413,8 +413,8 @@ export const addUserMessage = (text) =>
|
||||||
export const addUserFile = (file) =>
|
export const addUserFile = (file) =>
|
||||||
{
|
{
|
||||||
return {
|
return {
|
||||||
type: 'ADD_NEW_USER_FILE',
|
type : 'ADD_NEW_USER_FILE',
|
||||||
payload: { file }
|
payload : { file }
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue