activestorage 6.1.7.7 → 7.0.0.alpha1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activestorage might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +126 -321
- data/MIT-LICENSE +1 -1
- data/README.md +25 -11
- data/app/assets/javascripts/activestorage.esm.js +844 -0
- data/app/assets/javascripts/activestorage.js +257 -376
- data/app/controllers/active_storage/base_controller.rb +1 -10
- data/app/controllers/active_storage/blobs/proxy_controller.rb +14 -5
- data/app/controllers/active_storage/blobs/redirect_controller.rb +6 -4
- data/app/controllers/active_storage/representations/base_controller.rb +5 -1
- data/app/controllers/active_storage/representations/proxy_controller.rb +6 -5
- data/app/controllers/active_storage/representations/redirect_controller.rb +6 -4
- data/app/controllers/concerns/active_storage/set_blob.rb +6 -2
- data/app/controllers/concerns/active_storage/set_current.rb +3 -3
- data/app/controllers/concerns/active_storage/streaming.rb +65 -0
- data/app/javascript/activestorage/ujs.js +1 -1
- data/app/models/active_storage/attachment.rb +35 -2
- data/app/models/active_storage/blob/representable.rb +7 -5
- data/app/models/active_storage/blob.rb +26 -27
- data/app/models/active_storage/current.rb +12 -2
- data/app/models/active_storage/preview.rb +6 -4
- data/app/models/active_storage/record.rb +1 -1
- data/app/models/active_storage/variant.rb +6 -9
- data/app/models/active_storage/variant_record.rb +2 -0
- data/app/models/active_storage/variant_with_record.rb +9 -5
- data/app/models/active_storage/variation.rb +3 -3
- data/config/routes.rb +10 -10
- data/db/migrate/20170806125915_create_active_storage_tables.rb +11 -2
- data/db/update_migrate/20190112182829_add_service_name_to_active_storage_blobs.rb +0 -4
- data/db/update_migrate/20191206030411_create_active_storage_variant_records.rb +1 -3
- data/lib/active_storage/analyzer/audio_analyzer.rb +65 -0
- data/lib/active_storage/analyzer/image_analyzer/image_magick.rb +39 -0
- data/lib/active_storage/analyzer/image_analyzer/vips.rb +49 -0
- data/lib/active_storage/analyzer/image_analyzer.rb +2 -30
- data/lib/active_storage/analyzer/video_analyzer.rb +26 -11
- data/lib/active_storage/analyzer.rb +8 -4
- data/lib/active_storage/attached/changes/create_many.rb +7 -3
- data/lib/active_storage/attached/changes/create_one.rb +1 -1
- data/lib/active_storage/attached/changes/create_one_of_many.rb +1 -1
- data/lib/active_storage/attached/changes/delete_many.rb +1 -1
- data/lib/active_storage/attached/changes/delete_one.rb +1 -1
- data/lib/active_storage/attached/changes/detach_many.rb +18 -0
- data/lib/active_storage/attached/changes/detach_one.rb +24 -0
- data/lib/active_storage/attached/changes/purge_many.rb +27 -0
- data/lib/active_storage/attached/changes/purge_one.rb +27 -0
- data/lib/active_storage/attached/changes.rb +7 -1
- data/lib/active_storage/attached/many.rb +27 -15
- data/lib/active_storage/attached/model.rb +31 -5
- data/lib/active_storage/attached/one.rb +32 -27
- data/lib/active_storage/downloader.rb +2 -2
- data/lib/active_storage/engine.rb +28 -16
- data/lib/active_storage/fixture_set.rb +76 -0
- data/lib/active_storage/gem_version.rb +4 -4
- data/lib/active_storage/previewer/video_previewer.rb +0 -2
- data/lib/active_storage/previewer.rb +4 -4
- data/lib/active_storage/reflection.rb +12 -2
- data/lib/active_storage/service/azure_storage_service.rb +1 -1
- data/lib/active_storage/service/configurator.rb +1 -1
- data/lib/active_storage/service/disk_service.rb +13 -18
- data/lib/active_storage/service/gcs_service.rb +91 -7
- data/lib/active_storage/service/mirror_service.rb +1 -1
- data/lib/active_storage/service/registry.rb +1 -1
- data/lib/active_storage/service/s3_service.rb +4 -4
- data/lib/active_storage/service.rb +3 -3
- data/lib/active_storage/transformers/image_processing_transformer.rb +1 -66
- data/lib/active_storage/transformers/transformer.rb +1 -1
- data/lib/active_storage.rb +3 -292
- metadata +32 -25
- data/app/controllers/concerns/active_storage/disable_session.rb +0 -12
- data/app/controllers/concerns/active_storage/set_headers.rb +0 -12
@@ -1,18 +1,17 @@
|
|
1
1
|
(function(global, factory) {
|
2
|
-
typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define([ "exports" ], factory) :
|
3
|
-
|
2
|
+
typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define([ "exports" ], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self,
|
3
|
+
factory(global.ActiveStorage = {}));
|
4
|
+
})(this, (function(exports) {
|
4
5
|
"use strict";
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
}
|
10
|
-
var sparkMd5 = createCommonjsModule(function(module, exports) {
|
6
|
+
var sparkMd5 = {
|
7
|
+
exports: {}
|
8
|
+
};
|
9
|
+
(function(module, exports) {
|
11
10
|
(function(factory) {
|
12
11
|
{
|
13
12
|
module.exports = factory();
|
14
13
|
}
|
15
|
-
})(function(undefined) {
|
14
|
+
})((function(undefined$1) {
|
16
15
|
var hex_chr = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" ];
|
17
16
|
function md5cycle(x, k) {
|
18
17
|
var a = x[0], b = x[1], c = x[2], d = x[3];
|
@@ -243,7 +242,7 @@
|
|
243
242
|
}
|
244
243
|
ArrayBuffer.prototype.slice = function(from, to) {
|
245
244
|
var length = this.byteLength, begin = clamp(from, length), end = length, num, target, targetArray, sourceArray;
|
246
|
-
if (to !== undefined) {
|
245
|
+
if (to !== undefined$1) {
|
247
246
|
end = clamp(to, length);
|
248
247
|
}
|
249
248
|
if (begin > end) {
|
@@ -327,7 +326,7 @@
|
|
327
326
|
return {
|
328
327
|
buff: this._buff,
|
329
328
|
length: this._length,
|
330
|
-
hash: this._hash
|
329
|
+
hash: this._hash.slice()
|
331
330
|
};
|
332
331
|
};
|
333
332
|
SparkMD5.prototype.setState = function(state) {
|
@@ -412,94 +411,55 @@
|
|
412
411
|
return raw ? hexToBinaryString(ret) : ret;
|
413
412
|
};
|
414
413
|
return SparkMD5;
|
415
|
-
});
|
416
|
-
});
|
417
|
-
var
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
function defineProperties(target, props) {
|
424
|
-
for (var i = 0; i < props.length; i++) {
|
425
|
-
var descriptor = props[i];
|
426
|
-
descriptor.enumerable = descriptor.enumerable || false;
|
427
|
-
descriptor.configurable = true;
|
428
|
-
if ("value" in descriptor) descriptor.writable = true;
|
429
|
-
Object.defineProperty(target, descriptor.key, descriptor);
|
430
|
-
}
|
414
|
+
}));
|
415
|
+
})(sparkMd5);
|
416
|
+
var SparkMD5 = sparkMd5.exports;
|
417
|
+
const fileSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice;
|
418
|
+
class FileChecksum {
|
419
|
+
static create(file, callback) {
|
420
|
+
const instance = new FileChecksum(file);
|
421
|
+
instance.create(callback);
|
431
422
|
}
|
432
|
-
|
433
|
-
if (protoProps) defineProperties(Constructor.prototype, protoProps);
|
434
|
-
if (staticProps) defineProperties(Constructor, staticProps);
|
435
|
-
return Constructor;
|
436
|
-
};
|
437
|
-
}();
|
438
|
-
var fileSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice;
|
439
|
-
var FileChecksum = function() {
|
440
|
-
createClass(FileChecksum, null, [ {
|
441
|
-
key: "create",
|
442
|
-
value: function create(file, callback) {
|
443
|
-
var instance = new FileChecksum(file);
|
444
|
-
instance.create(callback);
|
445
|
-
}
|
446
|
-
} ]);
|
447
|
-
function FileChecksum(file) {
|
448
|
-
classCallCheck(this, FileChecksum);
|
423
|
+
constructor(file) {
|
449
424
|
this.file = file;
|
450
425
|
this.chunkSize = 2097152;
|
451
426
|
this.chunkCount = Math.ceil(this.file.size / this.chunkSize);
|
452
427
|
this.chunkIndex = 0;
|
453
428
|
}
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
this.
|
468
|
-
}
|
469
|
-
}, {
|
470
|
-
key: "fileReaderDidLoad",
|
471
|
-
value: function fileReaderDidLoad(event) {
|
472
|
-
this.md5Buffer.append(event.target.result);
|
473
|
-
if (!this.readNextChunk()) {
|
474
|
-
var binaryDigest = this.md5Buffer.end(true);
|
475
|
-
var base64digest = btoa(binaryDigest);
|
476
|
-
this.callback(null, base64digest);
|
477
|
-
}
|
478
|
-
}
|
479
|
-
}, {
|
480
|
-
key: "fileReaderDidError",
|
481
|
-
value: function fileReaderDidError(event) {
|
482
|
-
this.callback("Error reading " + this.file.name);
|
429
|
+
create(callback) {
|
430
|
+
this.callback = callback;
|
431
|
+
this.md5Buffer = new SparkMD5.ArrayBuffer;
|
432
|
+
this.fileReader = new FileReader;
|
433
|
+
this.fileReader.addEventListener("load", (event => this.fileReaderDidLoad(event)));
|
434
|
+
this.fileReader.addEventListener("error", (event => this.fileReaderDidError(event)));
|
435
|
+
this.readNextChunk();
|
436
|
+
}
|
437
|
+
fileReaderDidLoad(event) {
|
438
|
+
this.md5Buffer.append(event.target.result);
|
439
|
+
if (!this.readNextChunk()) {
|
440
|
+
const binaryDigest = this.md5Buffer.end(true);
|
441
|
+
const base64digest = btoa(binaryDigest);
|
442
|
+
this.callback(null, base64digest);
|
483
443
|
}
|
484
|
-
}
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
444
|
+
}
|
445
|
+
fileReaderDidError(event) {
|
446
|
+
this.callback(`Error reading ${this.file.name}`);
|
447
|
+
}
|
448
|
+
readNextChunk() {
|
449
|
+
if (this.chunkIndex < this.chunkCount || this.chunkIndex == 0 && this.chunkCount == 0) {
|
450
|
+
const start = this.chunkIndex * this.chunkSize;
|
451
|
+
const end = Math.min(start + this.chunkSize, this.file.size);
|
452
|
+
const bytes = fileSlice.call(this.file, start, end);
|
453
|
+
this.fileReader.readAsArrayBuffer(bytes);
|
454
|
+
this.chunkIndex++;
|
455
|
+
return true;
|
456
|
+
} else {
|
457
|
+
return false;
|
497
458
|
}
|
498
|
-
}
|
499
|
-
|
500
|
-
}();
|
459
|
+
}
|
460
|
+
}
|
501
461
|
function getMetaValue(name) {
|
502
|
-
|
462
|
+
const element = findElement(document.head, `meta[name="${name}"]`);
|
503
463
|
if (element) {
|
504
464
|
return element.getAttribute("content");
|
505
465
|
}
|
@@ -509,8 +469,8 @@
|
|
509
469
|
selector = root;
|
510
470
|
root = document;
|
511
471
|
}
|
512
|
-
|
513
|
-
return toArray
|
472
|
+
const elements = root.querySelectorAll(selector);
|
473
|
+
return toArray(elements);
|
514
474
|
}
|
515
475
|
function findElement(root, selector) {
|
516
476
|
if (typeof root == "string") {
|
@@ -519,11 +479,10 @@
|
|
519
479
|
}
|
520
480
|
return root.querySelector(selector);
|
521
481
|
}
|
522
|
-
function dispatchEvent(element, type) {
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
var event = document.createEvent("Event");
|
482
|
+
function dispatchEvent(element, type, eventInit = {}) {
|
483
|
+
const {disabled: disabled} = element;
|
484
|
+
const {bubbles: bubbles, cancelable: cancelable, detail: detail} = eventInit;
|
485
|
+
const event = document.createEvent("Event");
|
527
486
|
event.initEvent(type, bubbles || true, cancelable || true);
|
528
487
|
event.detail = detail || {};
|
529
488
|
try {
|
@@ -534,7 +493,7 @@
|
|
534
493
|
}
|
535
494
|
return event;
|
536
495
|
}
|
537
|
-
function toArray
|
496
|
+
function toArray(value) {
|
538
497
|
if (Array.isArray(value)) {
|
539
498
|
return value;
|
540
499
|
} else if (Array.from) {
|
@@ -543,10 +502,8 @@
|
|
543
502
|
return [].slice.call(value);
|
544
503
|
}
|
545
504
|
}
|
546
|
-
|
547
|
-
|
548
|
-
var _this = this;
|
549
|
-
classCallCheck(this, BlobRecord);
|
505
|
+
class BlobRecord {
|
506
|
+
constructor(file, checksum, url) {
|
550
507
|
this.file = file;
|
551
508
|
this.attributes = {
|
552
509
|
filename: file.name,
|
@@ -554,322 +511,246 @@
|
|
554
511
|
byte_size: file.size,
|
555
512
|
checksum: checksum
|
556
513
|
};
|
557
|
-
this.xhr = new XMLHttpRequest
|
514
|
+
this.xhr = new XMLHttpRequest;
|
558
515
|
this.xhr.open("POST", url, true);
|
559
516
|
this.xhr.responseType = "json";
|
560
517
|
this.xhr.setRequestHeader("Content-Type", "application/json");
|
561
518
|
this.xhr.setRequestHeader("Accept", "application/json");
|
562
519
|
this.xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
|
563
|
-
|
520
|
+
const csrfToken = getMetaValue("csrf-token");
|
564
521
|
if (csrfToken != undefined) {
|
565
522
|
this.xhr.setRequestHeader("X-CSRF-Token", csrfToken);
|
566
523
|
}
|
567
|
-
this.xhr.addEventListener("load",
|
568
|
-
|
569
|
-
});
|
570
|
-
this.xhr.addEventListener("error", function(event) {
|
571
|
-
return _this.requestDidError(event);
|
572
|
-
});
|
524
|
+
this.xhr.addEventListener("load", (event => this.requestDidLoad(event)));
|
525
|
+
this.xhr.addEventListener("error", (event => this.requestDidError(event)));
|
573
526
|
}
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
}
|
582
|
-
|
583
|
-
key: "requestDidLoad",
|
584
|
-
value: function requestDidLoad(event) {
|
585
|
-
if (this.status >= 200 && this.status < 300) {
|
586
|
-
var response = this.response;
|
587
|
-
var direct_upload = response.direct_upload;
|
588
|
-
delete response.direct_upload;
|
589
|
-
this.attributes = response;
|
590
|
-
this.directUploadData = direct_upload;
|
591
|
-
this.callback(null, this.toJSON());
|
592
|
-
} else {
|
593
|
-
this.requestDidError(event);
|
594
|
-
}
|
595
|
-
}
|
596
|
-
}, {
|
597
|
-
key: "requestDidError",
|
598
|
-
value: function requestDidError(event) {
|
599
|
-
this.callback('Error creating Blob for "' + this.file.name + '". Status: ' + this.status);
|
600
|
-
}
|
601
|
-
}, {
|
602
|
-
key: "toJSON",
|
603
|
-
value: function toJSON() {
|
604
|
-
var result = {};
|
605
|
-
for (var key in this.attributes) {
|
606
|
-
result[key] = this.attributes[key];
|
607
|
-
}
|
608
|
-
return result;
|
527
|
+
get status() {
|
528
|
+
return this.xhr.status;
|
529
|
+
}
|
530
|
+
get response() {
|
531
|
+
const {responseType: responseType, response: response} = this.xhr;
|
532
|
+
if (responseType == "json") {
|
533
|
+
return response;
|
534
|
+
} else {
|
535
|
+
return JSON.parse(response);
|
609
536
|
}
|
610
|
-
}
|
611
|
-
|
612
|
-
|
613
|
-
|
537
|
+
}
|
538
|
+
create(callback) {
|
539
|
+
this.callback = callback;
|
540
|
+
this.xhr.send(JSON.stringify({
|
541
|
+
blob: this.attributes
|
542
|
+
}));
|
543
|
+
}
|
544
|
+
requestDidLoad(event) {
|
545
|
+
if (this.status >= 200 && this.status < 300) {
|
546
|
+
const {response: response} = this;
|
547
|
+
const {direct_upload: direct_upload} = response;
|
548
|
+
delete response.direct_upload;
|
549
|
+
this.attributes = response;
|
550
|
+
this.directUploadData = direct_upload;
|
551
|
+
this.callback(null, this.toJSON());
|
552
|
+
} else {
|
553
|
+
this.requestDidError(event);
|
614
554
|
}
|
615
|
-
}
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
}
|
555
|
+
}
|
556
|
+
requestDidError(event) {
|
557
|
+
this.callback(`Error creating Blob for "${this.file.name}". Status: ${this.status}`);
|
558
|
+
}
|
559
|
+
toJSON() {
|
560
|
+
const result = {};
|
561
|
+
for (const key in this.attributes) {
|
562
|
+
result[key] = this.attributes[key];
|
624
563
|
}
|
625
|
-
|
626
|
-
|
627
|
-
}
|
628
|
-
|
629
|
-
|
630
|
-
var _this = this;
|
631
|
-
classCallCheck(this, BlobUpload);
|
564
|
+
return result;
|
565
|
+
}
|
566
|
+
}
|
567
|
+
class BlobUpload {
|
568
|
+
constructor(blob) {
|
632
569
|
this.blob = blob;
|
633
570
|
this.file = blob.file;
|
634
|
-
|
635
|
-
this.xhr = new XMLHttpRequest
|
571
|
+
const {url: url, headers: headers} = blob.directUploadData;
|
572
|
+
this.xhr = new XMLHttpRequest;
|
636
573
|
this.xhr.open("PUT", url, true);
|
637
574
|
this.xhr.responseType = "text";
|
638
|
-
for (
|
575
|
+
for (const key in headers) {
|
639
576
|
this.xhr.setRequestHeader(key, headers[key]);
|
640
577
|
}
|
641
|
-
this.xhr.addEventListener("load",
|
642
|
-
|
643
|
-
});
|
644
|
-
this.xhr.addEventListener("error", function(event) {
|
645
|
-
return _this.requestDidError(event);
|
646
|
-
});
|
578
|
+
this.xhr.addEventListener("load", (event => this.requestDidLoad(event)));
|
579
|
+
this.xhr.addEventListener("error", (event => this.requestDidError(event)));
|
647
580
|
}
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
}
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
if (status >= 200 && status < 300) {
|
659
|
-
this.callback(null, response);
|
660
|
-
} else {
|
661
|
-
this.requestDidError(event);
|
662
|
-
}
|
663
|
-
}
|
664
|
-
}, {
|
665
|
-
key: "requestDidError",
|
666
|
-
value: function requestDidError(event) {
|
667
|
-
this.callback('Error storing "' + this.file.name + '". Status: ' + this.xhr.status);
|
581
|
+
create(callback) {
|
582
|
+
this.callback = callback;
|
583
|
+
this.xhr.send(this.file.slice());
|
584
|
+
}
|
585
|
+
requestDidLoad(event) {
|
586
|
+
const {status: status, response: response} = this.xhr;
|
587
|
+
if (status >= 200 && status < 300) {
|
588
|
+
this.callback(null, response);
|
589
|
+
} else {
|
590
|
+
this.requestDidError(event);
|
668
591
|
}
|
669
|
-
}
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
592
|
+
}
|
593
|
+
requestDidError(event) {
|
594
|
+
this.callback(`Error storing "${this.file.name}". Status: ${this.xhr.status}`);
|
595
|
+
}
|
596
|
+
}
|
597
|
+
let id = 0;
|
598
|
+
class DirectUpload {
|
599
|
+
constructor(file, url, delegate) {
|
676
600
|
this.id = ++id;
|
677
601
|
this.file = file;
|
678
602
|
this.url = url;
|
679
603
|
this.delegate = delegate;
|
680
604
|
}
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
605
|
+
create(callback) {
|
606
|
+
FileChecksum.create(this.file, ((error, checksum) => {
|
607
|
+
if (error) {
|
608
|
+
callback(error);
|
609
|
+
return;
|
610
|
+
}
|
611
|
+
const blob = new BlobRecord(this.file, checksum, this.url);
|
612
|
+
notify(this.delegate, "directUploadWillCreateBlobWithXHR", blob.xhr);
|
613
|
+
blob.create((error => {
|
686
614
|
if (error) {
|
687
615
|
callback(error);
|
688
|
-
|
616
|
+
} else {
|
617
|
+
const upload = new BlobUpload(blob);
|
618
|
+
notify(this.delegate, "directUploadWillStoreFileWithXHR", upload.xhr);
|
619
|
+
upload.create((error => {
|
620
|
+
if (error) {
|
621
|
+
callback(error);
|
622
|
+
} else {
|
623
|
+
callback(null, blob.toJSON());
|
624
|
+
}
|
625
|
+
}));
|
689
626
|
}
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
} else {
|
696
|
-
var upload = new BlobUpload(blob);
|
697
|
-
notify(_this.delegate, "directUploadWillStoreFileWithXHR", upload.xhr);
|
698
|
-
upload.create(function(error) {
|
699
|
-
if (error) {
|
700
|
-
callback(error);
|
701
|
-
} else {
|
702
|
-
callback(null, blob.toJSON());
|
703
|
-
}
|
704
|
-
});
|
705
|
-
}
|
706
|
-
});
|
707
|
-
});
|
708
|
-
}
|
709
|
-
} ]);
|
710
|
-
return DirectUpload;
|
711
|
-
}();
|
712
|
-
function notify(object, methodName) {
|
627
|
+
}));
|
628
|
+
}));
|
629
|
+
}
|
630
|
+
}
|
631
|
+
function notify(object, methodName, ...messages) {
|
713
632
|
if (object && typeof object[methodName] == "function") {
|
714
|
-
|
715
|
-
messages[_key - 2] = arguments[_key];
|
716
|
-
}
|
717
|
-
return object[methodName].apply(object, messages);
|
633
|
+
return object[methodName](...messages);
|
718
634
|
}
|
719
635
|
}
|
720
|
-
|
721
|
-
|
722
|
-
classCallCheck(this, DirectUploadController);
|
636
|
+
class DirectUploadController {
|
637
|
+
constructor(input, file) {
|
723
638
|
this.input = input;
|
724
639
|
this.file = file;
|
725
640
|
this.directUpload = new DirectUpload(this.file, this.url, this);
|
726
641
|
this.dispatch("initialize");
|
727
642
|
}
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
_this.dispatchError(error);
|
741
|
-
} else {
|
742
|
-
hiddenInput.value = attributes.signed_id;
|
743
|
-
}
|
744
|
-
_this.dispatch("end");
|
745
|
-
callback(error);
|
746
|
-
});
|
747
|
-
}
|
748
|
-
}, {
|
749
|
-
key: "uploadRequestDidProgress",
|
750
|
-
value: function uploadRequestDidProgress(event) {
|
751
|
-
var progress = event.loaded / event.total * 100;
|
752
|
-
if (progress) {
|
753
|
-
this.dispatch("progress", {
|
754
|
-
progress: progress
|
755
|
-
});
|
756
|
-
}
|
757
|
-
}
|
758
|
-
}, {
|
759
|
-
key: "dispatch",
|
760
|
-
value: function dispatch(name) {
|
761
|
-
var detail = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
762
|
-
detail.file = this.file;
|
763
|
-
detail.id = this.directUpload.id;
|
764
|
-
return dispatchEvent(this.input, "direct-upload:" + name, {
|
765
|
-
detail: detail
|
766
|
-
});
|
767
|
-
}
|
768
|
-
}, {
|
769
|
-
key: "dispatchError",
|
770
|
-
value: function dispatchError(error) {
|
771
|
-
var event = this.dispatch("error", {
|
772
|
-
error: error
|
773
|
-
});
|
774
|
-
if (!event.defaultPrevented) {
|
775
|
-
alert(error);
|
643
|
+
start(callback) {
|
644
|
+
const hiddenInput = document.createElement("input");
|
645
|
+
hiddenInput.type = "hidden";
|
646
|
+
hiddenInput.name = this.input.name;
|
647
|
+
this.input.insertAdjacentElement("beforebegin", hiddenInput);
|
648
|
+
this.dispatch("start");
|
649
|
+
this.directUpload.create(((error, attributes) => {
|
650
|
+
if (error) {
|
651
|
+
hiddenInput.parentNode.removeChild(hiddenInput);
|
652
|
+
this.dispatchError(error);
|
653
|
+
} else {
|
654
|
+
hiddenInput.value = attributes.signed_id;
|
776
655
|
}
|
777
|
-
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
784
|
-
|
785
|
-
|
786
|
-
key: "directUploadWillStoreFileWithXHR",
|
787
|
-
value: function directUploadWillStoreFileWithXHR(xhr) {
|
788
|
-
var _this2 = this;
|
789
|
-
this.dispatch("before-storage-request", {
|
790
|
-
xhr: xhr
|
791
|
-
});
|
792
|
-
xhr.upload.addEventListener("progress", function(event) {
|
793
|
-
return _this2.uploadRequestDidProgress(event);
|
656
|
+
this.dispatch("end");
|
657
|
+
callback(error);
|
658
|
+
}));
|
659
|
+
}
|
660
|
+
uploadRequestDidProgress(event) {
|
661
|
+
const progress = event.loaded / event.total * 100;
|
662
|
+
if (progress) {
|
663
|
+
this.dispatch("progress", {
|
664
|
+
progress: progress
|
794
665
|
});
|
795
666
|
}
|
796
|
-
}
|
797
|
-
|
798
|
-
|
799
|
-
|
667
|
+
}
|
668
|
+
get url() {
|
669
|
+
return this.input.getAttribute("data-direct-upload-url");
|
670
|
+
}
|
671
|
+
dispatch(name, detail = {}) {
|
672
|
+
detail.file = this.file;
|
673
|
+
detail.id = this.directUpload.id;
|
674
|
+
return dispatchEvent(this.input, `direct-upload:${name}`, {
|
675
|
+
detail: detail
|
676
|
+
});
|
677
|
+
}
|
678
|
+
dispatchError(error) {
|
679
|
+
const event = this.dispatch("error", {
|
680
|
+
error: error
|
681
|
+
});
|
682
|
+
if (!event.defaultPrevented) {
|
683
|
+
alert(error);
|
800
684
|
}
|
801
|
-
}
|
802
|
-
|
803
|
-
|
804
|
-
|
805
|
-
|
806
|
-
|
807
|
-
|
685
|
+
}
|
686
|
+
directUploadWillCreateBlobWithXHR(xhr) {
|
687
|
+
this.dispatch("before-blob-request", {
|
688
|
+
xhr: xhr
|
689
|
+
});
|
690
|
+
}
|
691
|
+
directUploadWillStoreFileWithXHR(xhr) {
|
692
|
+
this.dispatch("before-storage-request", {
|
693
|
+
xhr: xhr
|
694
|
+
});
|
695
|
+
xhr.upload.addEventListener("progress", (event => this.uploadRequestDidProgress(event)));
|
696
|
+
}
|
697
|
+
}
|
698
|
+
const inputSelector = "input[type=file][data-direct-upload-url]:not([disabled])";
|
699
|
+
class DirectUploadsController {
|
700
|
+
constructor(form) {
|
808
701
|
this.form = form;
|
809
|
-
this.inputs = findElements(form, inputSelector).filter(
|
810
|
-
|
702
|
+
this.inputs = findElements(form, inputSelector).filter((input => input.files.length));
|
703
|
+
}
|
704
|
+
start(callback) {
|
705
|
+
const controllers = this.createDirectUploadControllers();
|
706
|
+
const startNextController = () => {
|
707
|
+
const controller = controllers.shift();
|
708
|
+
if (controller) {
|
709
|
+
controller.start((error => {
|
710
|
+
if (error) {
|
711
|
+
callback(error);
|
712
|
+
this.dispatch("end");
|
713
|
+
} else {
|
714
|
+
startNextController();
|
715
|
+
}
|
716
|
+
}));
|
717
|
+
} else {
|
718
|
+
callback();
|
719
|
+
this.dispatch("end");
|
720
|
+
}
|
721
|
+
};
|
722
|
+
this.dispatch("start");
|
723
|
+
startNextController();
|
724
|
+
}
|
725
|
+
createDirectUploadControllers() {
|
726
|
+
const controllers = [];
|
727
|
+
this.inputs.forEach((input => {
|
728
|
+
toArray(input.files).forEach((file => {
|
729
|
+
const controller = new DirectUploadController(input, file);
|
730
|
+
controllers.push(controller);
|
731
|
+
}));
|
732
|
+
}));
|
733
|
+
return controllers;
|
734
|
+
}
|
735
|
+
dispatch(name, detail = {}) {
|
736
|
+
return dispatchEvent(this.form, `direct-uploads:${name}`, {
|
737
|
+
detail: detail
|
811
738
|
});
|
812
739
|
}
|
813
|
-
|
814
|
-
|
815
|
-
|
816
|
-
|
817
|
-
var controllers = this.createDirectUploadControllers();
|
818
|
-
var startNextController = function startNextController() {
|
819
|
-
var controller = controllers.shift();
|
820
|
-
if (controller) {
|
821
|
-
controller.start(function(error) {
|
822
|
-
if (error) {
|
823
|
-
callback(error);
|
824
|
-
_this.dispatch("end");
|
825
|
-
} else {
|
826
|
-
startNextController();
|
827
|
-
}
|
828
|
-
});
|
829
|
-
} else {
|
830
|
-
callback();
|
831
|
-
_this.dispatch("end");
|
832
|
-
}
|
833
|
-
};
|
834
|
-
this.dispatch("start");
|
835
|
-
startNextController();
|
836
|
-
}
|
837
|
-
}, {
|
838
|
-
key: "createDirectUploadControllers",
|
839
|
-
value: function createDirectUploadControllers() {
|
840
|
-
var controllers = [];
|
841
|
-
this.inputs.forEach(function(input) {
|
842
|
-
toArray$1(input.files).forEach(function(file) {
|
843
|
-
var controller = new DirectUploadController(input, file);
|
844
|
-
controllers.push(controller);
|
845
|
-
});
|
846
|
-
});
|
847
|
-
return controllers;
|
848
|
-
}
|
849
|
-
}, {
|
850
|
-
key: "dispatch",
|
851
|
-
value: function dispatch(name) {
|
852
|
-
var detail = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
853
|
-
return dispatchEvent(this.form, "direct-uploads:" + name, {
|
854
|
-
detail: detail
|
855
|
-
});
|
856
|
-
}
|
857
|
-
} ]);
|
858
|
-
return DirectUploadsController;
|
859
|
-
}();
|
860
|
-
var processingAttribute = "data-direct-uploads-processing";
|
861
|
-
var submitButtonsByForm = new WeakMap();
|
862
|
-
var started = false;
|
740
|
+
}
|
741
|
+
const processingAttribute = "data-direct-uploads-processing";
|
742
|
+
const submitButtonsByForm = new WeakMap;
|
743
|
+
let started = false;
|
863
744
|
function start() {
|
864
745
|
if (!started) {
|
865
746
|
started = true;
|
866
747
|
document.addEventListener("click", didClick, true);
|
867
|
-
document.addEventListener("submit", didSubmitForm);
|
748
|
+
document.addEventListener("submit", didSubmitForm, true);
|
868
749
|
document.addEventListener("ajax:before", didSubmitRemoteElement);
|
869
750
|
}
|
870
751
|
}
|
871
752
|
function didClick(event) {
|
872
|
-
|
753
|
+
const {target: target} = event;
|
873
754
|
if ((target.tagName == "INPUT" || target.tagName == "BUTTON") && target.type == "submit" && target.form) {
|
874
755
|
submitButtonsByForm.set(target.form, target);
|
875
756
|
}
|
@@ -883,31 +764,31 @@
|
|
883
764
|
}
|
884
765
|
}
|
885
766
|
function handleFormSubmissionEvent(event) {
|
886
|
-
|
767
|
+
const form = event.target;
|
887
768
|
if (form.hasAttribute(processingAttribute)) {
|
888
769
|
event.preventDefault();
|
889
770
|
return;
|
890
771
|
}
|
891
|
-
|
892
|
-
|
772
|
+
const controller = new DirectUploadsController(form);
|
773
|
+
const {inputs: inputs} = controller;
|
893
774
|
if (inputs.length) {
|
894
775
|
event.preventDefault();
|
895
776
|
form.setAttribute(processingAttribute, "");
|
896
777
|
inputs.forEach(disable);
|
897
|
-
controller.start(
|
778
|
+
controller.start((error => {
|
898
779
|
form.removeAttribute(processingAttribute);
|
899
780
|
if (error) {
|
900
781
|
inputs.forEach(enable);
|
901
782
|
} else {
|
902
783
|
submitForm(form);
|
903
784
|
}
|
904
|
-
});
|
785
|
+
}));
|
905
786
|
}
|
906
787
|
}
|
907
788
|
function submitForm(form) {
|
908
|
-
|
789
|
+
let button = submitButtonsByForm.get(form) || findElement(form, "input[type=submit], button[type=submit]");
|
909
790
|
if (button) {
|
910
|
-
|
791
|
+
const {disabled: disabled} = button;
|
911
792
|
button.disabled = false;
|
912
793
|
button.focus();
|
913
794
|
button.click();
|
@@ -934,9 +815,9 @@
|
|
934
815
|
}
|
935
816
|
}
|
936
817
|
setTimeout(autostart, 1);
|
937
|
-
exports.start = start;
|
938
818
|
exports.DirectUpload = DirectUpload;
|
819
|
+
exports.start = start;
|
939
820
|
Object.defineProperty(exports, "__esModule", {
|
940
821
|
value: true
|
941
822
|
});
|
942
|
-
});
|
823
|
+
}));
|