2016-07-19 22:49:46 +02:00
|
|
|
import React, { PropTypes } from 'react';
|
2016-07-16 01:05:18 +02:00
|
|
|
import Dropzone from 'dropzone';
|
2016-07-19 22:49:46 +02:00
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
import { connect } from 'react-redux';
|
2017-02-22 20:29:35 +01:00
|
|
|
import * as UploaderActions from '../actions/uploader';
|
|
|
|
|
2017-04-13 20:39:03 +02:00
|
|
|
const s3Bucket = `https://s3-${process.env.AWS_REGION}.amazonaws.com/${process.env.S3_BUCKET}/`;
|
2016-07-16 01:05:18 +02:00
|
|
|
|
|
|
|
class FileUploader extends React.Component {
|
|
|
|
componentDidMount() {
|
2016-07-21 00:37:49 +02:00
|
|
|
this.createDropzone();
|
2016-07-16 02:01:21 +02:00
|
|
|
Dropzone.autoDiscover = false;
|
2016-07-21 00:37:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
createDropzone() {
|
2017-04-06 22:22:34 +02:00
|
|
|
const userId = this.props.project.owner ? this.props.project.owner.id : this.props.user.id;
|
2016-07-16 02:01:21 +02:00
|
|
|
this.uploader = new Dropzone('div#uploader', {
|
2016-07-16 01:05:18 +02:00
|
|
|
url: s3Bucket,
|
|
|
|
method: 'post',
|
|
|
|
autoProcessQueue: true,
|
|
|
|
clickable: true,
|
2016-11-30 01:18:11 +01:00
|
|
|
maxFiles: 6,
|
|
|
|
parallelUploads: 2,
|
2016-10-28 22:19:56 +02:00
|
|
|
maxFilesize: 5, // in mb
|
2016-11-30 01:18:11 +01:00
|
|
|
maxThumbnailFilesize: 8, // 8 mb
|
2016-07-20 03:42:33 +02:00
|
|
|
thumbnailWidth: 200,
|
|
|
|
thumbnailHeight: 200,
|
2016-08-25 06:18:28 +02:00
|
|
|
// TODO what is a good list of MIME types????
|
2016-10-25 00:25:57 +02:00
|
|
|
acceptedFiles: `image/*,audio/*,text/javascript,text/html,text/css,
|
|
|
|
application/json,application/x-font-ttf,application/x-font-truetype,
|
2016-11-02 16:42:59 +01:00
|
|
|
text/plain,text/csv,.obj,video/webm,video/ogg,video/quicktime,video/mp4,
|
|
|
|
.otf,.ttf`,
|
2016-07-20 03:42:33 +02:00
|
|
|
dictDefaultMessage: 'Drop files here to upload or click to use the file browser',
|
2017-04-06 22:22:34 +02:00
|
|
|
accept: this.props.dropzoneAcceptCallback.bind(this, userId),
|
2016-07-19 22:49:46 +02:00
|
|
|
sending: this.props.dropzoneSendingCallback,
|
2017-04-06 20:34:14 +02:00
|
|
|
complete: this.props.dropzoneCompleteCallback
|
2016-10-25 03:15:32 +02:00
|
|
|
// error: (file, errorMessage) => {
|
|
|
|
// console.log(file);
|
|
|
|
// console.log(errorMessage);
|
|
|
|
// }
|
2016-07-16 02:01:21 +02:00
|
|
|
});
|
2016-07-16 01:05:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div id="uploader" className="uploader dropzone"></div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 22:49:46 +02:00
|
|
|
FileUploader.propTypes = {
|
|
|
|
dropzoneAcceptCallback: PropTypes.func.isRequired,
|
|
|
|
dropzoneSendingCallback: PropTypes.func.isRequired,
|
2017-04-06 20:34:14 +02:00
|
|
|
dropzoneCompleteCallback: PropTypes.func.isRequired,
|
|
|
|
project: PropTypes.shape({
|
|
|
|
owner: PropTypes.shape({
|
2017-04-06 22:22:34 +02:00
|
|
|
id: PropTypes.string
|
2017-04-06 20:34:14 +02:00
|
|
|
})
|
2017-04-06 22:22:34 +02:00
|
|
|
}),
|
|
|
|
user: PropTypes.shape({
|
|
|
|
id: PropTypes.string
|
2017-04-06 20:34:14 +02:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
FileUploader.defaultProps = {
|
|
|
|
project: {
|
|
|
|
id: undefined,
|
2017-04-06 22:22:34 +02:00
|
|
|
owner: undefined
|
|
|
|
},
|
|
|
|
user: {
|
|
|
|
id: undefined
|
2017-04-06 20:34:14 +02:00
|
|
|
}
|
2016-07-19 22:49:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function mapStateToProps(state) {
|
|
|
|
return {
|
|
|
|
files: state.files,
|
2017-04-06 22:22:34 +02:00
|
|
|
project: state.project,
|
|
|
|
user: state.user
|
2016-07-19 22:49:46 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapDispatchToProps(dispatch) {
|
|
|
|
return bindActionCreators(UploaderActions, dispatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(FileUploader);
|