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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.
var client = require('../utils/client.jsx');
var Constants = require('../utils/constants.jsx');
var ChannelStore = require('../stores/channel_store.jsx');
var utils = require('../utils/utils.jsx');
module.exports = React.createClass({
displayName: 'FileUpload',
propTypes: {
onUploadError: React.PropTypes.func,
getFileCount: React.PropTypes.func,
onFileUpload: React.PropTypes.func,
onUploadStart: React.PropTypes.func
},
getInitialState: function() {
return {requests: {}};
},
handleChange: function() {
var element = $(this.refs.fileInput.getDOMNode());
var files = element.prop('files');
var channelId = ChannelStore.getCurrentId();
this.props.onUploadError(null);
// This looks redundant, but must be done this way due to
// setState being an asynchronous call
var numFiles = 0;
for (var i = 0; i < files.length; i++) {
if (files[i].size <= Constants.MAX_FILE_SIZE) {
numFiles++;
}
}
var numToUpload = Math.min(Constants.MAX_UPLOAD_FILES - this.props.getFileCount(channelId), numFiles);
if (numFiles > numToUpload) {
this.props.onUploadError('Uploads limited to ' + Constants.MAX_UPLOAD_FILES + ' files maximum. Please use additional posts for more files.');
}
for (var i = 0; i < files.length && i < numToUpload; i++) {
if (files[i].size > Constants.MAX_FILE_SIZE) {
this.props.onUploadError('Files must be no more than ' + Constants.MAX_FILE_SIZE / 1000000 + ' MB');
continue;
}
// generate a unique id that can be used by other components to refer back to this file upload
var clientId = utils.generateId();
// Prepare data to be uploaded.
var formData = new FormData();
formData.append('channel_id', channelId);
formData.append('files', files[i], files[i].name);
formData.append('client_ids', clientId);
var request = client.uploadFile(formData,
function(data) {
var parsedData = $.parseJSON(data);
this.props.onFileUpload(parsedData.filenames, parsedData.client_ids, channelId);
var requests = this.state.requests;
for (var i = 0; i < parsedData.client_ids.length; i++) {
delete requests[parsedData.client_ids[i]];
}
this.setState({requests: requests});
}.bind(this),
function(err) {
this.props.onUploadError(err, clientId);
}.bind(this)
);
var requests = this.state.requests;
requests[clientId] = request;
this.setState({requests: requests});
this.props.onUploadStart([clientId], channelId);
}
// clear file input for all modern browsers
try {
element[0].value = '';
if (element.value) {
element[0].type = 'text';
element[0].type = 'file';
}
} catch(e) {}
},
componentDidMount: function() {
var inputDiv = this.refs.input.getDOMNode();
var self = this;
document.addEventListener('paste', function(e) {
var textarea = $(inputDiv.parentNode.parentNode).find('.custom-textarea')[0];
if (textarea !== e.target && !$.contains(textarea, e.target)) {
return;
}
self.props.onUploadError(null);
// This looks redundant, but must be done this way due to
// setState being an asynchronous call
var items = e.clipboardData.items;
var numItems = 0;
if (items) {
for (var i = 0; i < items.length; i++) {
if (items[i].type.indexOf('image') !== -1) {
var testExt = items[i].type.split('/')[1].toLowerCase();
if (Constants.IMAGE_TYPES.indexOf(testExt) < 0) {
continue;
}
numItems++;
}
}
var numToUpload = Math.min(Constants.MAX_UPLOAD_FILES - self.props.getFileCount(ChannelStore.getCurrentId()), numItems);
if (numItems > numToUpload) {
self.props.onUploadError('Uploads limited to ' + Constants.MAX_UPLOAD_FILES + ' files maximum. Please use additional posts for more files.');
}
for (var i = 0; i < items.length && i < numToUpload; i++) {
if (items[i].type.indexOf('image') !== -1) {
var file = items[i].getAsFile();
var ext = items[i].type.split('/')[1].toLowerCase();
if (Constants.IMAGE_TYPES.indexOf(ext) < 0) {
continue;
}
var channelId = ChannelStore.getCurrentId();
// generate a unique id that can be used by other components to refer back to this file upload
var clientId = utils.generateId();
var formData = new FormData();
formData.append('channel_id', channelId);
var d = new Date();
var hour;
if (d.getHours() < 10) {
hour = '0' + d.getHours();
} else {
hour = String(d.getHours());
}
var min;
if (d.getMinutes() < 10) {
min = '0' + d.getMinutes();
} else {
min = String(d.getMinutes());
}
var name = 'Image Pasted at ' + d.getFullYear() + '-' + d.getMonth() + '-' + d.getDate() + ' ' + hour + '-' + min + '.' + ext;
formData.append('files', file, name);
formData.append('client_ids', clientId);
var request = client.uploadFile(formData,
function(data) {
var parsedData = $.parseJSON(data);
self.props.onFileUpload(parsedData.filenames, parsedData.client_ids, channelId);
var requests = self.state.requests;
for (var i = 0; i < parsedData.client_ids.length; i++) {
delete requests[parsedData.client_ids[i]];
}
self.setState({requests: requests});
},
function(err) {
self.props.onUploadError(err, clientId);
}
);
var requests = self.state.requests;
requests[clientId] = request;
self.setState({requests: requests});
self.props.onUploadStart([clientId], channelId);
}
}
}
});
},
cancelUpload: function(clientId) {
var requests = this.state.requests;
var request = requests[clientId];
if (request) {
request.abort();
delete requests[clientId];
this.setState({requests: requests});
}
},
render: function() {
return (
<span ref='input' className='btn btn-file'>
<span>
<i className='glyphicon glyphicon-paperclip' />
</span>
<input ref='fileInput' type='file' onChange={this.handleChange} multiple/>
</span>
);
}
});
|