activestorage 6.1.7 → 7.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +152 -276
  3. data/MIT-LICENSE +1 -1
  4. data/README.md +29 -15
  5. data/app/assets/javascripts/activestorage.esm.js +848 -0
  6. data/app/assets/javascripts/activestorage.js +263 -376
  7. data/app/controllers/active_storage/base_controller.rb +0 -9
  8. data/app/controllers/active_storage/blobs/proxy_controller.rb +16 -4
  9. data/app/controllers/active_storage/blobs/redirect_controller.rb +6 -4
  10. data/app/controllers/active_storage/disk_controller.rb +5 -2
  11. data/app/controllers/active_storage/representations/base_controller.rb +5 -1
  12. data/app/controllers/active_storage/representations/proxy_controller.rb +8 -3
  13. data/app/controllers/active_storage/representations/redirect_controller.rb +6 -4
  14. data/app/controllers/concerns/active_storage/disable_session.rb +12 -0
  15. data/app/controllers/concerns/active_storage/file_server.rb +4 -1
  16. data/app/controllers/concerns/active_storage/set_blob.rb +6 -2
  17. data/app/controllers/concerns/active_storage/set_current.rb +3 -3
  18. data/app/controllers/concerns/active_storage/streaming.rb +66 -0
  19. data/app/javascript/activestorage/blob_record.js +4 -1
  20. data/app/javascript/activestorage/direct_upload.js +3 -2
  21. data/app/javascript/activestorage/index.js +3 -1
  22. data/app/javascript/activestorage/ujs.js +1 -1
  23. data/app/jobs/active_storage/analyze_job.rb +1 -1
  24. data/app/jobs/active_storage/mirror_job.rb +1 -1
  25. data/app/jobs/active_storage/purge_job.rb +1 -1
  26. data/app/jobs/active_storage/transform_job.rb +12 -0
  27. data/app/models/active_storage/attachment.rb +111 -4
  28. data/app/models/active_storage/blob/analyzable.rb +4 -3
  29. data/app/models/active_storage/blob/identifiable.rb +1 -0
  30. data/app/models/active_storage/blob/representable.rb +14 -8
  31. data/app/models/active_storage/blob.rb +93 -57
  32. data/app/models/active_storage/current.rb +2 -2
  33. data/app/models/active_storage/filename.rb +2 -0
  34. data/app/models/active_storage/named_variant.rb +21 -0
  35. data/app/models/active_storage/preview.rb +11 -7
  36. data/app/models/active_storage/record.rb +1 -1
  37. data/app/models/active_storage/variant.rb +10 -12
  38. data/app/models/active_storage/variant_record.rb +2 -0
  39. data/app/models/active_storage/variant_with_record.rb +28 -12
  40. data/app/models/active_storage/variation.rb +7 -5
  41. data/config/routes.rb +12 -10
  42. data/db/migrate/20170806125915_create_active_storage_tables.rb +15 -6
  43. data/db/update_migrate/20211119233751_remove_not_null_on_active_storage_blobs_checksum.rb +7 -0
  44. data/lib/active_storage/analyzer/audio_analyzer.rb +77 -0
  45. data/lib/active_storage/analyzer/image_analyzer/image_magick.rb +41 -0
  46. data/lib/active_storage/analyzer/image_analyzer/vips.rb +51 -0
  47. data/lib/active_storage/analyzer/image_analyzer.rb +4 -30
  48. data/lib/active_storage/analyzer/video_analyzer.rb +41 -17
  49. data/lib/active_storage/analyzer.rb +10 -4
  50. data/lib/active_storage/attached/changes/create_many.rb +14 -5
  51. data/lib/active_storage/attached/changes/create_one.rb +46 -4
  52. data/lib/active_storage/attached/changes/create_one_of_many.rb +1 -1
  53. data/lib/active_storage/attached/changes/delete_many.rb +1 -1
  54. data/lib/active_storage/attached/changes/delete_one.rb +1 -1
  55. data/lib/active_storage/attached/changes/detach_many.rb +18 -0
  56. data/lib/active_storage/attached/changes/detach_one.rb +24 -0
  57. data/lib/active_storage/attached/changes/purge_many.rb +27 -0
  58. data/lib/active_storage/attached/changes/purge_one.rb +27 -0
  59. data/lib/active_storage/attached/changes.rb +7 -1
  60. data/lib/active_storage/attached/many.rb +32 -19
  61. data/lib/active_storage/attached/model.rb +80 -29
  62. data/lib/active_storage/attached/one.rb +37 -31
  63. data/lib/active_storage/attached.rb +2 -0
  64. data/lib/active_storage/deprecator.rb +7 -0
  65. data/lib/active_storage/downloader.rb +4 -4
  66. data/lib/active_storage/engine.rb +55 -7
  67. data/lib/active_storage/fixture_set.rb +75 -0
  68. data/lib/active_storage/gem_version.rb +3 -3
  69. data/lib/active_storage/log_subscriber.rb +12 -0
  70. data/lib/active_storage/previewer.rb +12 -5
  71. data/lib/active_storage/reflection.rb +12 -2
  72. data/lib/active_storage/service/azure_storage_service.rb +30 -6
  73. data/lib/active_storage/service/configurator.rb +1 -1
  74. data/lib/active_storage/service/disk_service.rb +26 -19
  75. data/lib/active_storage/service/gcs_service.rb +100 -11
  76. data/lib/active_storage/service/mirror_service.rb +12 -7
  77. data/lib/active_storage/service/registry.rb +1 -1
  78. data/lib/active_storage/service/s3_service.rb +39 -15
  79. data/lib/active_storage/service.rb +17 -7
  80. data/lib/active_storage/transformers/image_processing_transformer.rb +1 -1
  81. data/lib/active_storage/transformers/transformer.rb +3 -1
  82. data/lib/active_storage/version.rb +1 -1
  83. data/lib/active_storage.rb +22 -2
  84. metadata +30 -30
  85. 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) : factory(global.ActiveStorage = {});
