activestorage 6.0.0

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 (76) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +198 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.md +162 -0
  5. data/app/assets/javascripts/activestorage.js +942 -0
  6. data/app/controllers/active_storage/base_controller.rb +8 -0
  7. data/app/controllers/active_storage/blobs_controller.rb +14 -0
  8. data/app/controllers/active_storage/direct_uploads_controller.rb +23 -0
  9. data/app/controllers/active_storage/disk_controller.rb +66 -0
  10. data/app/controllers/active_storage/representations_controller.rb +14 -0
  11. data/app/controllers/concerns/active_storage/set_blob.rb +16 -0
  12. data/app/controllers/concerns/active_storage/set_current.rb +15 -0
  13. data/app/javascript/activestorage/blob_record.js +73 -0
  14. data/app/javascript/activestorage/blob_upload.js +35 -0
  15. data/app/javascript/activestorage/direct_upload.js +48 -0
  16. data/app/javascript/activestorage/direct_upload_controller.js +67 -0
  17. data/app/javascript/activestorage/direct_uploads_controller.js +50 -0
  18. data/app/javascript/activestorage/file_checksum.js +53 -0
  19. data/app/javascript/activestorage/helpers.js +51 -0
  20. data/app/javascript/activestorage/index.js +11 -0
  21. data/app/javascript/activestorage/ujs.js +86 -0
  22. data/app/jobs/active_storage/analyze_job.rb +12 -0
  23. data/app/jobs/active_storage/base_job.rb +4 -0
  24. data/app/jobs/active_storage/purge_job.rb +13 -0
  25. data/app/models/active_storage/attachment.rb +50 -0
  26. data/app/models/active_storage/blob.rb +278 -0
  27. data/app/models/active_storage/blob/analyzable.rb +57 -0
  28. data/app/models/active_storage/blob/identifiable.rb +31 -0
  29. data/app/models/active_storage/blob/representable.rb +93 -0
  30. data/app/models/active_storage/current.rb +5 -0
  31. data/app/models/active_storage/filename.rb +77 -0
  32. data/app/models/active_storage/preview.rb +89 -0
  33. data/app/models/active_storage/variant.rb +131 -0
  34. data/app/models/active_storage/variation.rb +80 -0
  35. data/config/routes.rb +32 -0
  36. data/db/migrate/20170806125915_create_active_storage_tables.rb +26 -0
  37. data/db/update_migrate/20180723000244_add_foreign_key_constraint_to_active_storage_attachments_for_blob_id.rb +9 -0
  38. data/lib/active_storage.rb +73 -0
  39. data/lib/active_storage/analyzer.rb +38 -0
  40. data/lib/active_storage/analyzer/image_analyzer.rb +52 -0
  41. data/lib/active_storage/analyzer/null_analyzer.rb +13 -0
  42. data/lib/active_storage/analyzer/video_analyzer.rb +118 -0
  43. data/lib/active_storage/attached.rb +25 -0
  44. data/lib/active_storage/attached/changes.rb +16 -0
  45. data/lib/active_storage/attached/changes/create_many.rb +46 -0
  46. data/lib/active_storage/attached/changes/create_one.rb +69 -0
  47. data/lib/active_storage/attached/changes/create_one_of_many.rb +10 -0
  48. data/lib/active_storage/attached/changes/delete_many.rb +27 -0
  49. data/lib/active_storage/attached/changes/delete_one.rb +19 -0
  50. data/lib/active_storage/attached/many.rb +65 -0
  51. data/lib/active_storage/attached/model.rb +147 -0
  52. data/lib/active_storage/attached/one.rb +79 -0
  53. data/lib/active_storage/downloader.rb +43 -0
  54. data/lib/active_storage/downloading.rb +47 -0
  55. data/lib/active_storage/engine.rb +149 -0
  56. data/lib/active_storage/errors.rb +26 -0
  57. data/lib/active_storage/gem_version.rb +17 -0
  58. data/lib/active_storage/log_subscriber.rb +58 -0
  59. data/lib/active_storage/previewer.rb +84 -0
  60. data/lib/active_storage/previewer/mupdf_previewer.rb +36 -0
  61. data/lib/active_storage/previewer/poppler_pdf_previewer.rb +35 -0
  62. data/lib/active_storage/previewer/video_previewer.rb +26 -0
  63. data/lib/active_storage/reflection.rb +64 -0
  64. data/lib/active_storage/service.rb +141 -0
  65. data/lib/active_storage/service/azure_storage_service.rb +165 -0
  66. data/lib/active_storage/service/configurator.rb +34 -0
  67. data/lib/active_storage/service/disk_service.rb +166 -0
  68. data/lib/active_storage/service/gcs_service.rb +141 -0
  69. data/lib/active_storage/service/mirror_service.rb +55 -0
  70. data/lib/active_storage/service/s3_service.rb +116 -0
  71. data/lib/active_storage/transformers/image_processing_transformer.rb +39 -0
  72. data/lib/active_storage/transformers/mini_magick_transformer.rb +38 -0
  73. data/lib/active_storage/transformers/transformer.rb +42 -0
  74. data/lib/active_storage/version.rb +10 -0
  75. data/lib/tasks/activestorage.rake +22 -0
  76. metadata +174 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 716b00e8dcc27ec9df016cff558ecce99f31470d58bbc7efe3a495b7ab272722
