/
HTML5UploaderSupport.js
471 lines (395 loc) · 17.4 KB
/
HTML5UploaderSupport.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
Copyright 2010-2011 OCAD University
Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Licenses.
You may obtain a copy of the ECL 2.0 License and BSD License at
https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
*/
// Declare dependencies
/*global FormData, fluid_1_4:true, jQuery*/
// JSLint options
/*jslint white: true, funcinvoke: true, undef: true, newcap: true, nomen: true, regexp: true, bitwise: true, browser: true, forin: true, maxerr: 100, indent: 4 */
var fluid_1_4 = fluid_1_4 || {};
(function ($, fluid) {
fluid.demands("fluid.uploaderImpl", "fluid.uploader.html5", {
funcName: "fluid.uploader.multiFileUploader"
});
fluid.demands("fluid.uploader.progressiveStrategy", "fluid.uploader.html5", {
funcName: "fluid.uploader.html5Strategy"
});
fluid.defaults("fluid.uploader.html5Strategy", {
gradeNames: ["fluid.littleComponent", "autoInit"],
components: {
local: {
type: "fluid.uploader.local",
options: {
queueSettings: "{multiFileUploader}.options.queueSettings",
events: {
onFileDialog: "{multiFileUploader}.events.onFileDialog",
afterFileDialog: "{multiFileUploader}.events.afterFileDialog",
afterFileQueued: "{multiFileUploader}.events.afterFileQueued",
onQueueError: "{multiFileUploader}.events.onQueueError"
}
}
},
remote: {
type: "fluid.uploader.remote",
options: {
queueSettings: "{multiFileUploader}.options.queueSettings",
events: {
afterReady: "{multiFileUploader}.events.afterReady",
onFileStart: "{multiFileUploader}.events.onFileStart",
onFileProgress: "{multiFileUploader}.events.onFileProgress",
onFileSuccess: "{multiFileUploader}.events.onFileSuccess",
onFileError: "{multiFileUploader}.events.onFileError",
onFileComplete: "{multiFileUploader}.events.onFileComplete"
}
}
}
},
// Used for browsers that rely on File.getAsBinary(), such as Firefox 3.6,
// which load the entire file to be loaded into memory.
// Set this option to a sane limit (100MB) so your users won't experience crashes or slowdowns (FLUID-3937).
legacyBrowserFileLimit: 100000
});
// TODO: The following two or three functions probably ultimately belong on a that responsible for
// coordinating with the XHR. A fileConnection object or something similar.
fluid.uploader.html5Strategy.fileSuccessHandler = function (file, events, xhr) {
events.onFileSuccess.fire(file, xhr.responseText, xhr);
events.onFileComplete.fire(file);
};
fluid.uploader.html5Strategy.fileErrorHandler = function (file, events, xhr) {
events.onFileError.fire(file,
fluid.uploader.errorConstants.UPLOAD_FAILED,
xhr.status,
xhr);
events.onFileComplete.fire(file);
};
fluid.uploader.html5Strategy.fileStopHandler = function (file, events, xhr) {
events.onFileError.fire(file,
fluid.uploader.errorConstants.UPLOAD_STOPPED,
xhr.status,
xhr);
events.onFileComplete.fire(file);
};
fluid.uploader.html5Strategy.progressTracker = function () {
var that = {
previousBytesLoaded: 0
};
that.getChunkSize = function (bytesLoaded) {
var chunkSize = bytesLoaded - that.previousBytesLoaded;
that.previousBytesLoaded = bytesLoaded;
return chunkSize;
};
return that;
};
fluid.uploader.html5Strategy.monitorFileUploadXHR = function (file, events, xhr) {
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
var status = xhr.status;
// TODO: See a pattern here? Fix it.
if (status === 200) {
fluid.uploader.html5Strategy.fileSuccessHandler(file, events, xhr);
} else if (status === 0) {
fluid.uploader.html5Strategy.fileStopHandler(file, events, xhr);
} else {
fluid.uploader.html5Strategy.fileErrorHandler(file, events, xhr);
}
}
};
var progressTracker = fluid.uploader.html5Strategy.progressTracker();
xhr.upload.onprogress = function (pe) {
events.onFileProgress.fire(file, progressTracker.getChunkSize(pe.loaded), pe.total);
};
};
/*************************************
* HTML5 Strategy's remote behaviour *
*************************************/
fluid.uploader.html5Strategy.remote = function (queue, options) {
var that = fluid.initLittleComponent("fluid.uploader.html5Strategy.remote", options);
that.queue = queue;
that.queueSettings = that.options.queueSettings;
// Upload files in the current batch without exceeding the fileUploadLimit
that.uploadNextFile = function () {
var batch = that.queue.currentBatch;
var file = batch.files[batch.fileIdx];
that.uploadFile(file);
};
that.uploadFile = function (file) {
that.events.onFileStart.fire(file);
that.currentXHR = that.createXHR();
fluid.uploader.html5Strategy.monitorFileUploadXHR(file, that.events, that.currentXHR);
that.fileSender.send(file, that.queueSettings, that.currentXHR);
};
that.stop = function () {
that.queue.isUploading = false;
that.currentXHR.abort();
};
fluid.initDependents(that);
that.events.afterReady.fire();
return that;
};
fluid.defaults("fluid.uploader.html5Strategy.remote", {
gradeNames: ["fluid.eventedComponent"],
argumentMap: {
options: 1
},
components: {
fileSender: {
type: "fluid.uploader.html5Strategy.fileSender"
}
},
invokers: {
createXHR: "fluid.uploader.html5Strategy.createXHR"
}
});
fluid.demands("fluid.uploader.remote", ["fluid.uploader.html5Strategy", "fluid.uploader.live"], {
funcName: "fluid.uploader.html5Strategy.remote",
args: [
"{multiFileUploader}.queue",
fluid.COMPONENT_OPTIONS
]
});
fluid.uploader.html5Strategy.createXHR = function () {
return new XMLHttpRequest();
};
fluid.uploader.html5Strategy.createFormData = function () {
return new FormData();
};
// Set additional POST parameters for xhr
var setPostParams = function (formData, postParams) {
$.each(postParams, function (key, value) {
formData.append(key, value);
});
};
/*******************************************************
* HTML5 FormData Sender, used by most modern browsers *
*******************************************************/
fluid.defaults("fluid.uploader.html5Strategy.formDataSender", {
gradeNames: ["fluid.littleComponent", "autoInit"],
finalInitFunction: "fluid.uploader.html5Strategy.formDataSender.init",
invokers: {
createFormData: "fluid.uploader.html5Strategy.createFormData"
}
});
fluid.uploader.html5Strategy.formDataSender.init = function (that) {
/**
* Uploads the file using the HTML5 FormData object.
*/
that.send = function (file, queueSettings, xhr) {
var formData = that.createFormData();
formData.append("file", file);
setPostParams(formData, queueSettings.postParams);
xhr.open("POST", queueSettings.uploadURL, true);
xhr.send(formData);
return formData;
};
};
fluid.demands("fluid.uploader.html5Strategy.fileSender", [
"fluid.uploader.html5Strategy.remote",
"fluid.browser.supportsFormData"
], {
funcName: "fluid.uploader.html5Strategy.formDataSender"
});
/********************************************
* Raw MIME Sender, required by Firefox 3.6 *
********************************************/
fluid.uploader.html5Strategy.generateMultipartBoundary = function () {
var boundary = "---------------------------";
boundary += Math.floor(Math.random() * 32768);
boundary += Math.floor(Math.random() * 32768);
boundary += Math.floor(Math.random() * 32768);
return boundary;
};
fluid.uploader.html5Strategy.generateMultiPartContent = function (boundary, file) {
var CRLF = "\r\n";
var multipart = "";
multipart += "--" + boundary + CRLF;
multipart += "Content-Disposition: form-data;" +
" name=\"fileData\";" +
" filename=\"" + file.name +
"\"" + CRLF;
multipart += "Content-Type: " + file.type + CRLF + CRLF;
multipart += file.getAsBinary(); // Concatting binary data to JS String; yes, FF will handle it.
multipart += CRLF + "--" + boundary + "--" + CRLF;
return multipart;
};
fluid.defaults("fluid.uploader.html5Strategy.rawMIMESender", {
gradeNames: ["fluid.littleComponent", "autoInit"],
finalInitFunction: "fluid.uploader.html5Strategy.rawMIMESender.init"
});
fluid.uploader.html5Strategy.rawMIMESender.init = function (that) {
/**
* Uploads the file by manually creating the multipart/form-data request. Required by Firefox 3.6.
*/
that.send = function (file, queueSettings, xhr) {
var boundary = fluid.uploader.html5Strategy.generateMultipartBoundary();
var multipart = fluid.uploader.html5Strategy.generateMultiPartContent(boundary, file);
xhr.open("POST", queueSettings.uploadURL, true);
xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
xhr.sendAsBinary(multipart);
return multipart;
};
};
fluid.demands("fluid.uploader.html5Strategy.fileSender", "fluid.uploader.html5Strategy.remote", {
funcName: "fluid.uploader.html5Strategy.rawMIMESender"
});
/************************************
* HTML5 Strategy's Local Behaviour *
************************************/
fluid.uploader.html5Strategy.local = function (queue, legacyBrowserFileLimit, options) {
var that = fluid.initLittleComponent("fluid.uploader.html5Strategy.local", options);
that.queue = queue;
that.queueSettings = that.options.queueSettings;
// Add files to the file queue without exceeding the fileUploadLimit and the fileSizeLimit
// NOTE: fileSizeLimit set to bytes for HTML5 Uploader (KB for SWF Uploader).
that.addFiles = function (files) {
// TODO: These look like they should be part of a real model.
var sizeLimit = (legacyBrowserFileLimit || that.queueSettings.fileSizeLimit) * 1024;
var fileLimit = that.queueSettings.fileUploadLimit;
var uploaded = that.queue.getUploadedFiles().length;
var queued = that.queue.getReadyFiles().length;
var remainingUploadLimit = fileLimit - uploaded - queued;
// TODO: Provide feedback to the user if the file size is too large and isn't added to the file queue
var numFilesAdded = 0;
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file.size < sizeLimit && (!fileLimit || remainingUploadLimit > 0)) {
file.id = "file-" + fluid.allocateGuid();
file.filestatus = fluid.uploader.fileStatusConstants.QUEUED;
that.events.afterFileQueued.fire(file);
remainingUploadLimit--;
numFilesAdded++;
} else {
file.filestatus = fluid.uploader.fileStatusConstants.ERROR;
that.events.onQueueError.fire(file, fluid.uploader.errorConstants.UPLOAD_LIMIT_EXCEEDED);
}
}
that.events.afterFileDialog.fire(numFilesAdded);
};
that.removeFile = function (file) {
};
that.enableBrowseButton = function () {
that.browseButtonView.enable();
};
that.disableBrowseButton = function () {
that.browseButtonView.disable();
};
fluid.initDependents(that);
return that;
};
fluid.defaults("fluid.uploader.html5Strategy.local", {
argumentMap: {
options: 2
},
gradeNames: ["fluid.eventedComponent"],
components: {
browseButtonView: {
type: "fluid.uploader.html5Strategy.browseButtonView",
options: {
queueSettings: "{multiFileUploader}.options.queueSettings",
selectors: {
browseButton: "{multiFileUploader}.selectors.browseButton"
},
listeners: {
onFilesQueued: "{local}.addFiles"
}
}
}
}
});
fluid.demands("fluid.uploader.local", "fluid.uploader.html5Strategy", {
funcName: "fluid.uploader.html5Strategy.local",
args: [
"{multiFileUploader}.queue",
"{html5Strategy}.options.legacyBrowserFileLimit",
"{options}"
]
});
fluid.demands("fluid.uploader.local", [
"fluid.uploader.html5Strategy",
"fluid.browser.supportsFormData"
], {
funcName: "fluid.uploader.html5Strategy.local",
args: [
"{multiFileUploader}.queue",
undefined,
"{options}"
]
});
/********************
* browseButtonView *
********************/
var bindEventsToFileInput = function (that, fileInput) {
fileInput.click(function () {
that.events.onBrowse.fire();
});
fileInput.change(function () {
var files = fileInput[0].files;
that.events.onFilesQueued.fire(files);
that.renderFreshMultiFileInput();
});
fileInput.focus(function () {
that.browseButton.addClass("focus");
});
fileInput.blur(function () {
that.browseButton.removeClass("focus");
});
};
var renderMultiFileInput = function (that) {
var multiFileInput = $(that.options.multiFileInputMarkup);
var fileTypes = that.options.queueSettings.fileTypes;
if (fluid.isArrayable(fileTypes)) {
fileTypes = fileTypes.join();
multiFileInput.attr("accept", fileTypes);
}
bindEventsToFileInput(that, multiFileInput);
return multiFileInput;
};
var setupBrowseButtonView = function (that) {
var multiFileInput = renderMultiFileInput(that);
that.browseButton.append(multiFileInput);
that.browseButton.attr("tabindex", -1);
};
fluid.uploader.html5Strategy.browseButtonView = function (container, options) {
var that = fluid.initView("fluid.uploader.html5Strategy.browseButtonView", container, options);
that.browseButton = that.locate("browseButton");
that.renderFreshMultiFileInput = function () {
var previousInput = that.locate("fileInputs").last();
previousInput.hide();
previousInput.attr("tabindex", -1);
var newInput = renderMultiFileInput(that);
previousInput.after(newInput);
};
that.enable = function () {
that.locate("fileInputs").prop("disabled", false);
};
that.disable = function () {
that.locate("fileInputs").prop("disabled", true);
};
setupBrowseButtonView(that);
return that;
};
fluid.defaults("fluid.uploader.html5Strategy.browseButtonView", {
gradeNames: "fluid.viewComponent",
multiFileInputMarkup: "<input type='file' multiple='' class='flc-uploader-html5-input' />",
queueSettings: {},
selectors: {
browseButton: ".flc-uploader-button-browse",
fileInputs: ".flc-uploader-html5-input"
},
events: {
onBrowse: null,
onFilesQueued: null
}
});
fluid.demands("fluid.uploader.html5Strategy.browseButtonView", "fluid.uploader.html5Strategy.local", {
container: "{multiFileUploader}.container",
mergeOptions: {
events: {
onBrowse: "{local}.events.onFileDialog"
}
}
});
})(jQuery, fluid_1_4);