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,51 @@
1
+ export function getMetaValue(name) {
2
+ const element = findElement(document.head, `meta[name="${name}"]`)
3
+ if (element) {
4
+ return element.getAttribute("content")
5
+ }
6
+ }
7
+
8
+ export function findElements(root, selector) {
9
+ if (typeof root == "string") {
10
+ selector = root
11
+ root = document
12
+ }
13
+ const elements = root.querySelectorAll(selector)
14
+ return toArray(elements)
15
+ }
16
+
17
+ export function findElement(root, selector) {
18
+ if (typeof root == "string") {
19
+ selector = root
20
+ root = document
21
+ }
22
+ return root.querySelector(selector)
23
+ }
24
+
25
+ export function dispatchEvent(element, type, eventInit = {}) {
26
+ const { disabled } = element
27
+ const { bubbles, cancelable, detail } = eventInit
28
+ const event = document.createEvent("Event")
29
+
30
+ event.initEvent(type, bubbles || true, cancelable || true)
31
+ event.detail = detail || {}
32
+
33
+ try {
34
+ element.disabled = false
35
+ element.dispatchEvent(event)
36
+ } finally {
37
+ element.disabled = disabled
38
+ }
39
+
40
+ return event
41
+ }
42
+
43
+ export function toArray(value) {
44
+ if (Array.isArray(value)) {
45
+ return value
46
+ } else if (Array.from) {
47
+ return Array.from(value)
48
+ } else {
49
+ return [].slice.call(value)
50
+ }
51
+ }
@@ -0,0 +1,11 @@
1
+ import { start } from "./ujs"
2
+ import { DirectUpload } from "./direct_upload"
3
+ export { start, DirectUpload }
4
+
5
+ function autostart() {
6
+ if (window.ActiveStorage) {
7
+ start()
8
+ }
9
+ }
10
+
11
+ setTimeout(autostart, 1)
@@ -0,0 +1,86 @@
1
+ import { DirectUploadsController } from "./direct_uploads_controller"
2
+ import { findElement } from "./helpers"
3
+
4
+ const processingAttribute = "data-direct-uploads-processing"
5
+ const submitButtonsByForm = new WeakMap
6
+ let started = false
7
+
8
+ export function start() {
9
+ if (!started) {
10
+ started = true
11
+ document.addEventListener("click", didClick, true)
12
+ document.addEventListener("submit", didSubmitForm)
13
+ document.addEventListener("ajax:before", didSubmitRemoteElement)
14
+ }
15
+ }
16
+
17
+ function didClick(event) {
18
+ const { target } = event
19
+ if ((target.tagName == "INPUT" || target.tagName == "BUTTON") && target.type == "submit" && target.form) {
20
+ submitButtonsByForm.set(target.form, target)
21
+ }
22
+ }
23
+
24
+ function didSubmitForm(event) {
25
+ handleFormSubmissionEvent(event)
26
+ }
27
+
28
+ function didSubmitRemoteElement(event) {
29
+ if (event.target.tagName == "FORM") {
30
+ handleFormSubmissionEvent(event)
31
+ }
32
+ }
33
+
34
+ function handleFormSubmissionEvent(event) {
35
+ const form = event.target
36
+
37
+ if (form.hasAttribute(processingAttribute)) {
38
+ event.preventDefault()
39
+ return
40
+ }
41
+
42
+ const controller = new DirectUploadsController(form)
43
+ const { inputs } = controller
44
+
45
+ if (inputs.length) {
46
+ event.preventDefault()
47
+ form.setAttribute(processingAttribute, "")
48
+ inputs.forEach(disable)
49
+ controller.start(error => {
50
+ form.removeAttribute(processingAttribute)
51
+ if (error) {
52
+ inputs.forEach(enable)
53
+ } else {
54
+ submitForm(form)
55
+ }
56
+ })
57
+ }
58
+ }
59
+
60
+ function submitForm(form) {
61
+ let button = submitButtonsByForm.get(form) || findElement(form, "input[type=submit], button[type=submit]")
62
+
63
+ if (button) {
64
+ const { disabled } = button
65
+ button.disabled = false
66
+ button.focus()
67
+ button.click()
68
+ button.disabled = disabled
69
+ } else {
70
+ button = document.createElement("input")
71
+ button.type = "submit"
72
+ button.style.display = "none"
73
+ form.appendChild(button)
74
+ button.click()
75
+ form.removeChild(button)
76
+ }
77
+ submitButtonsByForm.delete(form)
78
+ }
79
+
80
+ function disable(input) {
81
+ input.disabled = true
82
+ }
83
+
84
+ function enable(input) {
85
+ input.disabled = false
86
+ }
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Provides asynchronous analysis of ActiveStorage::Blob records via ActiveStorage::Blob#analyze_later.
4
+ class ActiveStorage::AnalyzeJob < ActiveStorage::BaseJob
5
+ queue_as { ActiveStorage.queues[:analysis] }
6
+
7
+ retry_on ActiveStorage::IntegrityError, attempts: 10, wait: :exponentially_longer
8
+
9
+ def perform(blob)
10
+ blob.analyze
11
+ end
12
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ActiveStorage::BaseJob < ActiveJob::Base
4
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Provides asynchronous purging of ActiveStorage::Blob records via ActiveStorage::Blob#purge_later.
4
+ class ActiveStorage::PurgeJob < ActiveStorage::BaseJob
5
+ queue_as { ActiveStorage.queues[:purge] }
6
+
7
+ discard_on ActiveRecord::RecordNotFound
8
+ retry_on ActiveRecord::Deadlocked, attempts: 10, wait: :exponentially_longer
9
+
10
+ def perform(blob)
11
+ blob.purge
12
+ end
13
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/module/delegation"
4
+
5
+ # Attachments associate records with blobs. Usually that's a one record-many blobs relationship,
6
+ # but it is possible to associate many different records with the same blob. A foreign-key constraint
7
+ # on the attachments table prevents blobs from being purged if they’re still attached to any records.
8
+ class ActiveStorage::Attachment < ActiveRecord::Base
9
+ self.table_name = "active_storage_attachments"
10
+
11
+ belongs_to :record, polymorphic: true, touch: true
12
+ belongs_to :blob, class_name: "ActiveStorage::Blob"
13
+
14
+ delegate_missing_to :blob
15
+
16
+ after_create_commit :analyze_blob_later, :identify_blob
17
+ after_destroy_commit :purge_dependent_blob_later
18
+
19
+ # Synchronously deletes the attachment and {purges the blob}[rdoc-ref:ActiveStorage::Blob#purge].
20
+ def purge
21
+ delete
22
+ blob&.purge
23
+ end
24
+
25
+ # Deletes the attachment and {enqueues a background job}[rdoc-ref:ActiveStorage::Blob#purge_later] to purge the blob.
26
+ def purge_later
27
+ delete
28
+ blob&.purge_later
29
+ end
30
+
31
+ private
32
+ def identify_blob
33
+ blob.identify
34
+ end
35
+
36
+ def analyze_blob_later
37
+ blob.analyze_later unless blob.analyzed?
38
+ end
39
+
40
+ def purge_dependent_blob_later
41
+ blob&.purge_later if dependent == :purge_later
42
+ end
43
+
44
+
45
+ def dependent
46
+ record.attachment_reflections[name]&.options[:dependent]
47
+ end
48
+ end
49
+
50
+ ActiveSupport.run_load_hooks :active_storage_attachment, ActiveStorage::Attachment
@@ -0,0 +1,278 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_storage/downloader"
4
+
5
+ # A blob is a record that contains the metadata about a file and a key for where that file resides on the service.
6
+ # Blobs can be created in two ways:
7
+ #
8
+ # 1. Subsequent to the file being uploaded server-side to the service via <tt>create_after_upload!</tt>.
9
+ # 2. Ahead of the file being directly uploaded client-side to the service via <tt>create_before_direct_upload!</tt>.
10
+ #
11
+ # The first option doesn't require any client-side JavaScript integration, and can be used by any other back-end
12
+ # service that deals with files. The second option is faster, since you're not using your own server as a staging
13
+ # point for uploads, and can work with deployments like Heroku that do not provide large amounts of disk space.
14
+ #
15
+ # Blobs are intended to be immutable in as-so-far as their reference to a specific file goes. You're allowed to
16
+ # update a blob's metadata on a subsequent pass, but you should not update the key or change the uploaded file.
17
+ # If you need to create a derivative or otherwise change the blob, simply create a new blob and purge the old one.
18
+ class ActiveStorage::Blob < ActiveRecord::Base
19
+ require_dependency "active_storage/blob/analyzable"
20
+ require_dependency "active_storage/blob/identifiable"
21
+ require_dependency "active_storage/blob/representable"
22
+
23
+ include Analyzable
24
+ include Identifiable
25
+ include Representable
26
+
27
+ self.table_name = "active_storage_blobs"
28
+
29
+ has_secure_token :key
30
+ store :metadata, accessors: [ :analyzed, :identified ], coder: ActiveRecord::Coders::JSON
31
+
32
+ class_attribute :service
33
+
34
+ has_many :attachments
35
+
36
+ scope :unattached, -> { left_joins(:attachments).where(ActiveStorage::Attachment.table_name => { blob_id: nil }) }
37
+
38
+ before_destroy(prepend: true) do
39
+ raise ActiveRecord::InvalidForeignKey if attachments.exists?
40
+ end
41
+
42
+ class << self
43
+ # You can use the signed ID of a blob to refer to it on the client side without fear of tampering.
44
+ # This is particularly helpful for direct uploads where the client-side needs to refer to the blob
45
+ # that was created ahead of the upload itself on form submission.
46
+ #
47
+ # The signed ID is also used to create stable URLs for the blob through the BlobsController.
48
+ def find_signed(id)
49
+ find ActiveStorage.verifier.verify(id, purpose: :blob_id)
50
+ end
51
+
52
+ # Returns a new, unsaved blob instance after the +io+ has been uploaded to the service.
53
+ # When providing a content type, pass <tt>identify: false</tt> to bypass automatic content type inference.
54
+ def build_after_upload(io:, filename:, content_type: nil, metadata: nil, identify: true)
55
+ new(filename: filename, content_type: content_type, metadata: metadata).tap do |blob|
56
+ blob.upload(io, identify: identify)
57
+ end
58
+ end
59
+
60
+ def build_after_unfurling(io:, filename:, content_type: nil, metadata: nil, identify: true) #:nodoc:
61
+ new(filename: filename, content_type: content_type, metadata: metadata).tap do |blob|
62
+ blob.unfurl(io, identify: identify)
63
+ end
64
+ end
65
+
66
+ # Returns a saved blob instance after the +io+ has been uploaded to the service. Note, the blob is first built,
67
+ # then the +io+ is uploaded, then the blob is saved. This is done this way to avoid uploading (which may take
68
+ # time), while having an open database transaction.
69
+ # When providing a content type, pass <tt>identify: false</tt> to bypass automatic content type inference.
70
+ def create_after_upload!(io:, filename:, content_type: nil, metadata: nil, identify: true)
71
+ build_after_upload(io: io, filename: filename, content_type: content_type, metadata: metadata, identify: identify).tap(&:save!)
72
+ end
73
+
74
+ # Returns a saved blob _without_ uploading a file to the service. This blob will point to a key where there is
75
+ # no file yet. It's intended to be used together with a client-side upload, which will first create the blob
76
+ # in order to produce the signed URL for uploading. This signed URL points to the key generated by the blob.
77
+ # Once the form using the direct upload is submitted, the blob can be associated with the right record using
78
+ # the signed ID.
79
+ def create_before_direct_upload!(filename:, byte_size:, checksum:, content_type: nil, metadata: nil)
80
+ create! filename: filename, byte_size: byte_size, checksum: checksum, content_type: content_type, metadata: metadata
81
+ end
82
+
83
+ # To prevent problems with case-insensitive filesystems, especially in combination
84
+ # with databases which treat indices as case-sensitive, all blob keys generated are going
85
+ # to only contain the base-36 character alphabet and will therefore be lowercase. To maintain
86
+ # the same or higher amount of entropy as in the base-58 encoding used by `has_secure_token`
87
+ # the number of bytes used is increased to 28 from the standard 24
88
+ def generate_unique_secure_token
89
+ SecureRandom.base36(28)
90
+ end
91
+ end
92
+
93
+ # Returns a signed ID for this blob that's suitable for reference on the client-side without fear of tampering.
94
+ # It uses the framework-wide verifier on <tt>ActiveStorage.verifier</tt>, but with a dedicated purpose.
95
+ def signed_id
96
+ ActiveStorage.verifier.generate(id, purpose: :blob_id)
97
+ end
98
+
99
+ # Returns the key pointing to the file on the service that's associated with this blob. The key is the
100
+ # secure-token format from Rails in lower case. So it'll look like: xtapjjcjiudrlk3tmwyjgpuobabd.
101
+ # This key is not intended to be revealed directly to the user.
102
+ # Always refer to blobs using the signed_id or a verified form of the key.
103
+ def key
104
+ # We can't wait until the record is first saved to have a key for it
105
+ self[:key] ||= self.class.generate_unique_secure_token
106
+ end
107
+
108
+ # Returns an ActiveStorage::Filename instance of the filename that can be
109
+ # queried for basename, extension, and a sanitized version of the filename
110
+ # that's safe to use in URLs.
111
+ def filename
112
+ ActiveStorage::Filename.new(self[:filename])
113
+ end
114
+
115
+ # Returns true if the content_type of this blob is in the image range, like image/png.
116
+ def image?
117
+ content_type.start_with?("image")
118
+ end
119
+
120
+ # Returns true if the content_type of this blob is in the audio range, like audio/mpeg.
121
+ def audio?
122
+ content_type.start_with?("audio")
123
+ end
124
+
125
+ # Returns true if the content_type of this blob is in the video range, like video/mp4.
126
+ def video?
127
+ content_type.start_with?("video")
128
+ end
129
+
130
+ # Returns true if the content_type of this blob is in the text range, like text/plain.
131
+ def text?
132
+ content_type.start_with?("text")
133
+ end
134
+
135
+
136
+ # Returns the URL of the blob on the service. This URL is intended to be short-lived for security and not used directly
137
+ # with users. Instead, the +service_url+ should only be exposed as a redirect from a stable, possibly authenticated URL.
138
+ # Hiding the +service_url+ behind a redirect also gives you the power to change services without updating all URLs. And
139
+ # it allows permanent URLs that redirect to the +service_url+ to be cached in the view.
140
+ def service_url(expires_in: ActiveStorage.service_urls_expire_in, disposition: :inline, filename: nil, **options)
141
+ filename = ActiveStorage::Filename.wrap(filename || self.filename)
142
+
143
+ service.url key, expires_in: expires_in, filename: filename, content_type: content_type_for_service_url,
144
+ disposition: forced_disposition_for_service_url || disposition, **options
145
+ end
146
+
147
+ # Returns a URL that can be used to directly upload a file for this blob on the service. This URL is intended to be
148
+ # short-lived for security and only generated on-demand by the client-side JavaScript responsible for doing the uploading.
149
+ def service_url_for_direct_upload(expires_in: ActiveStorage.service_urls_expire_in)
150
+ service.url_for_direct_upload key, expires_in: expires_in, content_type: content_type, content_length: byte_size, checksum: checksum
151
+ end
152
+
153
+ # Returns a Hash of headers for +service_url_for_direct_upload+ requests.
154
+ def service_headers_for_direct_upload
155
+ service.headers_for_direct_upload key, filename: filename, content_type: content_type, content_length: byte_size, checksum: checksum
156
+ end
157
+
158
+
159
+ # Uploads the +io+ to the service on the +key+ for this blob. Blobs are intended to be immutable, so you shouldn't be
160
+ # using this method after a file has already been uploaded to fit with a blob. If you want to create a derivative blob,
161
+ # you should instead simply create a new blob based on the old one.
162
+ #
163
+ # Prior to uploading, we compute the checksum, which is sent to the service for transit integrity validation. If the
164
+ # checksum does not match what the service receives, an exception will be raised. We also measure the size of the +io+
165
+ # and store that in +byte_size+ on the blob record. The content type is automatically extracted from the +io+ unless
166
+ # you specify a +content_type+ and pass +identify+ as false.
167
+ #
168
+ # Normally, you do not have to call this method directly at all. Use the factory class methods of +build_after_upload+
169
+ # and +create_after_upload!+.
170
+ def upload(io, identify: true)
171
+ unfurl io, identify: identify
172
+ upload_without_unfurling io
173
+ end
174
+
175
+ def unfurl(io, identify: true) #:nodoc:
176
+ self.checksum = compute_checksum_in_chunks(io)
177
+ self.content_type = extract_content_type(io) if content_type.nil? || identify
178
+ self.byte_size = io.size
179
+ self.identified = true
180
+ end
181
+
182
+ def upload_without_unfurling(io) #:nodoc:
183
+ service.upload key, io, checksum: checksum, **service_metadata
184
+ end
185
+
186
+ # Downloads the file associated with this blob. If no block is given, the entire file is read into memory and returned.
187
+ # That'll use a lot of RAM for very large files. If a block is given, then the download is streamed and yielded in chunks.
188
+ def download(&block)
189
+ service.download key, &block
190
+ end
191
+
192
+ # Downloads the blob to a tempfile on disk. Yields the tempfile.
193
+ #
194
+ # The tempfile's name is prefixed with +ActiveStorage-+ and the blob's ID. Its extension matches that of the blob.
195
+ #
196
+ # By default, the tempfile is created in <tt>Dir.tmpdir</tt>. Pass +tmpdir:+ to create it in a different directory:
197
+ #
198
+ # blob.open(tmpdir: "/path/to/tmp") do |file|
199
+ # # ...
200
+ # end
201
+ #
202
+ # The tempfile is automatically closed and unlinked after the given block is executed.
203
+ #
204
+ # Raises ActiveStorage::IntegrityError if the downloaded data does not match the blob's checksum.
205
+ def open(tmpdir: nil, &block)
206
+ service.open key, checksum: checksum,
207
+ name: [ "ActiveStorage-#{id}-", filename.extension_with_delimiter ], tmpdir: tmpdir, &block
208
+ end
209
+
210
+
211
+ # Deletes the files on the service associated with the blob. This should only be done if the blob is going to be
212
+ # deleted as well or you will essentially have a dead reference. It's recommended to use #purge and #purge_later
213
+ # methods in most circumstances.
214
+ def delete
215
+ service.delete(key)
216
+ service.delete_prefixed("variants/#{key}/") if image?
217
+ end
218
+
219
+ # Deletes the file on the service and then destroys the blob record. This is the recommended way to dispose of unwanted
220
+ # blobs. Note, though, that deleting the file off the service will initiate a HTTP connection to the service, which may
221
+ # be slow or prevented, so you should not use this method inside a transaction or in callbacks. Use #purge_later instead.
222
+ def purge
223
+ destroy
224
+ delete
225
+ rescue ActiveRecord::InvalidForeignKey
226
+ end
227
+
228
+ # Enqueues an ActiveStorage::PurgeJob to call #purge. This is the recommended way to purge blobs from a transaction,
229
+ # an Active Record callback, or in any other real-time scenario.
230
+ def purge_later
231
+ ActiveStorage::PurgeJob.perform_later(self)
232
+ end
233
+
234
+ private
235
+ def compute_checksum_in_chunks(io)
236
+ Digest::MD5.new.tap do |checksum|
237
+ while chunk = io.read(5.megabytes)
238
+ checksum << chunk
239
+ end
240
+
241
+ io.rewind
242
+ end.base64digest
243
+ end
244
+
245
+ def extract_content_type(io)
246
+ Marcel::MimeType.for io, name: filename.to_s, declared_type: content_type
247
+ end
248
+
249
+ def forcibly_serve_as_binary?
250
+ ActiveStorage.content_types_to_serve_as_binary.include?(content_type)
251
+ end
252
+
253
+ def allowed_inline?
254
+ ActiveStorage.content_types_allowed_inline.include?(content_type)
255
+ end
256
+
257
+ def content_type_for_service_url
258
+ forcibly_serve_as_binary? ? ActiveStorage.binary_content_type : content_type
259
+ end
260
+
261
+ def forced_disposition_for_service_url
262
+ if forcibly_serve_as_binary? || !allowed_inline?
263
+ :attachment
264
+ end
265
+ end
266
+
267
+ def service_metadata
268
+ if forcibly_serve_as_binary?
269
+ { content_type: ActiveStorage.binary_content_type, disposition: :attachment, filename: filename }
270
+ elsif !allowed_inline?
271
+ { content_type: content_type, disposition: :attachment, filename: filename }
272
+ else
273
+ { content_type: content_type }
274
+ end
275
+ end
276
+ end
277
+
278
+ ActiveSupport.run_load_hooks :active_storage_blob, ActiveStorage::Blob