forked from odysseyscience/react-s3-uploader
-
Notifications
You must be signed in to change notification settings - Fork 15
/
ReactS3Uploader.js
143 lines (125 loc) · 4.49 KB
/
ReactS3Uploader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"use strict";
var React = require('react'),
ReactDOM = require('react-dom'),
PropTypes = require('prop-types'),
createReactClass = require('create-react-class'),
S3Upload = require('./s3upload.js'),
objectAssign = require('object-assign');
var ReactS3Uploader = createReactClass({
propTypes: {
signingUrl: PropTypes.string,
getSignedUrl: PropTypes.func,
preprocess: PropTypes.func,
onProgress: PropTypes.func,
onFinish: PropTypes.func,
onError: PropTypes.func,
signingUrlMethod: PropTypes.string,
signingUrlHeaders: PropTypes.oneOfType([
PropTypes.object,
PropTypes.func
]),
signingUrlQueryParams: PropTypes.oneOfType([
PropTypes.object,
PropTypes.func
]),
signingUrlWithCredentials: PropTypes.bool,
uploadRequestHeaders: PropTypes.object,
contentDisposition: PropTypes.string,
server: PropTypes.string,
scrubFilename: PropTypes.func,
s3path: PropTypes.string,
inputRef: PropTypes.func,
autoUpload: PropTypes.bool,
evaporateOptions: PropTypes.object.isRequired,
},
getDefaultProps: function() {
return {
preprocess: function(file, next) {
console.log('Pre-process: ' + file.name);
next(file);
},
onProgress: function(percent, message) {
console.log('Upload progress: ' + percent + '% ' + message);
},
onFinish: function(signResult) {
console.log("Upload finished: " + signResult.publicUrl)
},
onError: function(message) {
console.log("Upload error: " + message);
},
server: '',
signingUrlMethod: 'GET',
scrubFilename: function(filename) {
return filename.replace(/[^\w\d_\-\.]+/ig, '');
},
s3path: '',
autoUpload: true
};
},
uploadFile: function() {
this.myUploader = new S3Upload({
evaporateOptions: this.props.evaporateOptions,
fileElement: ReactDOM.findDOMNode(this),
signingUrl: this.props.signingUrl,
getSignedUrl: this.props.getSignedUrl,
preprocess: this.props.preprocess,
onProgress: this.props.onProgress,
onFinishS3Put: this.props.onFinish,
onError: this.props.onError,
signingUrlMethod: this.props.signingUrlMethod,
signingUrlHeaders: this.props.signingUrlHeaders,
signingUrlQueryParams: this.props.signingUrlQueryParams,
signingUrlWithCredentials: this.props.signingUrlWithCredentials,
uploadRequestHeaders: this.props.uploadRequestHeaders,
contentDisposition: this.props.contentDisposition,
server: this.props.server,
scrubFilename: this.props.scrubFilename,
s3Path: this.props.s3path
});
},
abort: function() {
this.myUploader && this.myUploader.abortUpload();
},
clear: function() {
clearInputFile(ReactDOM.findDOMNode(this));
},
render: function() {
return React.createElement('input', this.getInputProps());
},
getInputProps: function() {
// declare ref beforehand and filter out
// `inputRef` by `ReactS3Uploader.propTypes`
var additional = {
type: 'file',
ref: this.props.inputRef
};
if ( this.props.autoUpload ) {
additional.onChange = this.uploadFile;
}
var temporaryProps = objectAssign({}, this.props, additional);
var inputProps = {};
var invalidProps = Object.keys(ReactS3Uploader.propTypes);
for(var key in temporaryProps) {
if(temporaryProps.hasOwnProperty(key) && invalidProps.indexOf(key) === -1) {
inputProps[key] = temporaryProps[key];
}
}
return inputProps;
}
});
// http://stackoverflow.com/a/24608023/194065
function clearInputFile(f){
if(f.value){
try{
f.value = ''; //for IE11, latest Chrome/Firefox/Opera...
}catch(err){ }
if(f.value){ //for IE5 ~ IE10
var form = document.createElement('form'),
parentNode = f.parentNode, ref = f.nextSibling;
form.appendChild(f);
form.reset();
parentNode.insertBefore(f,ref);
}
}
}
module.exports = ReactS3Uploader;