activestorage 5.2.1.1 → 6.0.1
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 +173 -46
- data/MIT-LICENSE +1 -1
- data/README.md +9 -6
- data/app/assets/javascripts/activestorage.js +942 -1
- data/app/controllers/active_storage/base_controller.rb +3 -5
- data/app/controllers/active_storage/blobs_controller.rb +1 -1
- data/app/controllers/active_storage/disk_controller.rb +19 -15
- data/app/controllers/active_storage/representations_controller.rb +1 -1
- data/app/controllers/concerns/active_storage/set_current.rb +15 -0
- data/app/javascript/activestorage/blob_record.js +6 -1
- data/app/javascript/activestorage/ujs.js +12 -1
- data/app/jobs/active_storage/analyze_job.rb +5 -0
- data/app/jobs/active_storage/base_job.rb +0 -1
- data/app/jobs/active_storage/purge_job.rb +4 -2
- data/app/models/active_storage/attachment.rb +20 -9
- data/app/models/active_storage/blob.rb +90 -32
- data/app/models/active_storage/blob/identifiable.rb +5 -1
- data/app/models/active_storage/blob/representable.rb +5 -5
- data/app/models/active_storage/filename.rb +0 -6
- data/app/models/active_storage/preview.rb +3 -3
- data/app/models/active_storage/variant.rb +51 -52
- data/app/models/active_storage/variation.rb +24 -33
- data/config/routes.rb +13 -12
- data/db/migrate/20170806125915_create_active_storage_tables.rb +1 -0
- data/db/update_migrate/20180723000244_add_foreign_key_constraint_to_active_storage_attachments_for_blob_id.rb +9 -0
- data/lib/active_storage.rb +26 -6
- data/lib/active_storage/analyzer.rb +9 -4
- data/lib/active_storage/analyzer/image_analyzer.rb +11 -4
- data/lib/active_storage/analyzer/video_analyzer.rb +3 -5
- data/lib/active_storage/attached.rb +7 -22
- data/lib/active_storage/attached/changes.rb +16 -0
- data/lib/active_storage/attached/changes/create_many.rb +46 -0
- data/lib/active_storage/attached/changes/create_one.rb +69 -0
- data/lib/active_storage/attached/changes/create_one_of_many.rb +10 -0
- data/lib/active_storage/attached/changes/delete_many.rb +27 -0
- data/lib/active_storage/attached/changes/delete_one.rb +19 -0
- data/lib/active_storage/attached/many.rb +16 -10
- data/lib/active_storage/attached/model.rb +147 -0
- data/lib/active_storage/attached/one.rb +16 -19
- data/lib/active_storage/downloader.rb +43 -0
- data/lib/active_storage/downloading.rb +8 -0
- data/lib/active_storage/engine.rb +43 -6
- data/lib/active_storage/errors.rb +22 -3
- data/lib/active_storage/gem_version.rb +3 -3
- data/lib/active_storage/previewer.rb +21 -11
- data/lib/active_storage/previewer/poppler_pdf_previewer.rb +2 -2
- data/lib/active_storage/previewer/video_previewer.rb +2 -3
- data/lib/active_storage/reflection.rb +64 -0
- data/lib/active_storage/service.rb +9 -6
- data/lib/active_storage/service/azure_storage_service.rb +30 -14
- data/lib/active_storage/service/configurator.rb +3 -1
- data/lib/active_storage/service/disk_service.rb +26 -14
- data/lib/active_storage/service/gcs_service.rb +49 -47
- data/lib/active_storage/service/mirror_service.rb +1 -1
- data/lib/active_storage/service/s3_service.rb +10 -6
- data/lib/active_storage/transformers/image_processing_transformer.rb +39 -0
- data/lib/active_storage/transformers/mini_magick_transformer.rb +38 -0
- data/lib/active_storage/transformers/transformer.rb +42 -0
- data/lib/tasks/activestorage.rake +7 -0
- metadata +42 -14
- data/app/models/active_storage/filename/parameters.rb +0 -36
- data/lib/active_storage/attached/macros.rb +0 -110
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 552504b488cd3a71744bf26402444008d417219053f2aaf0d56cedda76c98fd2
|
4
|
+
data.tar.gz: 6d1e4241db656db0397763c982789e0336b8cbcaa8c06cda48f9707118e967e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96773f10ef5074d0cf9f112e8b881034c94c04d9d1b370179aebb926be2ce9fc344faf7db6a9104475f852af11afae2576650f4398866fd622dadbdb554e46d1
|
7
|
+
data.tar.gz: 5a444b03f9a8ff6e6ee16c9f847049894fb84a8f2dbede5f6829855fc53d63f3b5135e75f46c3d285efbbe0ee7bb4d09333ac331ae5cc28af8f015cbb15f5732
|
data/CHANGELOG.md
CHANGED
@@ -1,84 +1,211 @@
|
|
1
|
-
## Rails
|
1
|
+
## Rails 6.0.1 (November 5, 2019) ##
|
2
2
|
|
3
|
-
*
|
3
|
+
* `ActiveStorage::AnalyzeJob`s are discarded on `ActiveRecord::RecordNotFound` errors.
|
4
4
|
|
5
|
-
|
5
|
+
*George Claghorn*
|
6
6
|
|
7
|
-
|
7
|
+
* Blobs are recorded in the database before being uploaded to the service.
|
8
|
+
This fixes that generated blob keys could silently collide, leading to
|
9
|
+
data loss.
|
8
10
|
|
11
|
+
*Julik Tarkhanov*
|
9
12
|
|
10
|
-
## Rails 5.2.1 (August 07, 2018) ##
|
11
13
|
|
12
|
-
|
14
|
+
## Rails 6.0.0 (August 16, 2019) ##
|
15
|
+
|
16
|
+
* No changes.
|
17
|
+
|
18
|
+
|
19
|
+
## Rails 6.0.0.rc2 (July 22, 2019) ##
|
20
|
+
|
21
|
+
* No changes.
|
22
|
+
|
23
|
+
|
24
|
+
## Rails 6.0.0.rc1 (April 24, 2019) ##
|
25
|
+
|
26
|
+
* Don't raise when analyzing an image whose type is unsupported by ImageMagick.
|
27
|
+
|
28
|
+
Fixes #36065.
|
29
|
+
|
30
|
+
*Guilherme Mansur*
|
31
|
+
|
32
|
+
* Permit generating variants of BMP images.
|
33
|
+
|
34
|
+
*Younes Serraj*
|
13
35
|
|
14
|
-
*George Claghorn*
|
15
36
|
|
16
|
-
|
37
|
+
## Rails 6.0.0.beta3 (March 11, 2019) ##
|
38
|
+
|
39
|
+
* No changes.
|
40
|
+
|
41
|
+
|
42
|
+
## Rails 6.0.0.beta2 (February 25, 2019) ##
|
43
|
+
|
44
|
+
* No changes.
|
45
|
+
|
46
|
+
|
47
|
+
## Rails 6.0.0.beta1 (January 18, 2019) ##
|
48
|
+
|
49
|
+
* [Rename npm package](https://github.com/rails/rails/pull/34905) from
|
50
|
+
[`activestorage`](https://www.npmjs.com/package/activestorage) to
|
51
|
+
[`@rails/activestorage`](https://www.npmjs.com/package/@rails/activestorage).
|
17
52
|
|
18
53
|
*Javan Makhmali*
|
19
54
|
|
55
|
+
* Replace `config.active_storage.queue` with two options that indicate which
|
56
|
+
queues analysis and purge jobs should use, respectively:
|
57
|
+
|
58
|
+
* `config.active_storage.queues.analysis`
|
59
|
+
* `config.active_storage.queues.purge`
|
60
|
+
|
61
|
+
`config.active_storage.queue` is preferred over the new options when it's
|
62
|
+
set, but it is deprecated and will be removed in Rails 6.1.
|
63
|
+
|
64
|
+
*George Claghorn*
|
65
|
+
|
66
|
+
* Permit generating variants of TIFF images.
|
20
67
|
|
21
|
-
|
68
|
+
*Luciano Sousa*
|
22
69
|
|
23
|
-
*
|
24
|
-
|
25
|
-
|
26
|
-
(instance or task) role credentials. Order of precedence is determined
|
27
|
-
by the [AWS SDK](https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/setup-config.html).
|
70
|
+
* Use base36 (all lowercase) for all new Blob keys to prevent
|
71
|
+
collisions and undefined behavior with case-insensitive filesystems and
|
72
|
+
database indices.
|
28
73
|
|
29
|
-
*
|
74
|
+
*Julik Tarkhanov*
|
30
75
|
|
31
|
-
*
|
76
|
+
* It doesn’t include an `X-CSRF-Token` header if a meta tag is not found on
|
77
|
+
the page. It previously included one with a value of `undefined`.
|
32
78
|
|
33
|
-
|
34
|
-
that is ambiguous in meaning. It needs to be set to the primary blob
|
35
|
-
storage endpoint but that can be determined from the blobs client anyway.
|
79
|
+
*Cameron Bothner*
|
36
80
|
|
37
|
-
|
38
|
-
now get the endpoint from the blobs client instead.
|
81
|
+
* Fix `ArgumentError` when uploading to amazon s3
|
39
82
|
|
40
|
-
|
83
|
+
*Hiroki Sanpei*
|
41
84
|
|
42
|
-
|
85
|
+
* Add progressive JPG to default list of variable content types
|
43
86
|
|
44
|
-
*
|
87
|
+
*Maurice Kühlborn*
|
45
88
|
|
46
|
-
|
89
|
+
* Add `ActiveStorage.routes_prefix` for configuring generated routes.
|
90
|
+
|
91
|
+
*Chris Bisnett*
|
92
|
+
|
93
|
+
* `ActiveStorage::Service::AzureStorageService` only handles specifically
|
94
|
+
relevant types of `Azure::Core::Http::HTTPError`. It previously obscured
|
95
|
+
other types of `HTTPError`, which is the azure-storage gem’s catch-all
|
96
|
+
exception class.
|
97
|
+
|
98
|
+
*Cameron Bothner*
|
99
|
+
|
100
|
+
* `ActiveStorage::DiskController#show` generates a 404 Not Found response when
|
101
|
+
the requested file is missing from the disk service. It previously raised
|
102
|
+
`Errno::ENOENT`.
|
103
|
+
|
104
|
+
*Cameron Bothner*
|
105
|
+
|
106
|
+
* `ActiveStorage::Blob#download` and `ActiveStorage::Blob#open` raise
|
107
|
+
`ActiveStorage::FileNotFoundError` when the corresponding file is missing
|
108
|
+
from the storage service. Services translate service-specific missing object
|
109
|
+
exceptions (e.g. `Google::Cloud::NotFoundError` for the GCS service and
|
110
|
+
`Errno::ENOENT` for the disk service) into
|
111
|
+
`ActiveStorage::FileNotFoundError`.
|
112
|
+
|
113
|
+
*Cameron Bothner*
|
114
|
+
|
115
|
+
* Added the `ActiveStorage::SetCurrent` concern for custom Active Storage
|
116
|
+
controllers that can't inherit from `ActiveStorage::BaseController`.
|
117
|
+
|
118
|
+
*George Claghorn*
|
119
|
+
|
120
|
+
* Active Storage error classes like `ActiveStorage::IntegrityError` and
|
121
|
+
`ActiveStorage::UnrepresentableError` now inherit from `ActiveStorage::Error`
|
122
|
+
instead of `StandardError`. This permits rescuing `ActiveStorage::Error` to
|
123
|
+
handle all Active Storage errors.
|
124
|
+
|
125
|
+
*Andrei Makarov*, *George Claghorn*
|
126
|
+
|
127
|
+
* Uploaded files assigned to a record are persisted to storage when the record
|
128
|
+
is saved instead of immediately.
|
129
|
+
|
130
|
+
In Rails 5.2, the following causes an uploaded file in `params[:avatar]` to
|
131
|
+
be stored:
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
@user.avatar = params[:avatar]
|
135
|
+
```
|
136
|
+
|
137
|
+
In Rails 6, the uploaded file is stored when `@user` is successfully saved.
|
47
138
|
|
48
139
|
*George Claghorn*
|
49
140
|
|
50
|
-
* Add
|
141
|
+
* Add the ability to reflect on defined attachments using the existing
|
142
|
+
ActiveRecord reflection mechanism.
|
143
|
+
|
144
|
+
*Kevin Deisz*
|
145
|
+
|
146
|
+
* Variant arguments of `false` or `nil` will no longer be passed to the
|
147
|
+
processor. For example, the following will not have the monochrome
|
148
|
+
variation applied:
|
149
|
+
|
150
|
+
```ruby
|
151
|
+
avatar.variant(monochrome: false)
|
152
|
+
```
|
153
|
+
|
154
|
+
*Jacob Smith*
|
155
|
+
|
156
|
+
* Generated attachment getter and setter methods are created
|
157
|
+
within the model's `GeneratedAssociationMethods` module to
|
158
|
+
allow overriding and composition using `super`.
|
159
|
+
|
160
|
+
*Josh Susser*, *Jamon Douglas*
|
161
|
+
|
162
|
+
* Add `ActiveStorage::Blob#open`, which downloads a blob to a tempfile on disk
|
163
|
+
and yields the tempfile. Deprecate `ActiveStorage::Downloading`.
|
164
|
+
|
165
|
+
*David Robertson*, *George Claghorn*
|
51
166
|
|
52
|
-
|
53
|
-
|
167
|
+
* Pass in `identify: false` as an argument when providing a `content_type` for
|
168
|
+
`ActiveStorage::Attached::{One,Many}#attach` to bypass automatic content
|
169
|
+
type inference. For example:
|
54
170
|
|
55
|
-
|
171
|
+
```ruby
|
172
|
+
@message.image.attach(
|
173
|
+
io: File.open('/path/to/file'),
|
174
|
+
filename: 'file.pdf',
|
175
|
+
content_type: 'application/pdf',
|
176
|
+
identify: false
|
177
|
+
)
|
178
|
+
```
|
56
179
|
|
57
|
-
*
|
58
|
-
with rectangular samples in `ActiveStorage::Analyzer::VideoAnalyzer`.
|
180
|
+
*Ryan Davidson*
|
59
181
|
|
60
|
-
|
61
|
-
|
62
|
-
its height by scaling its encoded frame width according to the DAR.
|
182
|
+
* The Google Cloud Storage service properly supports streaming downloads.
|
183
|
+
It now requires version 1.11 or newer of the google-cloud-storage gem.
|
63
184
|
|
64
185
|
*George Claghorn*
|
65
186
|
|
66
|
-
* Use
|
67
|
-
|
187
|
+
* Use the [ImageProcessing](https://github.com/janko-m/image_processing) gem
|
188
|
+
for Active Storage variants, and deprecate the MiniMagick backend.
|
68
189
|
|
69
|
-
|
190
|
+
This means that variants are now automatically oriented if the original
|
191
|
+
image was rotated. Also, in addition to the existing ImageMagick
|
192
|
+
operations, variants can now use `:resize_to_fit`, `:resize_to_fill`, and
|
193
|
+
other ImageProcessing macros. These are now recommended over raw `:resize`,
|
194
|
+
as they also sharpen the thumbnail after resizing.
|
70
195
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
196
|
+
The ImageProcessing gem also comes with a backend implemented on
|
197
|
+
[libvips](http://jcupitt.github.io/libvips/), an alternative to
|
198
|
+
ImageMagick which has significantly better performance than
|
199
|
+
ImageMagick in most cases, both in terms of speed and memory usage. In
|
200
|
+
Active Storage it's now possible to switch to the libvips backend by
|
201
|
+
changing `Rails.application.config.active_storage.variant_processor` to
|
202
|
+
`:vips`.
|
75
203
|
|
76
|
-
*
|
204
|
+
*Janko Marohnić*
|
77
205
|
|
78
|
-
*
|
206
|
+
* Rails 6 requires Ruby 2.5.0 or newer.
|
79
207
|
|
80
|
-
*
|
208
|
+
*Jeremy Daer*, *Kasper Timm Hansen*
|
81
209
|
|
82
|
-
* Added to Rails.
|
83
210
|
|
84
|
-
|
211
|
+
Please check [5-2-stable](https://github.com/rails/rails/blob/5-2-stable/activestorage/CHANGELOG.md) for previous changes.
|
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
@@ -4,11 +4,13 @@ Active Storage makes it simple to upload and reference files in cloud services l
|
|
4
4
|
|
5
5
|
Files can be uploaded from the server to the cloud or directly from the client to the cloud.
|
6
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) supported transformation.
|
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.
|
8
10
|
|
9
11
|
## Compared to other storage solutions
|
10
12
|
|
11
|
-
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/
|
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`.
|
12
14
|
|
13
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).
|
14
16
|
|
@@ -16,6 +18,8 @@ A key difference to how Active Storage works compared to other attachment soluti
|
|
16
18
|
|
17
19
|
Run `rails active_storage:install` to copy over active_storage migrations.
|
18
20
|
|
21
|
+
NOTE: If the task cannot be found, verify that `require "active_storage/engine"` is present in `config/application.rb`.
|
22
|
+
|
19
23
|
## Examples
|
20
24
|
|
21
25
|
One attachment:
|
@@ -99,7 +103,7 @@ Variation of image attachment:
|
|
99
103
|
|
100
104
|
```erb
|
101
105
|
<%# Hitting the variant URL will lazy transform the original blob and then redirect to its new service location %>
|
102
|
-
<%= image_tag user.avatar.variant(
|
106
|
+
<%= image_tag user.avatar.variant(resize_to_limit: [100, 100]) %>
|
103
107
|
```
|
104
108
|
|
105
109
|
## Direct uploads
|
@@ -116,8 +120,7 @@ Active Storage, with its included JavaScript library, supports uploading directl
|
|
116
120
|
```
|
117
121
|
Using the npm package:
|
118
122
|
```js
|
119
|
-
|
120
|
-
ActiveStorage.start()
|
123
|
+
require("@rails/activestorage").start()
|
121
124
|
```
|
122
125
|
2. Annotate file inputs with the direct upload URL.
|
123
126
|
|
@@ -148,7 +151,7 @@ Active Storage is released under the [MIT License](https://opensource.org/licens
|
|
148
151
|
|
149
152
|
API documentation is at:
|
150
153
|
|
151
|
-
*
|
154
|
+
* https://api.rubyonrails.org
|
152
155
|
|
153
156
|
Bug reports for the Ruby on Rails project can be filed here:
|
154
157
|
|
@@ -1 +1,942 @@
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.ActiveStorage=e():t.ActiveStorage=e()}(this,function(){return function(t){function e(n){if(r[n])return r[n].exports;var i=r[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,e),i.l=!0,i.exports}var r={};return e.m=t,e.c=r,e.d=function(t,r,n){e.o(t,r)||Object.defineProperty(t,r,{configurable:!1,enumerable:!0,get:n})},e.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(r,"a",r),r},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=2)}([function(t,e,r){"use strict";function n(t){var e=a(document.head,'meta[name="'+t+'"]');if(e)return e.getAttribute("content")}function i(t,e){return"string"==typeof t&&(e=t,t=document),o(t.querySelectorAll(e))}function a(t,e){return"string"==typeof t&&(e=t,t=document),t.querySelector(e)}function u(t,e){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},n=t.disabled,i=r.bubbles,a=r.cancelable,u=r.detail,o=document.createEvent("Event");o.initEvent(e,i||!0,a||!0),o.detail=u||{};try{t.disabled=!1,t.dispatchEvent(o)}finally{t.disabled=n}return o}function o(t){return Array.isArray(t)?t:Array.from?Array.from(t):[].slice.call(t)}e.d=n,e.c=i,e.b=a,e.a=u,e.e=o},function(t,e,r){"use strict";function n(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function i(t,e){if(t&&"function"==typeof t[e]){for(var r=arguments.length,n=Array(r>2?r-2:0),i=2;i
|
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
|
+
});
|