4
+ data.tar.gz: 2986c8c1b288f85025be8fc2f768c6c4370c36b593a81ff68599e1d3e691d7b7
5
+ SHA512:
6
+ metadata.gz: c51640d41c7223552bdf418f1993af9fc49f73c24f002323e7deefcb8610dfddad022a566fb60a8c608081ae39d563e5940f092c291304c8074a0f8d0572120a
7
+ data.tar.gz: c3c6b78ab356d557188ddd05dd8c033bdb2e55b7bde8c0443d0d9b4373d8da081b7f450c2edc1efb0f312d69f5cbd7b8517c6b249160c3719172dc473be88a3f
@@ -0,0 +1,198 @@
1
+ ## Rails 6.0.0 (August 16, 2019) ##
2
+
3
+ * No changes.
4
+
5
+
6
+ ## Rails 6.0.0.rc2 (July 22, 2019) ##
7
+
8
+ * No changes.
9
+
10
+
11
+ ## Rails 6.0.0.rc1 (April 24, 2019) ##
12
+
13
+ * Don't raise when analyzing an image whose type is unsupported by ImageMagick.
14
+
15
+ Fixes #36065.
16
+
17
+ *Guilherme Mansur*
18
+
19
+ * Permit generating variants of BMP images.
20
+
21
+ *Younes Serraj*
22
+
23
+
24
+ ## Rails 6.0.0.beta3 (March 11, 2019) ##
25
+
26
+ * No changes.
27
+
28
+
29
+ ## Rails 6.0.0.beta2 (February 25, 2019) ##
30
+
31
+ * No changes.
32
+
33
+
34
+ ## Rails 6.0.0.beta1 (January 18, 2019) ##
35
+
36
+ * [Rename npm package](https://github.com/rails/rails/pull/34905) from
37
+ [`activestorage`](https://www.npmjs.com/package/activestorage) to
38
+ [`@rails/activestorage`](https://www.npmjs.com/package/@rails/activestorage).
39
+
40
+ *Javan Makhmali*
41
+
42
+ * Replace `config.active_storage.queue` with two options that indicate which
43
+ queues analysis and purge jobs should use, respectively:
44
+
45
+ * `config.active_storage.queues.analysis`
46
+ * `config.active_storage.queues.purge`
47
+
48
+ `config.active_storage.queue` is preferred over the new options when it's
49
+ set, but it is deprecated and will be removed in Rails 6.1.
50
+
51
+ *George Claghorn*
52
+
53
+ * Permit generating variants of TIFF images.
54
+
55
+ *Luciano Sousa*
56
+
57
+ * Use base36 (all lowercase) for all new Blob keys to prevent
58
+ collisions and undefined behavior with case-insensitive filesystems and
59
+ database indices.
60
+
61
+ *Julik Tarkhanov*
62
+
63
+ * It doesn’t include an `X-CSRF-Token` header if a meta tag is not found on
64
+ the page. It previously included one with a value of `undefined`.
65
+
66
+ *Cameron Bothner*
67
+
68
+ * Fix `ArgumentError` when uploading to amazon s3
69
+
70
+ *Hiroki Sanpei*
71
+
72
+ * Add progressive JPG to default list of variable content types
73
+
74
+ *Maurice Kühlborn*
75
+
76
+ * Add `ActiveStorage.routes_prefix` for configuring generated routes.
77
+
78
+ *Chris Bisnett*
79
+
80
+ * `ActiveStorage::Service::AzureStorageService` only handles specifically
81
+ relevant types of `Azure::Core::Http::HTTPError`. It previously obscured
82
+ other types of `HTTPError`, which is the azure-storage gem’s catch-all
83
+ exception class.
84
+
85
+ *Cameron Bothner*
86
+
87
+ * `ActiveStorage::DiskController#show` generates a 404 Not Found response when
88
+ the requested file is missing from the disk service. It previously raised
89
+ `Errno::ENOENT`.
90
+
91
+ *Cameron Bothner*
92
+
93
+ * `ActiveStorage::Blob#download` and `ActiveStorage::Blob#open` raise
94
+ `ActiveStorage::FileNotFoundError` when the corresponding file is missing
95
+ from the storage service. Services translate service-specific missing object
96
+ exceptions (e.g. `Google::Cloud::NotFoundError` for the GCS service and
97
+ `Errno::ENOENT` for the disk service) into
98
+ `ActiveStorage::FileNotFoundError`.
99
+
100
+ *Cameron Bothner*
101
+
102
+ * Added the `ActiveStorage::SetCurrent` concern for custom Active Storage
103
+ controllers that can't inherit from `ActiveStorage::BaseController`.
104
+
105
+ *George Claghorn*
106
+
107
+ * Active Storage error classes like `ActiveStorage::IntegrityError` and
108
+ `ActiveStorage::UnrepresentableError` now inherit from `ActiveStorage::Error`
109
+ instead of `StandardError`. This permits rescuing `ActiveStorage::Error` to
110
+ handle all Active Storage errors.
111
+
112
+ *Andrei Makarov*, *George Claghorn*
113
+
114
+ * Uploaded files assigned to a record are persisted to storage when the record
115
+ is saved instead of immediately.
116
+
117
+ In Rails 5.2, the following causes an uploaded file in `params[:avatar]` to
118
+ be stored:
119
+
120
+ ```ruby
121
+ @user.avatar = params[:avatar]
122
+ ```
123
+
124
+ In Rails 6, the uploaded file is stored when `@user` is successfully saved.
125
+
126
+ *George Claghorn*
127
+
128
+ * Add the ability to reflect on defined attachments using the existing
129
+ ActiveRecord reflection mechanism.
130
+
131
+ *Kevin Deisz*
132
+
133
+ * Variant arguments of `false` or `nil` will no longer be passed to the
134
+ processor. For example, the following will not have the monochrome
135
+ variation applied:
136
+
137
+ ```ruby
138
+ avatar.variant(monochrome: false)
139
+ ```
140
+
141
+ *Jacob Smith*
142
+
143
+ * Generated attachment getter and setter methods are created
144
+ within the model's `GeneratedAssociationMethods` module to
145
+ allow overriding and composition using `super`.
146
+
147
+ *Josh Susser*, *Jamon Douglas*
148
+
149
+ * Add `ActiveStorage::Blob#open`, which downloads a blob to a tempfile on disk
150
+ and yields the tempfile. Deprecate `ActiveStorage::Downloading`.
151
+
152
+ *David Robertson*, *George Claghorn*
153
+
154
+ * Pass in `identify: false` as an argument when providing a `content_type` for
155
+ `ActiveStorage::Attached::{One,Many}#attach` to bypass automatic content
156
+ type inference. For example:
157
+
158
+ ```ruby
159
+ @message.image.attach(
160
+ io: File.open('/path/to/file'),
161
+ filename: 'file.pdf',
162
+ content_type: 'application/pdf',
163
+ identify: false
164
+ )
165
+ ```
166
+
167
+ *Ryan Davidson*
168
+
169
+ * The Google Cloud Storage service properly supports streaming downloads.
170
+ It now requires version 1.11 or newer of the google-cloud-storage gem.
171
+
172
+ *George Claghorn*
173
+
174
+ * Use the [ImageProcessing](https://github.com/janko-m/image_processing) gem
175
+ for Active Storage variants, and deprecate the MiniMagick backend.
176
+
177
+ This means that variants are now automatically oriented if the original
178
+ image was rotated. Also, in addition to the existing ImageMagick
179
+ operations, variants can now use `:resize_to_fit`, `:resize_to_fill`, and
180
+ other ImageProcessing macros. These are now recommended over raw `:resize`,
181
+ as they also sharpen the thumbnail after resizing.
182
+
183
+ The ImageProcessing gem also comes with a backend implemented on
184
+ [libvips](http://jcupitt.github.io/libvips/), an alternative to
185
+ ImageMagick which has significantly better performance than
186
+ ImageMagick in most cases, both in terms of speed and memory usage. In
187
+ Active Storage it's now possible to switch to the libvips backend by
188
+ changing `Rails.application.config.active_storage.variant_processor` to
189
+ `:vips`.
190
+
191
+ *Janko Marohnić*
192
+
193
+ * Rails 6 requires Ruby 2.5.0 or newer.
194
+
195
+ *Jeremy Daer*, *Kasper Timm Hansen*
196
+
197
+
198
+ Please check [5-2-stable](https://github.com/rails/rails/blob/5-2-stable/activestorage/CHANGELOG.md) for previous changes.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2017-2019 David Heinemeier Hansson, Basecamp
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,162 @@
1
+ # Active Storage
2
+
3
+ Active Storage makes it simple to upload and reference files in cloud services like [Amazon S3](https://aws.amazon.com/s3/), [Google Cloud Storage](https://cloud.google.com/storage/docs/), or [Microsoft Azure Storage](https://azure.microsoft.com/en-us/services/storage/), and attach those files to Active Records. Supports having one main service and mirrors in other services for redundancy. It also provides a disk service for testing or local deployments, but the focus is on cloud storage.
4
+
5
+ Files can be uploaded from the server to the cloud or directly from the client to the cloud.
6
+
7
+ Image files can furthermore be transformed using on-demand variants for quality, aspect ratio, size, or any other [MiniMagick](https://github.com/minimagick/minimagick) or [Vips](https://www.rubydoc.info/gems/ruby-vips/Vips/Image) supported transformation.
8
+
9
+ You can read more about Active Storage in the [Active Storage Overview](https://edgeguides.rubyonrails.org/active_storage_overview.html) guide.
10
+
11
+ ## Compared to other storage solutions
12
+
13
+ A key difference to how Active Storage works compared to other attachment solutions in Rails is through the use of built-in [Blob](https://github.com/rails/rails/blob/master/activestorage/app/models/active_storage/blob.rb) and [Attachment](https://github.com/rails/rails/blob/master/activestorage/app/models/active_storage/attachment.rb) models (backed by Active Record). This means existing application models do not need to be modified with additional columns to associate with files. Active Storage uses polymorphic associations via the `Attachment` join model, which then connects to the actual `Blob`.
14
+
15
+ `Blob` models store attachment metadata (filename, content-type, etc.), and their identifier key in the storage service. Blob models do not store the actual binary data. They are intended to be immutable in spirit. One file, one blob. You can associate the same blob with multiple application models as well. And if you want to do transformations of a given `Blob`, the idea is that you'll simply create a new one, rather than attempt to mutate the existing one (though of course you can delete the previous version later if you don't need it).
16
+
17
+ ## Installation
18
+
19
+ Run `rails active_storage:install` to copy over active_storage migrations.
20
+
21
+ NOTE: If the task cannot be found, verify that `require "active_storage/engine"` is present in `config/application.rb`.
22
+
23
+ ## Examples
24
+
25
+ One attachment:
26
+
27
+ ```ruby
28
+ class User < ApplicationRecord
29
+ # Associates an attachment and a blob. When the user is destroyed they are
30
+ # purged by default (models destroyed, and resource files deleted).
31
+ has_one_attached :avatar
32
+ end
33
+
34
+ # Attach an avatar to the user.
35
+ user.avatar.attach(io: File.open("/path/to/face.jpg"), filename: "face.jpg", content_type: "image/jpg")
36
+
37
+ # Does the user have an avatar?
38
+ user.avatar.attached? # => true
39
+
40
+ # Synchronously destroy the avatar and actual resource files.
41
+ user.avatar.purge
42
+
43
+ # Destroy the associated models and actual resource files async, via Active Job.
44
+ user.avatar.purge_later
45
+
46
+ # Does the user have an avatar?
47
+ user.avatar.attached? # => false
48
+
49
+ # Generate a permanent URL for the blob that points to the application.
50
+ # Upon access, a redirect to the actual service endpoint is returned.
51
+ # This indirection decouples the public URL from the actual one, and
52
+ # allows for example mirroring attachments in different services for
53
+ # high-availability. The redirection has an HTTP expiration of 5 min.
54
+ url_for(user.avatar)
55
+
56
+ class AvatarsController < ApplicationController
57
+ def update
58
+ # params[:avatar] contains a ActionDispatch::Http::UploadedFile object
59
+ Current.user.avatar.attach(params.require(:avatar))
60
+ redirect_to Current.user
61
+ end
62
+ end
63
+ ```
64
+
65
+ Many attachments:
66
+
67
+ ```ruby
68
+ class Message < ApplicationRecord
69
+ has_many_attached :images
70
+ end
71
+ ```
72
+
73
+ ```erb
74
+ <%= form_with model: @message, local: true do |form| %>
75
+ <%= form.text_field :title, placeholder: "Title" %><br>
76
+ <%= form.text_area :content %><br><br>
77
+
78
+ <%= form.file_field :images, multiple: true %><br>
79
+ <%= form.submit %>
80
+ <% end %>
81
+ ```
82
+
83
+ ```ruby
84
+ class MessagesController < ApplicationController
85
+ def index
86
+ # Use the built-in with_attached_images scope to avoid N+1
87
+ @messages = Message.all.with_attached_images
88
+ end
89
+
90
+ def create
91
+ message = Message.create! params.require(:message).permit(:title, :content)
92
+ message.images.attach(params[:message][:images])
93
+ redirect_to message
94
+ end
95
+
96
+ def show
97
+ @message = Message.find(params[:id])
98
+ end
99
+ end
100
+ ```
101
+
102
+ Variation of image attachment:
103
+
104
+ ```erb
105
+ <%# Hitting the variant URL will lazy transform the original blob and then redirect to its new service location %>
106
+ <%= image_tag user.avatar.variant(resize_to_limit: [100, 100]) %>
107
+ ```
108
+
109
+ ## Direct uploads
110
+
111
+ Active Storage, with its included JavaScript library, supports uploading directly from the client to the cloud.
112
+
113
+ ### Direct upload installation
114
+
115
+ 1. Include `activestorage.js` in your application's JavaScript bundle.
116
+
117
+ Using the asset pipeline:
118
+ ```js
119
+ //= require activestorage
120
+ ```
121
+ Using the npm package:
122
+ ```js
123
+ require("@rails/activestorage").start()
124
+ ```
125
+ 2. Annotate file inputs with the direct upload URL.
126
+
127
+ ```ruby
128
+ <%= form.file_field :attachments, multiple: true, direct_upload: true %>
129
+ ```
130
+ 3. That's it! Uploads begin upon form submission.
131
+
132
+ ### Direct upload JavaScript events
133
+
134
+ | Event name | Event target | Event data (`event.detail`) | Description |
135
+ | --- | --- | --- | --- |
136
+ | `direct-uploads:start` | `<form>` | None | A form containing files for direct upload fields was submitted. |
137
+ | `direct-upload:initialize` | `<input>` | `{id, file}` | Dispatched for every file after form submission. |
138
+ | `direct-upload:start` | `<input>` | `{id, file}` | A direct upload is starting. |
139
+ | `direct-upload:before-blob-request` | `<input>` | `{id, file, xhr}` | Before making a request to your application for direct upload metadata. |
140
+ | `direct-upload:before-storage-request` | `<input>` | `{id, file, xhr}` | Before making a request to store a file. |
141
+ | `direct-upload:progress` | `<input>` | `{id, file, progress}` | As requests to store files progress. |
142
+ | `direct-upload:error` | `<input>` | `{id, file, error}` | An error occurred. An `alert` will display unless this event is canceled. |
143
+ | `direct-upload:end` | `<input>` | `{id, file}` | A direct upload has ended. |
144
+ | `direct-uploads:end` | `<form>` | None | All direct uploads have ended. |
145
+
146
+ ## License
147
+
148
+ Active Storage is released under the [MIT License](https://opensource.org/licenses/MIT).
149
+
150
+ ## Support
151
+
152
+ API documentation is at:
153
+
154
+ * https://api.rubyonrails.org
155
+
156
+ Bug reports for the Ruby on Rails project can be filed here:
157
+
158
+ * https://github.com/rails/rails/issues
159
+
160
+ Feature requests should be discussed on the rails-core mailing list here:
161
+
162
+ * https://groups.google.com/forum/?fromgroups#!forum/rubyonrails-core
@@ -0,0 +1,942 @@
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) {
4
+ "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) {
11
+ (function(factory) {
12
+ {
13
+ module.exports = factory();
14
+ }
15
+ })(function(undefined) {
16
+ var hex_chr = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" ];
17
+ function md5cycle(x, k) {
18
+ var a = x[0], b = x[1], c = x[2], d = x[3];
19
+ a += (b & c | ~b & d) + k[0] - 680876936 | 0;
20
+ a = (a << 7 | a >>> 25) + b | 0;
21
+ d += (a & b | ~a & c) + k[1] - 389564586 | 0;
22
+ d = (d << 12 | d >>> 20) + a | 0;
23
+ c += (d & a | ~d & b) + k[2] + 606105819 | 0;
24
+ c = (c << 17 | c >>> 15) + d | 0;
25
+ b += (c & d | ~c & a) + k[3] - 1044525330 | 0;
26
+ b = (b << 22 | b >>> 10) + c | 0;
27
+ a += (b & c | ~b & d) + k[4] - 176418897 | 0;
28
+ a = (a << 7 | a >>> 25) + b | 0;
29
+ d += (a & b | ~a & c) + k[5] + 1200080426 | 0;
30
+ d = (d << 12 | d >>> 20) + a | 0;
31
+ c += (d & a | ~d & b) + k[6] - 1473231341 | 0;
32
+ c = (c << 17 | c >>> 15) + d | 0;
33
+ b += (c & d | ~c & a) + k[7] - 45705983 | 0;
34
+ b = (b << 22 | b >>> 10) + c | 0;
35
+ a += (b & c | ~b & d) + k[8] + 1770035416 | 0;
36
+ a = (a << 7 | a >>> 25) + b | 0;
37
+ d += (a & b | ~a & c) + k[9] - 1958414417 | 0;
38
+ d = (d << 12 | d >>> 20) + a | 0;
39
+ c += (d & a | ~d & b) + k[10] - 42063 | 0;
40
+ c = (c << 17 | c >>> 15) + d | 0;
41
+ b += (c & d | ~c & a) + k[11] - 1990404162 | 0;
42
+ b = (b << 22 | b >>> 10) + c | 0;
43
+ a += (b & c | ~b & d) + k[12] + 1804603682 | 0;
44
+ a = (a << 7 | a >>> 25) + b | 0;
45
+ d += (a & b | ~a & c) + k[13] - 40341101 | 0;
46
+ d = (d << 12 | d >>> 20) + a | 0;
47
+ c += (d & a | ~d & b) + k[14] - 1502002290 | 0;
48
+ c = (c << 17 | c >>> 15) + d | 0;
49
+ b += (c & d | ~c & a) + k[15] + 1236535329 | 0;
50
+ b = (b << 22 | b >>> 10) + c | 0;
51
+ a += (b & d | c & ~d) + k[1] - 165796510 | 0;
52
+ a = (a << 5 | a >>> 27) + b | 0;
53
+ d += (a & c | b & ~c) + k[6] - 1069501632 | 0;
54
+ d = (d << 9 | d >>> 23) + a | 0;
55
+ c += (d & b | a & ~b) + k[11] + 643717713 | 0;
56
+ c = (c << 14 | c >>> 18) + d | 0;
57
+ b += (c & a | d & ~a) + k[0] - 373897302 | 0;
58
+ b = (b << 20 | b >>> 12) + c | 0;
59
+ a += (b & d | c & ~d) + k[5] - 701558691 | 0;
60
+ a = (a << 5 | a >>> 27) + b | 0;
61
+ d += (a & c | b & ~c) + k[10] + 38016083 | 0;
62
+ d = (d << 9 | d >>> 23) + a | 0;
63
+ c += (d & b | a & ~b) + k[15] - 660478335 | 0;
64
+ c = (c << 14 | c >>> 18) + d | 0;
65
+ b += (c & a | d & ~a) + k[4] - 405537848 | 0;
66
+ b = (b << 20 | b >>> 12) + c | 0;
67
+ a += (b & d | c & ~d) + k[9] + 568446438 | 0;
68
+ a = (a << 5 | a >>> 27) + b | 0;
69
+ d += (a & c | b & ~c) + k[14] - 1019803690 | 0;
70
+ d = (d << 9 | d >>> 23) + a | 0;
71
+ c += (d & b | a & ~b) + k[3] - 187363961 | 0;
72
+ c = (c << 14 | c >>> 18) + d | 0;
73
+ b += (c & a | d & ~a) + k[8] + 1163531501 | 0;
74
+ b = (b << 20 | b >>> 12) + c | 0;
75
+ a += (b & d | c & ~d) + k[13] - 1444681467 | 0;
76
+ a = (a << 5 | a >>> 27) + b | 0;
77
+ d += (a & c | b & ~c) + k[2] - 51403784 | 0;
78
+ d = (d << 9 | d >>> 23) + a | 0;
79
+ c += (d & b | a & ~b) + k[7] + 1735328473 | 0;
80
+ c = (c << 14 | c >>> 18) + d | 0;
81
+ b += (c & a | d & ~a) + k[12] - 1926607734 | 0;
82
+ b = (b << 20 | b >>> 12) + c | 0;
83
+ a += (b ^ c ^ d) + k[5] - 378558 | 0;
84
+ a = (a << 4 | a >>> 28) + b | 0;
85
+ d += (a ^ b ^ c) + k[8] - 2022574463 | 0;
86
+ d = (d << 11 | d >>> 21) + a | 0;
87
+ c += (d ^ a ^ b) + k[11] + 1839030562 | 0;
88
+ c = (c << 16 | c >>> 16) + d | 0;
89
+ b += (c ^ d ^ a) + k[14] - 35309556 | 0;
90
+ b = (b << 23 | b >>> 9) + c | 0;
91
+ a += (b ^ c ^ d) + k[1] - 1530992060 | 0;
92
+ a = (a << 4 | a >>> 28) + b | 0;
93
+ d += (a ^ b ^ c) + k[4] + 1272893353 | 0;
94
+ d = (d << 11 | d >>> 21) + a | 0;
95
+ c += (d ^ a ^ b) + k[7] - 155497632 | 0;
96
+ c = (c << 16 | c >>> 16) + d | 0;
97
+ b += (c ^ d ^ a) + k[10] - 1094730640 | 0;
98
+ b = (b << 23 | b >>> 9) + c | 0;
99
+ a += (b ^ c ^ d) + k[13] + 681279174 | 0;
100
+ a = (a << 4 | a >>> 28) + b | 0;
101
+ d += (a ^ b ^ c) + k[0] - 358537222 | 0;
102
+ d = (d << 11 | d >>> 21) + a | 0;
103
+ c += (d ^ a ^ b) + k[3] - 722521979 | 0;
104
+ c = (c << 16 | c >>> 16) + d | 0;
105
+ b += (c ^ d ^ a) + k[6] + 76029189 | 0;
106
+ b = (b << 23 | b >>> 9) + c | 0;
107
+ a += (b ^ c ^ d) + k[9] - 640364487 | 0;
108
+ a = (a << 4 | a >>> 28) + b | 0;
109
+ d += (a ^ b ^ c) + k[12] - 421815835 | 0;
110
+ d = (d << 11 | d >>> 21) + a | 0;
111
+ c += (d ^ a ^ b) + k[15] + 530742520 | 0;
112
+ c = (c << 16 | c >>> 16) + d | 0;
113
+ b += (c ^ d ^ a) + k[2] - 995338651 | 0;
114
+ b = (b << 23 | b >>> 9) + c | 0;
115
+ a += (c ^ (b | ~d)) + k[0] - 198630844 | 0;
116
+ a = (a << 6 | a >>> 26) + b | 0;
117
+ d += (b ^ (a | ~c)) + k[7] + 1126891415 | 0;
118
+ d = (d << 10 | d >>> 22) + a | 0;
119
+ c += (a ^ (d | ~b)) + k[14] - 1416354905 | 0;
120
+ c = (c << 15 | c >>> 17) + d | 0;
121
+ b += (d ^ (c | ~a)) + k[5] - 57434055 | 0;
122
+ b = (b << 21 | b >>> 11) + c | 0;
123
+ a += (c ^ (b | ~d)) + k[12] + 1700485571 | 0;
124
+ a = (a << 6 | a >>> 26) + b | 0;
125
+ d += (b ^ (a | ~c)) + k[3] - 1894986606 | 0;
126
+ d = (d << 10 | d >>> 22) + a | 0;
127
+ c += (a ^ (d | ~b)) + k[10] - 1051523 | 0;
128
+ c = (c << 15 | c >>> 17) + d | 0;
129
+ b += (d ^ (c | ~a)) + k[1] - 2054922799 | 0;
130
+ b = (b << 21 | b >>> 11) + c | 0;
131
+ a += (c ^ (b | ~d)) + k[8] + 1873313359 | 0;
132
+ a = (a << 6 | a >>> 26) + b | 0;
133
+ d += (b ^ (a | ~c)) + k[15] - 30611744 | 0;
134
+ d = (d << 10 | d >>> 22) + a | 0;
135
+ c += (a ^ (d | ~b)) + k[6] - 1560198380 | 0;
136
+ c = (c << 15 | c >>> 17) + d | 0;
137
+ b += (d ^ (c | ~a)) + k[13] + 1309151649 | 0;
138
+ b = (b << 21 | b >>> 11) + c | 0;
139
+ a += (c ^ (b | ~d)) + k[4] - 145523070 | 0;
140
+ a = (a << 6 | a >>> 26) + b | 0;
141
+ d += (b ^ (a | ~c)) + k[11] - 1120210379 | 0;
142
+ d = (d << 10 | d >>> 22) + a | 0;
143
+ c += (a ^ (d | ~b)) + k[2] + 718787259 | 0;
144
+ c = (c << 15 | c >>> 17) + d | 0;
145
+ b += (d ^ (c | ~a)) + k[9] - 343485551 | 0;
146
+ b = (b << 21 | b >>> 11) + c | 0;
147
+ x[0] = a + x[0] | 0;
148
+ x[1] = b + x[1] | 0;
149
+ x[2] = c + x[2] | 0;
150
+ x[3] = d + x[3] | 0;
151
+ }
152
+ function md5blk(s) {
153
+ var md5blks = [], i;
154
+ for (i = 0; i < 64; i += 4) {
155
+ md5blks[i >> 2] = s.charCodeAt(i) + (s.charCodeAt(i + 1) << 8) + (s.charCodeAt(i + 2) << 16) + (s.charCodeAt(i + 3) << 24);
156
+ }
157
+ return md5blks;
158
+ }
159
+ function md5blk_array(a) {
160
+ var md5blks = [], i;
161
+ for (i = 0; i < 64; i += 4) {
162
+ md5blks[i >> 2] = a[i] + (a[i + 1] << 8) + (a[i + 2] << 16) + (a[i + 3] << 24);
163
+ }
164
+ return md5blks;
165
+ }
166
+ function md51(s) {
167
+ var n = s.length, state = [ 1732584193, -271733879, -1732584194, 271733878 ], i, length, tail, tmp, lo, hi;
168
+ for (i = 64; i <= n; i += 64) {
169
+ md5cycle(state, md5blk(s.substring(i - 64, i)));
170
+ }
171
+ s = s.substring(i - 64);
172
+ length = s.length;
173
+ tail = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
174
+ for (i = 0; i < length; i += 1) {
175
+ tail[i >> 2] |= s.charCodeAt(i) << (i % 4 << 3);
176
+ }
177
+ tail[i >> 2] |= 128 << (i % 4 << 3);
178
+ if (i > 55) {
179
+ md5cycle(state, tail);
180
+ for (i = 0; i < 16; i += 1) {
181
+ tail[i] = 0;
182
+ }
183
+ }
184
+ tmp = n * 8;
185
+ tmp = tmp.toString(16).match(/(.*?)(.{0,8})$/);
186
+ lo = parseInt(tmp[2], 16);
187
+ hi = parseInt(tmp[1], 16) || 0;
188
+ tail[14] = lo;
189
+ tail[15] = hi;
190
+ md5cycle(state, tail);
191
+ return state;
192
+ }
193
+ function md51_array(a) {
194
+ var n = a.length, state = [ 1732584193, -271733879, -1732584194, 271733878 ], i, length, tail, tmp, lo, hi;
195
+ for (i = 64; i <= n; i += 64) {
196
+ md5cycle(state, md5blk_array(a.subarray(i - 64, i)));
197
+ }
198
+ a = i - 64 < n ? a.subarray(i - 64) : new Uint8Array(0);
199
+ length = a.length;
200
+ tail = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
201
+ for (i = 0; i < length; i += 1) {
202
+ tail[i >> 2] |= a[i] << (i % 4 << 3);
203
+ }
204
+ tail[i >> 2] |= 128 << (i % 4 << 3);
205
+ if (i > 55) {
206
+ md5cycle(state, tail);
207
+ for (i = 0; i < 16; i += 1) {
208
+ tail[i] = 0;
209
+ }
210
+ }
211
+ tmp = n * 8;
212
+ tmp = tmp.toString(16).match(/(.*?)(.{0,8})$/);
213
+ lo = parseInt(tmp[2], 16);
214
+ hi = parseInt(tmp[1], 16) || 0;
215
+ tail[14] = lo;
216
+ tail[15] = hi;
217
+ md5cycle(state, tail);
218
+ return state;
219
+ }
220
+ function rhex(n) {
221
+ var s = "", j;
222
+ for (j = 0; j < 4; j += 1) {
223
+ s += hex_chr[n >> j * 8 + 4 & 15] + hex_chr[n >> j * 8 & 15];
224
+ }
225
+ return s;
226
+ }
227
+ function hex(x) {
228
+ var i;
229
+ for (i = 0; i < x.length; i += 1) {
230
+ x[i] = rhex(x[i]);
231
+ }
232
+ return x.join("");
233
+ }
234
+ if (hex(md51("hello")) !== "5d41402abc4b2a76b9719d911017c592") ;
235
+ if (typeof ArrayBuffer !== "undefined" && !ArrayBuffer.prototype.slice) {
236
+ (function() {
237
+ function clamp(val, length) {
238
+ val = val | 0 || 0;
239
+ if (val < 0) {
240
+ return Math.max(val + length, 0);
241
+ }
242
+ return Math.min(val, length);
243
+ }
244
+ ArrayBuffer.prototype.slice = function(from, to) {
245
+ var length = this.byteLength, begin = clamp(from, length), end = length, num, target, targetArray, sourceArray;
246
+ if (to !== undefined) {
247
+ end = clamp(to, length);
248
+ }
249
+ if (begin > end) {
250
+ return new ArrayBuffer(0);
251
+ }
252
+ num = end - begin;
253
+ target = new ArrayBuffer(num);
254
+ targetArray = new Uint8Array(target);
255
+ sourceArray = new Uint8Array(this, begin, num);
256
+ targetArray.set(sourceArray);
257
+ return target;
258
+ };
259
+ })();
260
+ }
261
+ function toUtf8(str) {
262
+ if (/[\u0080-\uFFFF]/.test(str)) {
263
+ str = unescape(encodeURIComponent(str));
264
+ }
265
+ return str;
266
+ }
267
+ function utf8Str2ArrayBuffer(str, returnUInt8Array) {
268
+ var length = str.length, buff = new ArrayBuffer(length), arr = new Uint8Array(buff), i;
269
+ for (i = 0; i < length; i += 1) {
270
+ arr[i] = str.charCodeAt(i);
271
+ }
272
+ return returnUInt8Array ? arr : buff;
273
+ }
274
+ function arrayBuffer2Utf8Str(buff) {
275
+ return String.fromCharCode.apply(null, new Uint8Array(buff));
276
+ }
277
+ function concatenateArrayBuffers(first, second, returnUInt8Array) {
278
+ var result = new Uint8Array(first.byteLength + second.byteLength);
279
+ result.set(new Uint8Array(first));
280
+ result.set(new Uint8Array(second), first.byteLength);
281
+ return returnUInt8Array ? result : result.buffer;
282
+ }
283
+ function hexToBinaryString(hex) {
284
+ var bytes = [], length = hex.length, x;
285
+ for (x = 0; x < length - 1; x += 2) {
286
+ bytes.push(parseInt(hex.substr(x, 2), 16));
287
+ }
288
+ return String.fromCharCode.apply(String, bytes);
289
+ }
290
+ function SparkMD5() {
291
+ this.reset();
292
+ }
293
+ SparkMD5.prototype.append = function(str) {
294
+ this.appendBinary(toUtf8(str));
295
+ return this;
296
+ };
297
+ SparkMD5.prototype.appendBinary = function(contents) {
298
+ this._buff += contents;
299
+ this._length += contents.length;
300
+ var length = this._buff.length, i;
301
+ for (i = 64; i <= length; i += 64) {
302
+ md5cycle(this._hash, md5blk(this._buff.substring(i - 64, i)));
303
+ }
304
+ this._buff = this._buff.substring(i - 64);
305
+ return this;
306
+ };
307
+ SparkMD5.prototype.end = function(raw) {
308
+ var buff = this._buff, length = buff.length, i, tail = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], ret;
309
+ for (i = 0; i < length; i += 1) {
310
+ tail[i >> 2] |= buff.charCodeAt(i) << (i % 4 << 3);
311
+ }
312
+ this._finish(tail, length);
313
+ ret = hex(this._hash);
314
+ if (raw) {
315
+ ret = hexToBinaryString(ret);
316
+ }
317
+ this.reset();
318
+ return ret;
319
+ };
320
+ SparkMD5.prototype.reset = function() {
321
+ this._buff = "";
322
+ this._length = 0;
323
+ this._hash = [ 1732584193, -271733879, -1732584194, 271733878 ];
324
+ return this;
325
+ };
326
+ SparkMD5.prototype.getState = function() {
327
+ return {
328
+ buff: this._buff,
329
+ length: this._length,
330
+ hash: this._hash
331
+ };
332
+ };
333
+ SparkMD5.prototype.setState = function(state) {
334
+ this._buff = state.buff;
335
+ this._length = state.length;
336
+ this._hash = state.hash;
337
+ return this;
338
+ };
339
+ SparkMD5.prototype.destroy = function() {
340
+ delete this._hash;
341
+ delete this._buff;
342
+ delete this._length;
343
+ };
344
+ SparkMD5.prototype._finish = function(tail, length) {
345
+ var i = length, tmp, lo, hi;
346
+ tail[i >> 2] |= 128 << (i % 4 << 3);
347
+ if (i > 55) {
348
+ md5cycle(this._hash, tail);
349
+ for (i = 0; i < 16; i += 1) {
350
+ tail[i] = 0;
351
+ }
352
+ }
353
+ tmp = this._length * 8;
354
+ tmp = tmp.toString(16).match(/(.*?)(.{0,8})$/);
355
+ lo = parseInt(tmp[2], 16);
356
+ hi = parseInt(tmp[1], 16) || 0;
357
+ tail[14] = lo;
358
+ tail[15] = hi;
359
+ md5cycle(this._hash, tail);
360
+ };
361
+ SparkMD5.hash = function(str, raw) {
362
+ return SparkMD5.hashBinary(toUtf8(str), raw);
363
+ };
364
+ SparkMD5.hashBinary = function(content, raw) {
365
+ var hash = md51(content), ret = hex(hash);
366
+ return raw ? hexToBinaryString(ret) : ret;
367
+ };
368
+ SparkMD5.ArrayBuffer = function() {
369
+ this.reset();
370
+ };
371
+ SparkMD5.ArrayBuffer.prototype.append = function(arr) {
372
+ var buff = concatenateArrayBuffers(this._buff.buffer, arr, true), length = buff.length, i;
373
+ this._length += arr.byteLength;
374
+ for (i = 64; i <= length; i += 64) {
375
+ md5cycle(this._hash, md5blk_array(buff.subarray(i - 64, i)));
376
+ }
377
+ this._buff = i - 64 < length ? new Uint8Array(buff.buffer.slice(i - 64)) : new Uint8Array(0);
378
+ return this;
379
+ };
380
+ SparkMD5.ArrayBuffer.prototype.end = function(raw) {
381
+ var buff = this._buff, length = buff.length, tail = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], i, ret;
382
+ for (i = 0; i < length; i += 1) {
383
+ tail[i >> 2] |= buff[i] << (i % 4 << 3);
384
+ }
385
+ this._finish(tail, length);
386
+ ret = hex(this._hash);
387
+ if (raw) {
388
+ ret = hexToBinaryString(ret);
389
+ }
390
+ this.reset();
391
+ return ret;
392
+ };
393
+ SparkMD5.ArrayBuffer.prototype.reset = function() {
394
+ this._buff = new Uint8Array(0);
395
+ this._length = 0;
396
+ this._hash = [ 1732584193, -271733879, -1732584194, 271733878 ];
397
+ return this;
398
+ };
399
+ SparkMD5.ArrayBuffer.prototype.getState = function() {
400
+ var state = SparkMD5.prototype.getState.call(this);
401
+ state.buff = arrayBuffer2Utf8Str(state.buff);
402
+ return state;
403
+ };
404
+ SparkMD5.ArrayBuffer.prototype.setState = function(state) {
405
+ state.buff = utf8Str2ArrayBuffer(state.buff, true);
406
+ return SparkMD5.prototype.setState.call(this, state);
407
+ };
408
+ SparkMD5.ArrayBuffer.prototype.destroy = SparkMD5.prototype.destroy;
409
+ SparkMD5.ArrayBuffer.prototype._finish = SparkMD5.prototype._finish;
410
+ SparkMD5.ArrayBuffer.hash = function(arr, raw) {
411
+ var hash = md51_array(new Uint8Array(arr)), ret = hex(hash);
412
+ return raw ? hexToBinaryString(ret) : ret;
413
+ };
414
+ 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
+ }
431
+ }
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);
449
+ this.file = file;
450
+ this.chunkSize = 2097152;
451
+ this.chunkCount = Math.ceil(this.file.size / this.chunkSize);
452
+ this.chunkIndex = 0;
453
+ }
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);
483
+ }
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
+ }
497
+ }
498
+ } ]);
499
+ return FileChecksum;
500
+ }();
501
+ function getMetaValue(name) {
502
+ var element = findElement(document.head, 'meta[name="' + name + '"]');
503
+ if (element) {
504
+ return element.getAttribute("content");
505
+ }
506
+ }
507
+ function findElements(root, selector) {
508
+ if (typeof root == "string") {
509
+ selector = root;
510
+ root = document;
511
+ }
512
+ var elements = root.querySelectorAll(selector);
513
+ return toArray$1(elements);
514
+ }
515
+ function findElement(root, selector) {
516
+ if (typeof root == "string") {
517
+ selector = root;
518
+ root = document;
519
+ }
520
+ return root.querySelector(selector);
521
+ }
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");
527
+ event.initEvent(type, bubbles || true, cancelable || true);
528
+ event.detail = detail || {};
529
+ try {
530
+ element.disabled = false;
531
+ element.dispatchEvent(event);
532
+ } finally {
533
+ element.disabled = disabled;
534
+ }
535
+ return event;
536
+ }
537
+ function toArray$1(value) {
538
+ if (Array.isArray(value)) {
539
+ return value;
540
+ } else if (Array.from) {
541
+ return Array.from(value);
542
+ } else {
543
+ return [].slice.call(value);
544
+ }
545
+ }
546
+ var BlobRecord = function() {
547
+ function BlobRecord(file, checksum, url) {
548
+ var _this = this;
549
+ classCallCheck(this, BlobRecord);
550
+ this.file = file;
551
+ this.attributes = {
552
+ filename: file.name,
553
+ content_type: file.type,
554
+ byte_size: file.size,
555
+ checksum: checksum
556
+ };
557
+ this.xhr = new XMLHttpRequest();
558
+ this.xhr.open("POST", url, true);
559
+ this.xhr.responseType = "json";
560
+ this.xhr.setRequestHeader("Content-Type", "application/json");
561
+ this.xhr.setRequestHeader("Accept", "application/json");
562
+ this.xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
563
+ var csrfToken = getMetaValue("csrf-token");
564
+ if (csrfToken != undefined) {
565
+ this.xhr.setRequestHeader("X-CSRF-Token", csrfToken);
566
+ }
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
+ });
573
+ }
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;
609
+ }
610
+ }, {
611
+ key: "status",
612
+ get: function get$$1() {
613
+ return this.xhr.status;
614
+ }
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
+ }
624
+ }
625
+ } ]);
626
+ return BlobRecord;
627
+ }();
628
+ var BlobUpload = function() {
629
+ function BlobUpload(blob) {
630
+ var _this = this;
631
+ classCallCheck(this, BlobUpload);
632
+ this.blob = blob;
633
+ this.file = blob.file;
634
+ var _blob$directUploadDat = blob.directUploadData, url = _blob$directUploadDat.url, headers = _blob$directUploadDat.headers;
635
+ this.xhr = new XMLHttpRequest();
636
+ this.xhr.open("PUT", url, true);
637
+ this.xhr.responseType = "text";
638
+ for (var key in headers) {
639
+ this.xhr.setRequestHeader(key, headers[key]);
640
+ }
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
+ });
647
+ }
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);
668
+ }
669
+ } ]);
670
+ return BlobUpload;
671
+ }();
672
+ var id = 0;
673
+ var DirectUpload = function() {
674
+ function DirectUpload(file, url, delegate) {
675
+ classCallCheck(this, DirectUpload);
676
+ this.id = ++id;
677
+ this.file = file;
678
+ this.url = url;
679
+ this.delegate = delegate;
680
+ }
681
+ createClass(DirectUpload, [ {
682
+ key: "create",
683
+ value: function create(callback) {
684
+ var _this = this;
685
+ FileChecksum.create(this.file, function(error, checksum) {
686
+ if (error) {
687
+ callback(error);
688
+ return;
689
+ }
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) {
713
+ 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);
718
+ }
719
+ }
720
+ var DirectUploadController = function() {
721
+ function DirectUploadController(input, file) {
722
+ classCallCheck(this, DirectUploadController);
723
+ this.input = input;
724
+ this.file = file;
725
+ this.directUpload = new DirectUpload(this.file, this.url, this);
726
+ this.dispatch("initialize");
727
+ }
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);
776
+ }
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);
794
+ });
795
+ }
796
+ }, {
797
+ key: "url",
798
+ get: function get$$1() {
799
+ return this.input.getAttribute("data-direct-upload-url");
800
+ }
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);
808
+ this.form = form;
809
+ this.inputs = findElements(form, inputSelector).filter(function(input) {
810
+ return input.files.length;
811
+ });
812
+ }
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;
863
+ function start() {
864
+ if (!started) {
865
+ started = true;
866
+ document.addEventListener("click", didClick, true);
867
+ document.addEventListener("submit", didSubmitForm);
868
+ document.addEventListener("ajax:before", didSubmitRemoteElement);
869
+ }
870
+ }
871
+ function didClick(event) {
872
+ var target = event.target;
873
+ if ((target.tagName == "INPUT" || target.tagName == "BUTTON") && target.type == "submit" && target.form) {
874
+ submitButtonsByForm.set(target.form, target);
875
+ }
876
+ }
877
+ function didSubmitForm(event) {
878
+ handleFormSubmissionEvent(event);
879
+ }
880
+ function didSubmitRemoteElement(event) {
881
+ if (event.target.tagName == "FORM") {
882
+ handleFormSubmissionEvent(event);
883
+ }
884
+ }
885
+ function handleFormSubmissionEvent(event) {
886
+ var form = event.target;
887
+ if (form.hasAttribute(processingAttribute)) {
888
+ event.preventDefault();
889
+ return;
890
+ }
891
+ var controller = new DirectUploadsController(form);
892
+ var inputs = controller.inputs;
893
+ if (inputs.length) {
894
+ event.preventDefault();
895
+ form.setAttribute(processingAttribute, "");
896
+ inputs.forEach(disable);
897
+ controller.start(function(error) {
898
+ form.removeAttribute(processingAttribute);
899
+ if (error) {
900
+ inputs.forEach(enable);
901
+ } else {
902
+ submitForm(form);
903
+ }
904
+ });
905
+ }
906
+ }
907
+ function submitForm(form) {
908
+ var button = submitButtonsByForm.get(form) || findElement(form, "input[type=submit], button[type=submit]");
909
+ if (button) {
910
+ var _button = button, disabled = _button.disabled;
911
+ button.disabled = false;
912
+ button.focus();
913
+ button.click();
914
+ button.disabled = disabled;
915
+ } else {
916
+ button = document.createElement("input");
917
+ button.type = "submit";
918
+ button.style.display = "none";
919
+ form.appendChild(button);
920
+ button.click();
921
+ form.removeChild(button);
922
+ }
923
+ submitButtonsByForm.delete(form);
924
+ }
925
+ function disable(input) {
926
+ input.disabled = true;
927
+ }
928
+ function enable(input) {
929
+ input.disabled = false;
930
+ }
931
+ function autostart() {
932
+ if (window.ActiveStorage) {
933
+ start();
934
+ }
935
+ }
936
+ setTimeout(autostart, 1);
937
+ exports.start = start;
938
+ exports.DirectUpload = DirectUpload;
939
+ Object.defineProperty(exports, "__esModule", {
940
+ value: true
941
+ });
942
+ });