3
- })(this, function(exports) {
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
- function createCommonjsModule(fn, module) {
6
- return module = {
7
- exports: {}
8
- }, fn(module, module.exports), module.exports;
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 classCallCheck = function(instance, Constructor) {
418
- if (!(instance instanceof Constructor)) {
419
- throw new TypeError("Cannot call a class as a function");
420
- }
421
- };
422
- var createClass = function() {
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
- return function(Constructor, protoProps, staticProps) {
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
- createClass(FileChecksum, [ {
455
- key: "create",
456
- value: function create(callback) {
457
- var _this = this;
458
- this.callback = callback;
459
- this.md5Buffer = new sparkMd5.ArrayBuffer();
460
- this.fileReader = new FileReader();
461
- this.fileReader.addEventListener("load", function(event) {
462
- return _this.fileReaderDidLoad(event);
463
- });
464
- this.fileReader.addEventListener("error", function(event) {
465
- return _this.fileReaderDidError(event);
466
- });
467
- this.readNextChunk();
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
- key: "readNextChunk",
486
- value: function readNextChunk() {
487
- if (this.chunkIndex < this.chunkCount || this.chunkIndex == 0 && this.chunkCount == 0) {
488
- var start = this.chunkIndex * this.chunkSize;
489
- var end = Math.min(start + this.chunkSize, this.file.size);
490
- var bytes = fileSlice.call(this.file, start, end);
491
- this.fileReader.readAsArrayBuffer(bytes);
492
- this.chunkIndex++;
493
- return true;
494
- } else {
495
- return false;
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
- return FileChecksum;
500
- }();
459
+ }
460
+ }
501
461
  function getMetaValue(name) {
502
- var element = findElement(document.head, 'meta[name="' + name + '"]');
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
- var elements = root.querySelectorAll(selector);
513
- return toArray$1(elements);
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
- var eventInit = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
524
- var disabled = element.disabled;
525
- var bubbles = eventInit.bubbles, cancelable = eventInit.cancelable, detail = eventInit.detail;
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$1(value) {
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
- var BlobRecord = function() {
547
- function BlobRecord(file, checksum, url) {
548
- var _this = this;
549
- classCallCheck(this, BlobRecord);
505
+ class BlobRecord {
506
+ constructor(file, checksum, url, customHeaders = {}) {
550
507
  this.file = file;
551
508
  this.attributes = {
552
509
  filename: file.name,
@@ -554,322 +511,250 @@
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
- var csrfToken = getMetaValue("csrf-token");
520
+ Object.keys(customHeaders).forEach((headerKey => {
521
+ this.xhr.setRequestHeader(headerKey, customHeaders[headerKey]);
522
+ }));
523
+ const csrfToken = getMetaValue("csrf-token");
564
524
  if (csrfToken != undefined) {
565
525
  this.xhr.setRequestHeader("X-CSRF-Token", csrfToken);
566
526
  }
567
- this.xhr.addEventListener("load", function(event) {
568
- return _this.requestDidLoad(event);
569
- });
570
- this.xhr.addEventListener("error", function(event) {
571
- return _this.requestDidError(event);
572
- });
527
+ this.xhr.addEventListener("load", (event => this.requestDidLoad(event)));
528
+ this.xhr.addEventListener("error", (event => this.requestDidError(event)));
573
529
  }
574
- createClass(BlobRecord, [ {
575
- key: "create",
576
- value: function create(callback) {
577
- this.callback = callback;
578
- this.xhr.send(JSON.stringify({
579
- blob: this.attributes
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;
530
+ get status() {
531
+ return this.xhr.status;
532
+ }
533
+ get response() {
534
+ const {responseType: responseType, response: response} = this.xhr;
535
+ if (responseType == "json") {
536
+ return response;
537
+ } else {
538
+ return JSON.parse(response);
609
539
  }
610
- }, {
611
- key: "status",
612
- get: function get$$1() {
613
- return this.xhr.status;
540
+ }
541
+ create(callback) {
542
+ this.callback = callback;
543
+ this.xhr.send(JSON.stringify({
544
+ blob: this.attributes
545
+ }));
546
+ }
547
+ requestDidLoad(event) {
548
+ if (this.status >= 200 && this.status < 300) {
549
+ const {response: response} = this;
550
+ const {direct_upload: direct_upload} = response;
551
+ delete response.direct_upload;
552
+ this.attributes = response;
553
+ this.directUploadData = direct_upload;
554
+ this.callback(null, this.toJSON());
555
+ } else {
556
+ this.requestDidError(event);
614
557
  }
615
- }, {
616
- key: "response",
617
- get: function get$$1() {
618
- var _xhr = this.xhr, responseType = _xhr.responseType, response = _xhr.response;
619
- if (responseType == "json") {
620
- return response;
621
- } else {
622
- return JSON.parse(response);
623
- }
558
+ }
559
+ requestDidError(event) {
560
+ this.callback(`Error creating Blob for "${this.file.name}". Status: ${this.status}`);
561
+ }
562
+ toJSON() {
563
+ const result = {};
564
+ for (const key in this.attributes) {
565
+ result[key] = this.attributes[key];
624
566
  }
625
- } ]);
626
- return BlobRecord;
627
- }();
628
- var BlobUpload = function() {
629
- function BlobUpload(blob) {
630
- var _this = this;
631
- classCallCheck(this, BlobUpload);
567
+ return result;
568
+ }
569
+ }
570
+ class BlobUpload {
571
+ constructor(blob) {
632
572
  this.blob = blob;
633
573
  this.file = blob.file;
634
- var _blob$directUploadDat = blob.directUploadData, url = _blob$directUploadDat.url, headers = _blob$directUploadDat.headers;
635
- this.xhr = new XMLHttpRequest();
574
+ const {url: url, headers: headers} = blob.directUploadData;
575
+ this.xhr = new XMLHttpRequest;
636
576
  this.xhr.open("PUT", url, true);
637
577
  this.xhr.responseType = "text";
638
- for (var key in headers) {
578
+ for (const key in headers) {
639
579
  this.xhr.setRequestHeader(key, headers[key]);
640
580
  }
641
- this.xhr.addEventListener("load", function(event) {
642
- return _this.requestDidLoad(event);
643
- });
644
- this.xhr.addEventListener("error", function(event) {
645
- return _this.requestDidError(event);
646
- });
581
+ this.xhr.addEventListener("load", (event => this.requestDidLoad(event)));
582
+ this.xhr.addEventListener("error", (event => this.requestDidError(event)));
647
583
  }
648
- createClass(BlobUpload, [ {
649
- key: "create",
650
- value: function create(callback) {
651
- this.callback = callback;
652
- this.xhr.send(this.file.slice());
653
- }
654
- }, {
655
- key: "requestDidLoad",
656
- value: function requestDidLoad(event) {
657
- var _xhr = this.xhr, status = _xhr.status, response = _xhr.response;
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);
584
+ create(callback) {
585
+ this.callback = callback;
586
+ this.xhr.send(this.file.slice());
587
+ }
588
+ requestDidLoad(event) {
589
+ const {status: status, response: response} = this.xhr;
590
+ if (status >= 200 && status < 300) {
591
+ this.callback(null, response);
592
+ } else {
593
+ this.requestDidError(event);
668
594
  }
669
- } ]);
670
- return BlobUpload;
671
- }();
672
- var id = 0;
673
- var DirectUpload = function() {
674
- function DirectUpload(file, url, delegate) {
675
- classCallCheck(this, DirectUpload);
595
+ }
596
+ requestDidError(event) {
597
+ this.callback(`Error storing "${this.file.name}". Status: ${this.xhr.status}`);
598
+ }
599
+ }
600
+ let id = 0;
601
+ class DirectUpload {
602
+ constructor(file, url, delegate, customHeaders = {}) {
676
603
  this.id = ++id;
677
604
  this.file = file;
678
605
  this.url = url;
679
606
  this.delegate = delegate;
607
+ this.customHeaders = customHeaders;
680
608
  }
681
- createClass(DirectUpload, [ {
682
- key: "create",
683
- value: function create(callback) {
684
- var _this = this;
685
- FileChecksum.create(this.file, function(error, checksum) {
609
+ create(callback) {
610
+ FileChecksum.create(this.file, ((error, checksum) => {
611
+ if (error) {
612
+ callback(error);
613
+ return;
614
+ }
615
+ const blob = new BlobRecord(this.file, checksum, this.url, this.customHeaders);
616
+ notify(this.delegate, "directUploadWillCreateBlobWithXHR", blob.xhr);
617
+ blob.create((error => {
686
618
  if (error) {
687
619
  callback(error);
688
- return;
620
+ } else {
621
+ const upload = new BlobUpload(blob);
622
+ notify(this.delegate, "directUploadWillStoreFileWithXHR", upload.xhr);
623
+ upload.create((error => {
624
+ if (error) {
625
+ callback(error);
626
+ } else {
627
+ callback(null, blob.toJSON());
628
+ }
629
+ }));
689
630
  }
690
- var blob = new BlobRecord(_this.file, checksum, _this.url);
691
- notify(_this.delegate, "directUploadWillCreateBlobWithXHR", blob.xhr);
692
- blob.create(function(error) {
693
- if (error) {
694
- callback(error);
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) {
631
+ }));
632
+ }));
633
+ }
634
+ }
635
+ function notify(object, methodName, ...messages) {
713
636
  if (object && typeof object[methodName] == "function") {
714
- for (var _len = arguments.length, messages = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
715
- messages[_key - 2] = arguments[_key];
716
- }
717
- return object[methodName].apply(object, messages);
637
+ return object[methodName](...messages);
718
638
  }
719
639
  }
720
- var DirectUploadController = function() {
721
- function DirectUploadController(input, file) {
722
- classCallCheck(this, DirectUploadController);
640
+ class DirectUploadController {
641
+ constructor(input, file) {
723
642
  this.input = input;
724
643
  this.file = file;
725
644
  this.directUpload = new DirectUpload(this.file, this.url, this);
726
645
  this.dispatch("initialize");
727
646
  }
728
- createClass(DirectUploadController, [ {
729
- key: "start",
730
- value: function start(callback) {
731
- var _this = this;
732
- var hiddenInput = document.createElement("input");
733
- hiddenInput.type = "hidden";
734
- hiddenInput.name = this.input.name;
735
- this.input.insertAdjacentElement("beforebegin", hiddenInput);
736
- this.dispatch("start");
737
- this.directUpload.create(function(error, attributes) {
738
- if (error) {
739
- hiddenInput.parentNode.removeChild(hiddenInput);
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);
647
+ start(callback) {
648
+ const hiddenInput = document.createElement("input");
649
+ hiddenInput.type = "hidden";
650
+ hiddenInput.name = this.input.name;
651
+ this.input.insertAdjacentElement("beforebegin", hiddenInput);
652
+ this.dispatch("start");
653
+ this.directUpload.create(((error, attributes) => {
654
+ if (error) {
655
+ hiddenInput.parentNode.removeChild(hiddenInput);
656
+ this.dispatchError(error);
657
+ } else {
658
+ hiddenInput.value = attributes.signed_id;
776
659
  }
777
- }
778
- }, {
779
- key: "directUploadWillCreateBlobWithXHR",
780
- value: function directUploadWillCreateBlobWithXHR(xhr) {
781
- this.dispatch("before-blob-request", {
782
- xhr: xhr
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);
660
+ this.dispatch("end");
661
+ callback(error);
662
+ }));
663
+ }
664
+ uploadRequestDidProgress(event) {
665
+ const progress = event.loaded / event.total * 100;
666
+ if (progress) {
667
+ this.dispatch("progress", {
668
+ progress: progress
794
669
  });
795
670
  }
796
- }, {
797
- key: "url",
798
- get: function get$$1() {
799
- return this.input.getAttribute("data-direct-upload-url");
671
+ }
672
+ get url() {
673
+ return this.input.getAttribute("data-direct-upload-url");
674
+ }
675
+ dispatch(name, detail = {}) {
676
+ detail.file = this.file;
677
+ detail.id = this.directUpload.id;
678
+ return dispatchEvent(this.input, `direct-upload:${name}`, {
679
+ detail: detail
680
+ });
681
+ }
682
+ dispatchError(error) {
683
+ const event = this.dispatch("error", {
684
+ error: error
685
+ });
686
+ if (!event.defaultPrevented) {
687
+ alert(error);
800
688
  }
801
- } ]);
802
- return DirectUploadController;
803
- }();
804
- var inputSelector = "input[type=file][data-direct-upload-url]:not([disabled])";
805
- var DirectUploadsController = function() {
806
- function DirectUploadsController(form) {
807
- classCallCheck(this, DirectUploadsController);
689
+ }
690
+ directUploadWillCreateBlobWithXHR(xhr) {
691
+ this.dispatch("before-blob-request", {
692
+ xhr: xhr
693
+ });
694
+ }
695
+ directUploadWillStoreFileWithXHR(xhr) {
696
+ this.dispatch("before-storage-request", {
697
+ xhr: xhr
698
+ });
699
+ xhr.upload.addEventListener("progress", (event => this.uploadRequestDidProgress(event)));
700
+ }
701
+ }
702
+ const inputSelector = "input[type=file][data-direct-upload-url]:not([disabled])";
703
+ class DirectUploadsController {
704
+ constructor(form) {
808
705
  this.form = form;
809
- this.inputs = findElements(form, inputSelector).filter(function(input) {
810
- return input.files.length;
706
+ this.inputs = findElements(form, inputSelector).filter((input => input.files.length));
707
+ }
708
+ start(callback) {
709
+ const controllers = this.createDirectUploadControllers();
710
+ const startNextController = () => {
711
+ const controller = controllers.shift();
712
+ if (controller) {
713
+ controller.start((error => {
714
+ if (error) {
715
+ callback(error);
716
+ this.dispatch("end");
717
+ } else {
718
+ startNextController();
719
+ }
720
+ }));
721
+ } else {
722
+ callback();
723
+ this.dispatch("end");
724
+ }
725
+ };
726
+ this.dispatch("start");
727
+ startNextController();
728
+ }
729
+ createDirectUploadControllers() {
730
+ const controllers = [];
731
+ this.inputs.forEach((input => {
732
+ toArray(input.files).forEach((file => {
733
+ const controller = new DirectUploadController(input, file);
734
+ controllers.push(controller);
735
+ }));
736
+ }));
737
+ return controllers;
738
+ }
739
+ dispatch(name, detail = {}) {
740
+ return dispatchEvent(this.form, `direct-uploads:${name}`, {
741
+ detail: detail
811
742
  });
812
743
  }
813
- createClass(DirectUploadsController, [ {
814
- key: "start",
815
- value: function start(callback) {
816
- var _this = this;
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;
744
+ }
745
+ const processingAttribute = "data-direct-uploads-processing";
746
+ const submitButtonsByForm = new WeakMap;
747
+ let started = false;
863
748
  function start() {
864
749
  if (!started) {
865
750
  started = true;
866
751
  document.addEventListener("click", didClick, true);
867
- document.addEventListener("submit", didSubmitForm);
752
+ document.addEventListener("submit", didSubmitForm, true);
868
753
  document.addEventListener("ajax:before", didSubmitRemoteElement);
869
754
  }
870
755
  }
871
756
  function didClick(event) {
872
- var target = event.target;
757
+ const {target: target} = event;
873
758
  if ((target.tagName == "INPUT" || target.tagName == "BUTTON") && target.type == "submit" && target.form) {
874
759
  submitButtonsByForm.set(target.form, target);
875
760
  }
@@ -883,31 +768,31 @@
883
768
  }
884
769
  }
885
770
  function handleFormSubmissionEvent(event) {
886
- var form = event.target;
771
+ const form = event.target;
887
772
  if (form.hasAttribute(processingAttribute)) {
888
773
  event.preventDefault();
889
774
  return;
890
775
  }
891
- var controller = new DirectUploadsController(form);
892
- var inputs = controller.inputs;
776
+ const controller = new DirectUploadsController(form);
777
+ const {inputs: inputs} = controller;
893
778
  if (inputs.length) {
894
779
  event.preventDefault();
895
780
  form.setAttribute(processingAttribute, "");
896
781
  inputs.forEach(disable);
897
- controller.start(function(error) {
782
+ controller.start((error => {
898
783
  form.removeAttribute(processingAttribute);
899
784
  if (error) {
900
785
  inputs.forEach(enable);
901
786
  } else {
902
787
  submitForm(form);
903
788
  }
904
- });
789
+ }));
905
790
  }
906
791
  }
907
792
  function submitForm(form) {
908
- var button = submitButtonsByForm.get(form) || findElement(form, "input[type=submit], button[type=submit]");
793
+ let button = submitButtonsByForm.get(form) || findElement(form, "input[type=submit], button[type=submit]");
909
794
  if (button) {
910
- var _button = button, disabled = _button.disabled;
795
+ const {disabled: disabled} = button;
911
796
  button.disabled = false;
912
797
  button.focus();
913
798
  button.click();
@@ -934,9 +819,11 @@
934
819
  }
935
820
  }
936
821
  setTimeout(autostart, 1);
937
- exports.start = start;
938
822
  exports.DirectUpload = DirectUpload;
823
+ exports.DirectUploadController = DirectUploadController;
824
+ exports.DirectUploadsController = DirectUploadsController;
825
+ exports.start = start;
939
826
  Object.defineProperty(exports, "__esModule", {
940
827
  value: true
941
828
  });
942
- });
829
+ }));