forked from odysseyscience/react-s3-uploader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReactS3Uploader.js
129 lines (113 loc) · 4.07 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
"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,
getType: 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
},
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);
},
getType: function(file) {
return file.type;
},
server: '',
signingUrlMethod: 'GET',
scrubFilename: function(filename) {
return filename.replace(/[^\w\d_\-\.]+/ig, '');
}
};
},
uploadFile: function() {
this.myUploader = new S3Upload({
fileElement: ReactDOM.findDOMNode(this),
signingUrl: this.props.signingUrl,
getSignedUrl: this.props.getSignedUrl,
preprocess: this.props.preprocess,
getType: this.props.getType,
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
});
},
abort: function() {
this.myUploader && this.myUploader.abortUpload();
},
clear: function() {
clearInputFile(ReactDOM.findDOMNode(this));
},
render: function() {
return React.createElement('input', this.getInputProps());
},
getInputProps: function() {
var temporaryProps = objectAssign({}, this.props, {type: 'file', onChange: this.uploadFile});
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;