forked from jalik/meteor-jalik-ufs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ufs-uploader.js
517 lines (468 loc) · 14.1 KB
/
ufs-uploader.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Karl STEIN
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
import { Meteor } from 'meteor/meteor';
import { _ } from 'meteor/underscore';
import { Store } from './ufs-store';
/**
* File uploader
*/
export class Uploader {
constructor(options) {
let self = this;
// Set default options
options = _.extend({
adaptive: true,
capacity: 0.9,
chunkSize: 16 * 1024,
data: null,
file: null,
maxChunkSize: 4 * 1024 * 1000,
maxTries: 5,
onAbort: this.onAbort,
onComplete: this.onComplete,
onCreate: this.onCreate,
onError: this.onError,
onProgress: this.onProgress,
onStart: this.onStart,
onStop: this.onStop,
retryDelay: 2000,
store: null,
transferDelay: 100,
}, options);
// Check options
if (typeof options.adaptive !== 'boolean') {
throw new TypeError('adaptive is not a number');
}
if (typeof options.capacity !== 'number') {
throw new TypeError('capacity is not a number');
}
if (options.capacity <= 0 || options.capacity > 1) {
throw new RangeError('capacity must be a float between 0.1 and 1.0');
}
if (typeof options.chunkSize !== 'number') {
throw new TypeError('chunkSize is not a number');
}
if (!(options.data instanceof Blob) && !(options.data instanceof File)) {
throw new TypeError('data is not an Blob or File');
}
if (options.file === null || typeof options.file !== 'object') {
throw new TypeError('file is not an object');
}
if (typeof options.maxChunkSize !== 'number') {
throw new TypeError('maxChunkSize is not a number');
}
if (typeof options.maxTries !== 'number') {
throw new TypeError('maxTries is not a number');
}
if (typeof options.retryDelay !== 'number') {
throw new TypeError('retryDelay is not a number');
}
if (typeof options.transferDelay !== 'number') {
throw new TypeError('transferDelay is not a number');
}
if (typeof options.onAbort !== 'function') {
throw new TypeError('onAbort is not a function');
}
if (typeof options.onComplete !== 'function') {
throw new TypeError('onComplete is not a function');
}
if (typeof options.onCreate !== 'function') {
throw new TypeError('onCreate is not a function');
}
if (typeof options.onError !== 'function') {
throw new TypeError('onError is not a function');
}
if (typeof options.onProgress !== 'function') {
throw new TypeError('onProgress is not a function');
}
if (typeof options.onStart !== 'function') {
throw new TypeError('onStart is not a function');
}
if (typeof options.onStop !== 'function') {
throw new TypeError('onStop is not a function');
}
if (typeof options.store !== 'string' && !(options.store instanceof Store)) {
throw new TypeError('store must be the name of the store or an instance of UploadFS.Store');
}
// Public attributes
self.adaptive = options.adaptive;
self.capacity = parseFloat(options.capacity);
self.chunkSize = parseInt(options.chunkSize);
self.maxChunkSize = parseInt(options.maxChunkSize);
self.maxTries = parseInt(options.maxTries);
self.retryDelay = parseInt(options.retryDelay);
self.transferDelay = parseInt(options.transferDelay);
self.onAbort = options.onAbort;
self.onComplete = options.onComplete;
self.onCreate = options.onCreate;
self.onError = options.onError;
self.onProgress = options.onProgress;
self.onStart = options.onStart;
self.onStop = options.onStop;
// Private attributes
let store = options.store;
let data = options.data;
let capacityMargin = 0.1;
let file = options.file;
let fileId = null;
let offset = 0;
let loaded = 0;
let total = data.size;
let tries = 0;
let postUrl = null;
let token = null;
let complete = false;
let uploading = false;
let timeA = null;
let timeB = null;
let elapsedTime = 0;
let startTime = 0;
// Keep only the name of the store
if (store instanceof Store) {
store = store.getName();
}
// Assign file to store
file.store = store;
function finish() {
// Finish the upload by telling the store the upload is complete
Meteor.call('ufsComplete', fileId, store, token, function (err, uploadedFile) {
if (err) {
self.onError(err, file);
self.abort();
} else if (uploadedFile) {
uploading = false;
complete = true;
file = uploadedFile;
self.onComplete(uploadedFile);
}
});
}
/**
* Aborts the current transfer
*/
self.abort = function () {
// Remove the file from database
Meteor.call('ufsDelete', fileId, store, token, function (err, result) {
if (err) {
self.onError(err, file);
}
});
// Reset uploader status
uploading = false;
fileId = null;
offset = 0;
tries = 0;
loaded = 0;
complete = false;
startTime = null;
self.onAbort(file);
};
/**
* Returns the average speed in bytes per second
* @returns {number}
*/
self.getAverageSpeed = function () {
let seconds = self.getElapsedTime() / 1000;
return self.getLoaded() / seconds;
};
/**
* Returns the elapsed time in milliseconds
* @returns {number}
*/
self.getElapsedTime = function () {
if (startTime && self.isUploading()) {
return elapsedTime + (Date.now() - startTime);
}
return elapsedTime;
};
/**
* Returns the file
* @return {object}
*/
self.getFile = function () {
return file;
};
/**
* Returns the loaded bytes
* @return {number}
*/
self.getLoaded = function () {
return loaded;
};
/**
* Returns current progress
* @return {number}
*/
self.getProgress = function () {
return Math.min((loaded / total) * 100 / 100, 1.0);
};
/**
* Returns the remaining time in milliseconds
* @returns {number}
*/
self.getRemainingTime = function () {
let averageSpeed = self.getAverageSpeed();
let remainingBytes = total - self.getLoaded();
return averageSpeed && remainingBytes ? Math.max(remainingBytes / averageSpeed, 0) : 0;
};
/**
* Returns the upload speed in bytes per second
* @returns {number}
*/
self.getSpeed = function () {
if (timeA && timeB && self.isUploading()) {
let seconds = (timeB - timeA) / 1000;
return self.chunkSize / seconds;
}
return 0;
};
/**
* Returns the total bytes
* @return {number}
*/
self.getTotal = function () {
return total;
};
/**
* Checks if the transfer is complete
* @return {boolean}
*/
self.isComplete = function () {
return complete;
};
/**
* Checks if the transfer is active
* @return {boolean}
*/
self.isUploading = function () {
return uploading;
};
/**
* Reads a portion of file
* @param start
* @param length
* @param callback
* @returns {Blob}
*/
self.readChunk = function (start, length, callback) {
if (typeof callback != 'function') {
throw new Error('readChunk is missing callback');
}
try {
let end;
// Calculate the chunk size
if (length && start + length > total) {
end = total;
} else {
end = start + length;
}
// Get chunk
let chunk = data.slice(start, end);
// Pass chunk to callback
callback.call(self, null, chunk);
} catch (err) {
console.error('read error', err);
// Retry to read chunk
Meteor.setTimeout(function () {
if (tries < self.maxTries) {
tries += 1;
self.readChunk(start, length, callback);
}
}, self.retryDelay);
}
};
/**
* Sends a file chunk to the store
*/
self.sendChunk = function () {
if (!complete && startTime !== null) {
if (offset < total) {
let chunkSize = self.chunkSize;
// Use adaptive length
if (self.adaptive && timeA && timeB && timeB > timeA) {
let duration = (timeB - timeA) / 1000;
let max = self.capacity * (1 + capacityMargin);
let min = self.capacity * (1 - capacityMargin);
if (duration >= max) {
chunkSize = Math.abs(Math.round(chunkSize * (max - duration)));
} else if (duration < min) {
chunkSize = Math.round(chunkSize * (min / duration));
}
// Limit to max chunk size
if (self.maxChunkSize > 0 && chunkSize > self.maxChunkSize) {
chunkSize = self.maxChunkSize;
}
}
// Reduce chunk size to fit total
if (offset + chunkSize > total) {
chunkSize = total - offset;
}
// Prepare the chunk
self.readChunk(offset, chunkSize, function (err, chunk) {
if (err) {
self.onError(err, file);
return;
}
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if ([200, 201, 202, 204].includes(xhr.status)) {
timeB = Date.now();
offset += chunkSize;
loaded += chunkSize;
// Send next chunk
self.onProgress(file, self.getProgress());
// Finish upload
if (loaded >= total) {
elapsedTime = Date.now() - startTime;
finish();
} else {
Meteor.setTimeout(self.sendChunk, self.transferDelay);
}
} else if (![402, 403, 404, 500].includes(xhr.status)) {
// Retry until max tries is reach
// But don't retry if these errors occur
if (tries <= self.maxTries) {
tries += 1;
// Wait before retrying
Meteor.setTimeout(self.sendChunk, self.retryDelay);
} else {
self.abort();
}
} else {
self.abort();
}
}
};
// Calculate upload progress
let progress = (offset + chunkSize) / total;
// let formData = new FormData();
// formData.append('progress', progress);
// formData.append('chunk', chunk);
let url = `${postUrl}&progress=${progress}`;
timeA = Date.now();
timeB = null;
uploading = true;
// Send chunk to the store
xhr.open('POST', url, true);
xhr.send(chunk);
});
}
}
};
/**
* Starts or resumes the transfer
*/
self.start = function () {
if (!fileId) {
// Create the file document and get the token
// that allows the user to send chunks to the store.
Meteor.call('ufsCreate', _.extend({}, file), function (err, result) {
if (err) {
self.onError(err, file);
} else if (result) {
token = result.token;
postUrl = result.url;
fileId = result.fileId;
file._id = result.fileId;
self.onCreate(file);
tries = 0;
startTime = Date.now();
self.onStart(file);
self.sendChunk();
}
});
} else if (!uploading && !complete) {
// Resume uploading
tries = 0;
startTime = Date.now();
self.onStart(file);
self.sendChunk();
}
};
/**
* Stops the transfer
*/
self.stop = function () {
if (uploading) {
// Update elapsed time
elapsedTime = Date.now() - startTime;
startTime = null;
uploading = false;
self.onStop(file);
Meteor.call('ufsStop', fileId, store, token, function (err, result) {
if (err) {
self.onError(err, file);
}
});
}
};
}
/**
* Called when the file upload is aborted
* @param file
*/
onAbort(file) {
}
/**
* Called when the file upload is complete
* @param file
*/
onComplete(file) {
}
/**
* Called when the file is created in the collection
* @param file
*/
onCreate(file) {
}
/**
* Called when an error occurs during file upload
* @param err
* @param file
*/
onError(err, file) {
console.error(`ufs: ${err.message}`);
}
/**
* Called when a file chunk has been sent
* @param file
* @param progress is a float from 0.0 to 1.0
*/
onProgress(file, progress) {
}
/**
* Called when the file upload starts
* @param file
*/
onStart(file) {
}
/**
* Called when the file upload stops
* @param file
*/
onStop(file) {
}
}