activestorage 6.1.4.1 → 7.0.0.rc2
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 +144 -204
- data/MIT-LICENSE +1 -1
- data/README.md +25 -11
- data/app/assets/javascripts/activestorage.esm.js +856 -0
- data/app/assets/javascripts/activestorage.js +270 -377
- data/app/controllers/active_storage/base_controller.rb +1 -10
- data/app/controllers/active_storage/blobs/proxy_controller.rb +14 -4
- data/app/controllers/active_storage/blobs/redirect_controller.rb +6 -4
- data/app/controllers/active_storage/direct_uploads_controller.rb +7 -1
- data/app/controllers/active_storage/disk_controller.rb +1 -0
- data/app/controllers/active_storage/representations/base_controller.rb +5 -1
- data/app/controllers/active_storage/representations/proxy_controller.rb +6 -4
- 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/blob_record.js +10 -3
- data/app/javascript/activestorage/direct_upload.js +4 -2
- data/app/javascript/activestorage/direct_upload_controller.js +9 -1
- data/app/javascript/activestorage/ujs.js +1 -1
- data/app/models/active_storage/attachment.rb +36 -3
- data/app/models/active_storage/blob/representable.rb +7 -5
- data/app/models/active_storage/blob.rb +92 -36
- 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 +3 -6
- 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 +2 -2
- data/config/routes.rb +10 -10
- data/db/migrate/20170806125915_create_active_storage_tables.rb +32 -11
- data/db/update_migrate/20191206030411_create_active_storage_variant_records.rb +15 -2
- data/db/update_migrate/20211119233751_remove_not_null_on_active_storage_blobs_checksum.rb +5 -0
- 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/direct_upload_token.rb +59 -0
- data/lib/active_storage/downloader.rb +4 -4
- data/lib/active_storage/engine.rb +30 -1
- data/lib/active_storage/errors.rb +3 -0
- data/lib/active_storage/fixture_set.rb +76 -0
- data/lib/active_storage/gem_version.rb +4 -4
- 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 +28 -6
- data/lib/active_storage/service/configurator.rb +1 -1
- data/lib/active_storage/service/disk_service.rb +24 -19
- data/lib/active_storage/service/gcs_service.rb +109 -11
- data/lib/active_storage/service/mirror_service.rb +2 -2
- data/lib/active_storage/service/registry.rb +1 -1
- data/lib/active_storage/service/s3_service.rb +37 -15
- data/lib/active_storage/service.rb +13 -5
- data/lib/active_storage/transformers/image_processing_transformer.rb +1 -1
- data/lib/active_storage/transformers/transformer.rb +1 -1
- data/lib/active_storage.rb +6 -1
- metadata +31 -19
- 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, directUploadToken, attachmentName) {
|
550
507
|
this.file = file;
|
551
508
|
this.attributes = {
|
552
509
|
filename: file.name,
|
@@ -554,322 +511,258 @@
|
|
554
511
|
byte_size: file.size,
|
555
512
|
checksum: checksum
|
556
513
|
};
|
557
|
-
this.
|
514
|
+
this.directUploadToken = directUploadToken;
|
515
|
+
this.attachmentName = attachmentName;
|
516
|
+
this.xhr = new XMLHttpRequest;
|
558
517
|
this.xhr.open("POST", url, true);
|
559
518
|
this.xhr.responseType = "json";
|
560
519
|
this.xhr.setRequestHeader("Content-Type", "application/json");
|
561
520
|
this.xhr.setRequestHeader("Accept", "application/json");
|
562
521
|
this.xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
|
563
|
-
|
522
|
+
const csrfToken = getMetaValue("csrf-token");
|
564
523
|
if (csrfToken != undefined) {
|
565
524
|
this.xhr.setRequestHeader("X-CSRF-Token", csrfToken);
|
566
525
|
}
|
567
|
-
this.xhr.addEventListener("load",
|
568
|
-
|
569
|
-
});
|
570
|
-
this.xhr.addEventListener("error", function(event) {
|
571
|
-
return _this.requestDidError(event);
|
572
|
-
});
|
526
|
+
this.xhr.addEventListener("load", (event => this.requestDidLoad(event)));
|
527
|
+
this.xhr.addEventListener("error", (event => this.requestDidError(event)));
|
573
528
|
}
|
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;
|
529
|
+
get status() {
|
530
|
+
return this.xhr.status;
|
531
|
+
}
|
532
|
+
get response() {
|
533
|
+
const {responseType: responseType, response: response} = this.xhr;
|
534
|
+
if (responseType == "json") {
|
535
|
+
return response;
|
536
|
+
} else {
|
537
|
+
return JSON.parse(response);
|
609
538
|
}
|
610
|
-
}
|
611
|
-
|
612
|
-
|
613
|
-
|
539
|
+
}
|
540
|
+
create(callback) {
|
541
|
+
this.callback = callback;
|
542
|
+
this.xhr.send(JSON.stringify({
|
543
|
+
blob: this.attributes,
|
544
|
+
direct_upload_token: this.directUploadToken,
|
545
|
+
attachment_name: this.attachmentName
|
546
|
+
}));
|
547
|
+
}
|
548
|
+
requestDidLoad(event) {
|
549
|
+
if (this.status >= 200 && this.status < 300) {
|
550
|
+
const {response: response} = this;
|
551
|
+
const {direct_upload: direct_upload} = response;
|
552
|
+
delete response.direct_upload;
|
553
|
+
this.attributes = response;
|
554
|
+
this.directUploadData = direct_upload;
|
555
|
+
this.callback(null, this.toJSON());
|
556
|
+
} else {
|
557
|
+
this.requestDidError(event);
|
614
558
|
}
|
615
|
-
}
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
}
|
559
|
+
}
|
560
|
+
requestDidError(event) {
|
561
|
+
this.callback(`Error creating Blob for "${this.file.name}". Status: ${this.status}`);
|
562
|
+
}
|
563
|
+
toJSON() {
|
564
|
+
const result = {};
|
565
|
+
for (const key in this.attributes) {
|
566
|
+
result[key] = this.attributes[key];
|
624
567
|
}
|
625
|
-
|
626
|
-
|
627
|
-
}
|
628
|
-
|
629
|
-
|
630
|
-
var _this = this;
|
631
|
-
classCallCheck(this, BlobUpload);
|
568
|
+
return result;
|
569
|
+
}
|
570
|
+
}
|
571
|
+
class BlobUpload {
|
572
|
+
constructor(blob) {
|
632
573
|
this.blob = blob;
|
633
574
|
this.file = blob.file;
|
634
|
-
|
635
|
-
this.xhr = new XMLHttpRequest
|
575
|
+
const {url: url, headers: headers} = blob.directUploadData;
|
576
|
+
this.xhr = new XMLHttpRequest;
|
636
577
|
this.xhr.open("PUT", url, true);
|
637
578
|
this.xhr.responseType = "text";
|
638
|
-
for (
|
579
|
+
for (const key in headers) {
|
639
580
|
this.xhr.setRequestHeader(key, headers[key]);
|
640
581
|
}
|
641
|
-
this.xhr.addEventListener("load",
|
642
|
-
|
643
|
-
});
|
644
|
-
this.xhr.addEventListener("error", function(event) {
|
645
|
-
return _this.requestDidError(event);
|
646
|
-
});
|
582
|
+
this.xhr.addEventListener("load", (event => this.requestDidLoad(event)));
|
583
|
+
this.xhr.addEventListener("error", (event => this.requestDidError(event)));
|
647
584
|
}
|
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);
|
585
|
+
create(callback) {
|
586
|
+
this.callback = callback;
|
587
|
+
this.xhr.send(this.file.slice());
|
588
|
+
}
|
589
|
+
requestDidLoad(event) {
|
590
|
+
const {status: status, response: response} = this.xhr;
|
591
|
+
if (status >= 200 && status < 300) {
|
592
|
+
this.callback(null, response);
|
593
|
+
} else {
|
594
|
+
this.requestDidError(event);
|
668
595
|
}
|
669
|
-
}
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
596
|
+
}
|
597
|
+
requestDidError(event) {
|
598
|
+
this.callback(`Error storing "${this.file.name}". Status: ${this.xhr.status}`);
|
599
|
+
}
|
600
|
+
}
|
601
|
+
let id = 0;
|
602
|
+
class DirectUpload {
|
603
|
+
constructor(file, url, serviceName, attachmentName, delegate) {
|
676
604
|
this.id = ++id;
|
677
605
|
this.file = file;
|
678
606
|
this.url = url;
|
607
|
+
this.serviceName = serviceName;
|
608
|
+
this.attachmentName = attachmentName;
|
679
609
|
this.delegate = delegate;
|
680
610
|
}
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
611
|
+
create(callback) {
|
612
|
+
FileChecksum.create(this.file, ((error, checksum) => {
|
613
|
+
if (error) {
|
614
|
+
callback(error);
|
615
|
+
return;
|
616
|
+
}
|
617
|
+
const blob = new BlobRecord(this.file, checksum, this.url, this.serviceName, this.attachmentName);
|
618
|
+
notify(this.delegate, "directUploadWillCreateBlobWithXHR", blob.xhr);
|
619
|
+
blob.create((error => {
|
686
620
|
if (error) {
|
687
621
|
callback(error);
|
688
|
-
|
622
|
+
} else {
|
623
|
+
const upload = new BlobUpload(blob);
|
624
|
+
notify(this.delegate, "directUploadWillStoreFileWithXHR", upload.xhr);
|
625
|
+
upload.create((error => {
|
626
|
+
if (error) {
|
627
|
+
callback(error);
|
628
|
+
} else {
|
629
|
+
callback(null, blob.toJSON());
|
630
|
+
}
|
631
|
+
}));
|
689
632
|
}
|
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) {
|
633
|
+
}));
|
634
|
+
}));
|
635
|
+
}
|
636
|
+
}
|
637
|
+
function notify(object, methodName, ...messages) {
|
713
638
|
if (object && typeof object[methodName] == "function") {
|
714
|
-
|
715
|
-
messages[_key - 2] = arguments[_key];
|
716
|
-
}
|
717
|
-
return object[methodName].apply(object, messages);
|
639
|
+
return object[methodName](...messages);
|
718
640
|
}
|
719
641
|
}
|
720
|
-
|
721
|
-
|
722
|
-
classCallCheck(this, DirectUploadController);
|
642
|
+
class DirectUploadController {
|
643
|
+
constructor(input, file) {
|
723
644
|
this.input = input;
|
724
645
|
this.file = file;
|
725
|
-
this.directUpload = new DirectUpload(this.file, this.url, this);
|
646
|
+
this.directUpload = new DirectUpload(this.file, this.url, this.directUploadToken, this.attachmentName, this);
|
726
647
|
this.dispatch("initialize");
|
727
648
|
}
|
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);
|
649
|
+
start(callback) {
|
650
|
+
const hiddenInput = document.createElement("input");
|
651
|
+
hiddenInput.type = "hidden";
|
652
|
+
hiddenInput.name = this.input.name;
|
653
|
+
this.input.insertAdjacentElement("beforebegin", hiddenInput);
|
654
|
+
this.dispatch("start");
|
655
|
+
this.directUpload.create(((error, attributes) => {
|
656
|
+
if (error) {
|
657
|
+
hiddenInput.parentNode.removeChild(hiddenInput);
|
658
|
+
this.dispatchError(error);
|
659
|
+
} else {
|
660
|
+
hiddenInput.value = attributes.signed_id;
|
776
661
|
}
|
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);
|
662
|
+
this.dispatch("end");
|
663
|
+
callback(error);
|
664
|
+
}));
|
665
|
+
}
|
666
|
+
uploadRequestDidProgress(event) {
|
667
|
+
const progress = event.loaded / event.total * 100;
|
668
|
+
if (progress) {
|
669
|
+
this.dispatch("progress", {
|
670
|
+
progress: progress
|
794
671
|
});
|
795
672
|
}
|
796
|
-
}
|
797
|
-
|
798
|
-
|
799
|
-
|
673
|
+
}
|
674
|
+
get url() {
|
675
|
+
return this.input.getAttribute("data-direct-upload-url");
|
676
|
+
}
|
677
|
+
get directUploadToken() {
|
678
|
+
return this.input.getAttribute("data-direct-upload-token");
|
679
|
+
}
|
680
|
+
get attachmentName() {
|
681
|
+
return this.input.getAttribute("data-direct-upload-attachment-name");
|
682
|
+
}
|
683
|
+
dispatch(name, detail = {}) {
|
684
|
+
detail.file = this.file;
|
685
|
+
detail.id = this.directUpload.id;
|
686
|
+
return dispatchEvent(this.input, `direct-upload:${name}`, {
|
687
|
+
detail: detail
|
688
|
+
});
|
689
|
+
}
|
690
|
+
dispatchError(error) {
|
691
|
+
const event = this.dispatch("error", {
|
692
|
+
error: error
|
693
|
+
});
|
694
|
+
if (!event.defaultPrevented) {
|
695
|
+
alert(error);
|
800
696
|
}
|
801
|
-
}
|
802
|
-
|
803
|
-
|
804
|
-
|
805
|
-
|
806
|
-
|
807
|
-
|
697
|
+
}
|
698
|
+
directUploadWillCreateBlobWithXHR(xhr) {
|
699
|
+
this.dispatch("before-blob-request", {
|
700
|
+
xhr: xhr
|
701
|
+
});
|
702
|
+
}
|
703
|
+
directUploadWillStoreFileWithXHR(xhr) {
|
704
|
+
this.dispatch("before-storage-request", {
|
705
|
+
xhr: xhr
|
706
|
+
});
|
707
|
+
xhr.upload.addEventListener("progress", (event => this.uploadRequestDidProgress(event)));
|
708
|
+
}
|
709
|
+
}
|
710
|
+
const inputSelector = "input[type=file][data-direct-upload-url]:not([disabled])";
|
711
|
+
class DirectUploadsController {
|
712
|
+
constructor(form) {
|
808
713
|
this.form = form;
|
809
|
-
this.inputs = findElements(form, inputSelector).filter(
|
810
|
-
|
714
|
+
this.inputs = findElements(form, inputSelector).filter((input => input.files.length));
|
715
|
+
}
|
716
|
+
start(callback) {
|
717
|
+
const controllers = this.createDirectUploadControllers();
|
718
|
+
const startNextController = () => {
|
719
|
+
const controller = controllers.shift();
|
720
|
+
if (controller) {
|
721
|
+
controller.start((error => {
|
722
|
+
if (error) {
|
723
|
+
callback(error);
|
724
|
+
this.dispatch("end");
|
725
|
+
} else {
|
726
|
+
startNextController();
|
727
|
+
}
|
728
|
+
}));
|
729
|
+
} else {
|
730
|
+
callback();
|
731
|
+
this.dispatch("end");
|
732
|
+
}
|
733
|
+
};
|
734
|
+
this.dispatch("start");
|
735
|
+
startNextController();
|
736
|
+
}
|
737
|
+
createDirectUploadControllers() {
|
738
|
+
const controllers = [];
|
739
|
+
this.inputs.forEach((input => {
|
740
|
+
toArray(input.files).forEach((file => {
|
741
|
+
const controller = new DirectUploadController(input, file);
|
742
|
+
controllers.push(controller);
|
743
|
+
}));
|
744
|
+
}));
|
745
|
+
return controllers;
|
746
|
+
}
|
747
|
+
dispatch(name, detail = {}) {
|
748
|
+
return dispatchEvent(this.form, `direct-uploads:${name}`, {
|
749
|
+
detail: detail
|
811
750
|
});
|
812
751
|
}
|
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;
|
752
|
+
}
|
753
|
+
const processingAttribute = "data-direct-uploads-processing";
|
754
|
+
const submitButtonsByForm = new WeakMap;
|
755
|
+
let started = false;
|
863
756
|
function start() {
|
864
757
|
if (!started) {
|
865
758
|
started = true;
|
866
759
|
document.addEventListener("click", didClick, true);
|
867
|
-
document.addEventListener("submit", didSubmitForm);
|
760
|
+
document.addEventListener("submit", didSubmitForm, true);
|
868
761
|
document.addEventListener("ajax:before", didSubmitRemoteElement);
|
869
762
|
}
|
870
763
|
}
|
871
764
|
function didClick(event) {
|
872
|
-
|
765
|
+
const {target: target} = event;
|
873
766
|
if ((target.tagName == "INPUT" || target.tagName == "BUTTON") && target.type == "submit" && target.form) {
|
874
767
|
submitButtonsByForm.set(target.form, target);
|
875
768
|
}
|
@@ -883,31 +776,31 @@
|
|
883
776
|
}
|
884
777
|
}
|
885
778
|
function handleFormSubmissionEvent(event) {
|
886
|
-
|
779
|
+
const form = event.target;
|
887
780
|
if (form.hasAttribute(processingAttribute)) {
|
888
781
|
event.preventDefault();
|
889
782
|
return;
|
890
783
|
}
|
891
|
-
|
892
|
-
|
784
|
+
const controller = new DirectUploadsController(form);
|
785
|
+
const {inputs: inputs} = controller;
|
893
786
|
if (inputs.length) {
|
894
787
|
event.preventDefault();
|
895
788
|
form.setAttribute(processingAttribute, "");
|
896
789
|
inputs.forEach(disable);
|
897
|
-
controller.start(
|
790
|
+
controller.start((error => {
|
898
791
|
form.removeAttribute(processingAttribute);
|
899
792
|
if (error) {
|
900
793
|
inputs.forEach(enable);
|
901
794
|
} else {
|
902
795
|
submitForm(form);
|
903
796
|
}
|
904
|
-
});
|
797
|
+
}));
|
905
798
|
}
|
906
799
|
}
|
907
800
|
function submitForm(form) {
|
908
|
-
|
801
|
+
let button = submitButtonsByForm.get(form) || findElement(form, "input[type=submit], button[type=submit]");
|
909
802
|
if (button) {
|
910
|
-
|
803
|
+
const {disabled: disabled} = button;
|
911
804
|
button.disabled = false;
|
912
805
|
button.focus();
|
913
806
|
button.click();
|
@@ -934,9 +827,9 @@
|
|
934
827
|
}
|
935
828
|
}
|
936
829
|
setTimeout(autostart, 1);
|
937
|
-
exports.start = start;
|
938
830
|
exports.DirectUpload = DirectUpload;
|
831
|
+
exports.start = start;
|
939
832
|
Object.defineProperty(exports, "__esModule", {
|
940
833
|
value: true
|
941
834
|
});
|
942
|
-
});
|
835
|
+
}));
|