activestorage 6.1.7.6 → 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.

Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +126 -311
  3. data/MIT-LICENSE +1 -1
  4. data/README.md +25 -11
  5. data/app/assets/javascripts/activestorage.esm.js +844 -0
  6. data/app/assets/javascripts/activestorage.js +257 -376
  7. data/app/controllers/active_storage/base_controller.rb +1 -10
  8. data/app/controllers/active_storage/blobs/proxy_controller.rb +14 -4
  9. data/app/controllers/active_storage/blobs/redirect_controller.rb +6 -4
  10. data/app/controllers/active_storage/representations/base_controller.rb +5 -1
  11. data/app/controllers/active_storage/representations/proxy_controller.rb +6 -4
  12. data/app/controllers/active_storage/representations/redirect_controller.rb +6 -4
  13. data/app/controllers/concerns/active_storage/set_blob.rb +6 -2
  14. data/app/controllers/concerns/active_storage/set_current.rb +3 -3
  15. data/app/controllers/concerns/active_storage/streaming.rb +65 -0
  16. data/app/javascript/activestorage/ujs.js +1 -1
  17. data/app/models/active_storage/attachment.rb +35 -2
  18. data/app/models/active_storage/blob/representable.rb +7 -5
  19. data/app/models/active_storage/blob.rb +26 -27
  20. data/app/models/active_storage/current.rb +12 -2
  21. data/app/models/active_storage/preview.rb +6 -4
  22. data/app/models/active_storage/record.rb +1 -1
  23. data/app/models/active_storage/variant.rb +6 -9
  24. data/app/models/active_storage/variant_record.rb +2 -0
  25. data/app/models/active_storage/variant_with_record.rb +9 -5
  26. data/app/models/active_storage/variation.rb +3 -3
  27. data/config/routes.rb +10 -10
  28. data/db/migrate/20170806125915_create_active_storage_tables.rb +11 -2
  29. data/db/update_migrate/20190112182829_add_service_name_to_active_storage_blobs.rb +0 -4
  30. data/db/update_migrate/20191206030411_create_active_storage_variant_records.rb +1 -3
  31. data/lib/active_storage/analyzer/audio_analyzer.rb +65 -0
  32. data/lib/active_storage/analyzer/image_analyzer/image_magick.rb +39 -0
  33. data/lib/active_storage/analyzer/image_analyzer/vips.rb +49 -0
  34. data/lib/active_storage/analyzer/image_analyzer.rb +2 -30
  35. data/lib/active_storage/analyzer/video_analyzer.rb +26 -11
  36. data/lib/active_storage/analyzer.rb +8 -4
  37. data/lib/active_storage/attached/changes/create_many.rb +7 -3
  38. data/lib/active_storage/attached/changes/create_one.rb +1 -1
  39. data/lib/active_storage/attached/changes/create_one_of_many.rb +1 -1
  40. data/lib/active_storage/attached/changes/delete_many.rb +1 -1
  41. data/lib/active_storage/attached/changes/delete_one.rb +1 -1
  42. data/lib/active_storage/attached/changes/detach_many.rb +18 -0
  43. data/lib/active_storage/attached/changes/detach_one.rb +24 -0
  44. data/lib/active_storage/attached/changes/purge_many.rb +27 -0
  45. data/lib/active_storage/attached/changes/purge_one.rb +27 -0
  46. data/lib/active_storage/attached/changes.rb +7 -1
  47. data/lib/active_storage/attached/many.rb +27 -15
  48. data/lib/active_storage/attached/model.rb +31 -5
  49. data/lib/active_storage/attached/one.rb +32 -27
  50. data/lib/active_storage/downloader.rb +2 -2
  51. data/lib/active_storage/engine.rb +28 -16
  52. data/lib/active_storage/fixture_set.rb +76 -0
  53. data/lib/active_storage/gem_version.rb +4 -4
  54. data/lib/active_storage/previewer/video_previewer.rb +0 -2
  55. data/lib/active_storage/previewer.rb +4 -4
  56. data/lib/active_storage/reflection.rb +12 -2
  57. data/lib/active_storage/service/azure_storage_service.rb +1 -1
  58. data/lib/active_storage/service/configurator.rb +1 -1
  59. data/lib/active_storage/service/disk_service.rb +13 -18
  60. data/lib/active_storage/service/gcs_service.rb +91 -7
  61. data/lib/active_storage/service/mirror_service.rb +1 -1
  62. data/lib/active_storage/service/registry.rb +1 -1
  63. data/lib/active_storage/service/s3_service.rb +4 -4
  64. data/lib/active_storage/service.rb +3 -3
  65. data/lib/active_storage/transformers/image_processing_transformer.rb +1 -66
  66. data/lib/active_storage/transformers/transformer.rb +1 -1
  67. data/lib/active_storage.rb +3 -292
  68. metadata +32 -24
  69. 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) {
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
- var csrfToken = getMetaValue("csrf-token");
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", function(event) {
568
- return _this.requestDidLoad(event);
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
- 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;
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
- key: "status",
612
- get: function get$$1() {
613
- return this.xhr.status;
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
- 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
- }
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
- return BlobRecord;
627
- }();
628
- var BlobUpload = function() {
629
- function BlobUpload(blob) {
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
- var _blob$directUploadDat = blob.directUploadData, url = _blob$directUploadDat.url, headers = _blob$directUploadDat.headers;
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 (var key in headers) {
575
+ for (const key in headers) {
639
576
  this.xhr.setRequestHeader(key, headers[key]);
640
577
  }
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
- });
578
+ this.xhr.addEventListener("load", (event => this.requestDidLoad(event)));
579
+ this.xhr.addEventListener("error", (event => this.requestDidError(event)));
647
580
  }
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);
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
- return BlobUpload;
671
- }();
672
- var id = 0;
673
- var DirectUpload = function() {
674
- function DirectUpload(file, url, delegate) {
675
- classCallCheck(this, DirectUpload);
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
- createClass(DirectUpload, [ {
682
- key: "create",
683
- value: function create(callback) {
684
- var _this = this;
685
- FileChecksum.create(this.file, function(error, checksum) {
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
- return;
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
- 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) {
627
+ }));
628
+ }));
629
+ }
630
+ }
631
+ function notify(object, methodName, ...messages) {
713
632
  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);
633
+ return object[methodName](...messages);
718
634
  }
719
635
  }
720
- var DirectUploadController = function() {
721
- function DirectUploadController(input, file) {
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
- 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);
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
- 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);
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
- key: "url",
798
- get: function get$$1() {
799
- return this.input.getAttribute("data-direct-upload-url");
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
- 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);
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(function(input) {
810
- return input.files.length;
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
- 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;
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
- var target = event.target;
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
- var form = event.target;
767
+ const form = event.target;
887
768
  if (form.hasAttribute(processingAttribute)) {
888
769
  event.preventDefault();
889
770
  return;
890
771
  }
891
- var controller = new DirectUploadsController(form);
892
- var inputs = controller.inputs;
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(function(error) {
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
- var button = submitButtonsByForm.get(form) || findElement(form, "input[type=submit], button[type=submit]");
789
+ let button = submitButtonsByForm.get(form) || findElement(form, "input[type=submit], button[type=submit]");
909
790
  if (button) {
910
- var _button = button, disabled = _button.disabled;
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
+ }));