Repository navigation
Expand file tree
/
Copy pathblob.rb
More file actions
483 lines (416 loc) · 20.3 KB
/
Copy pathblob.rb
File metadata and controls
483 lines (416 loc) · 20.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# frozen_string_literal: true
# Active Storage \Blob
# ====================
#
# A blob is a record that contains the metadata about a file and a key for where that file resides on the service.
# Blobs can be created in two ways:
#
# 1. Ahead of the file being uploaded server-side to the service, via `create_and_upload!`. A rewindable
# `io` with the file contents must be available at the server for this operation.
# 2. Ahead of the file being directly uploaded client-side to the service, via `create_before_direct_upload!`.
#
# The first option doesn't require any client-side JavaScript integration, and can be used by any other back-end
# service that deals with files. The second option is faster, since you're not using your own server as a staging
# point for uploads, and can work with deployments like Heroku that do not provide large amounts of disk space.
#
# Blobs are intended to be immutable in as-so-far as their reference to a specific file goes. You're allowed to
# update a blob's metadata on a subsequent pass, but you should not update the key or change the uploaded file.
# If you need to create a derivative or otherwise change the blob, simply create a new blob and purge the old one.
#
# When using a custom `key`, the value is treated as trusted. Using untrusted user input
# as the key may result in unexpected behavior.
class ActiveStorage::Blob < ActiveStorage::Record
MINIMUM_TOKEN_LENGTH = 28
has_secure_token :key, length: MINIMUM_TOKEN_LENGTH
# FIXME: these property should never have been stored in the metadata.
# The blob table should be migrated to have dedicated columns for these.
PROTECTED_METADATA = %w(analyzed identified composed).freeze
private_constant :PROTECTED_METADATA
store :metadata, accessors: [ :analyzed, :identified, :composed ], coder: ActiveRecord::Coders::JSON
# Temporary reference to a local io during the upload flow. When set,
# `open` will use this instead of downloading from the service. This
# enables analysis and other operations to run on the local file before
# uploading, avoiding a download round-trip.
attr_accessor :local_io
class_attribute :services, default: {}
class_attribute :service, instance_accessor: false
##
# :method:
#
# Returns the associated ActiveStorage::Attachment instances.
has_many :attachments, autosave: false
##
# :singleton-method:
#
# Returns the blobs that aren't attached to any record.
scope :unattached, -> { where.missing(:attachments) }
after_initialize do
self.service_name ||= self.class.service&.name
end
after_update :touch_attachments
after_update_commit :sync_metadata_later, if: -> { content_type_previously_changed? || metadata_previously_changed? }
before_destroy(prepend: true) do
raise ActiveRecord::InvalidForeignKey if attachments.exists?
end
validates :service_name, presence: true
validates :checksum, presence: true, unless: :composed
validate do
if service_name_changed? && service_name.present?
services.fetch(service_name) do
errors.add(:service_name, :invalid)
end
end
end
class << self
# You can use the signed ID of a blob to refer to it on the client side without fear of tampering.
# This is particularly helpful for direct uploads where the client-side needs to refer to the blob
# that was created ahead of the upload itself on form submission.
#
# The signed ID is also used to create stable URLs for the blob through the BlobsController.
def find_signed(id, record: nil, purpose: :blob_id)
super(id, purpose: purpose)
end
# Works like `find_signed`, but will raise an `ActiveSupport::MessageVerifier::InvalidSignature`
# exception if the `signed_id` has either expired, has a purpose mismatch, or has been tampered with.
# It will also raise an `ActiveRecord::RecordNotFound` exception if the valid signed id can't find a record.
def find_signed!(id, record: nil, purpose: :blob_id)
super(id, purpose: purpose)
end
def build_after_unfurling(key: nil, io:, filename:, content_type: nil, metadata: nil, service_name: nil, identify: true, record: nil) # :nodoc:
new(key: key, filename: filename, content_type: content_type, metadata: metadata, service_name: service_name).tap do |blob|
blob.unfurl(io, identify: identify)
end
end
def create_after_unfurling!(key: nil, io:, filename:, content_type: nil, metadata: nil, service_name: nil, identify: true, record: nil) # :nodoc:
build_after_unfurling(key: key, io: io, filename: filename, content_type: content_type, metadata: metadata, service_name: service_name, identify: identify).tap(&:save!)
end
# Creates a new blob instance and then uploads the contents of
# the given `io` to the service. The blob instance is going to
# be saved before the upload begins to prevent the upload clobbering another due to key collisions.
# When providing a content type, pass `identify: false` to bypass
# automatic content type inference.
#
# The optional `key` parameter is treated as trusted. Using untrusted user input
# as the key may result in unexpected behavior.
def create_and_upload!(key: nil, io:, filename:, content_type: nil, metadata: nil, service_name: nil, identify: true, record: nil)
create_after_unfurling!(key: key, io: io, filename: filename, content_type: content_type, metadata: metadata, service_name: service_name, identify: identify).tap do |blob|
blob.upload_without_unfurling(io)
end
end
# Returns a saved blob *without* uploading a file to the service. This blob will point to a key where there is
# no file yet. It's intended to be used together with a client-side upload, which will first create the blob
# in order to produce the signed URL for uploading. This signed URL points to the key generated by the blob.
# Once the form using the direct upload is submitted, the blob can be associated with the right record using
# the signed ID.
def create_before_direct_upload!(key: nil, filename:, byte_size:, checksum:, content_type: nil, metadata: nil, service_name: nil, record: nil)
metadata = filter_metadata(metadata)
create! key: key, filename: filename, byte_size: byte_size, checksum: checksum, content_type: content_type, metadata: metadata, service_name: service_name
end
# To prevent problems with case-insensitive filesystems, especially in combination
# with databases which treat indices as case-sensitive, all blob keys generated are going
# to only contain the base-36 character alphabet and will therefore be lowercase. To maintain
# the same or higher amount of entropy as in the base-58 encoding used by `has_secure_token`
# the number of bytes used is increased to 28 from the standard 24
def generate_unique_secure_token(length: MINIMUM_TOKEN_LENGTH)
SecureRandom.base36(length)
end
# Customize signed ID purposes for backwards compatibility.
def combine_signed_id_purposes(purpose) # :nodoc:
purpose.to_s
end
# Customize the default signed ID verifier for backwards compatibility.
#
# We override the reader (.signed_id_verifier) instead of just calling the writer (.signed_id_verifier=)
# to guard against the case where ActiveStorage.verifier isn't yet initialized at load time.
def signed_id_verifier # :nodoc:
@signed_id_verifier ||= ActiveStorage.verifier
end
def scope_for_strict_loading # :nodoc:
if self.strict_loading_by_default? && ActiveStorage.track_variants
includes(
variant_records: { image_attachment: :blob },
preview_image_attachment: { blob: { variant_records: { image_attachment: :blob } } }
)
else
all
end
end
# Concatenate multiple blobs into a single "composed" blob.
def compose(blobs, key: nil, filename:, content_type: nil, metadata: nil)
raise ActiveRecord::RecordNotSaved, "All blobs must be persisted." if blobs.any?(&:new_record?)
content_type ||= blobs.pluck(:content_type).compact.first
new(key: key, filename: filename, content_type: content_type, metadata: metadata, byte_size: blobs.sum(&:byte_size)).tap do |combined_blob|
combined_blob.compose(blobs.pluck(:key))
combined_blob.save!
end
end
private
def filter_metadata(metadata)
if metadata.is_a?(Hash)
metadata.without(*PROTECTED_METADATA)
else
metadata
end
end
end
include Analyzable
include Identifiable
include Representable
include Servable
##
# :method: content_type
#
# Returns the content type of the associated file:
#
# ```
# ActiveStorage::Blob.first.content_type
# => "image/png"
# ```
##
# :method: metadata
#
# Returns a Hash of metadata extracted from the associated file.
# ActiveStorage also stores whether the file has been identified and analyzed:
#
# ```
# ActiveStorage::Blob.first.metadata
# => {"identified" => true, "width" => 763, "height" => 588, "analyzed" => true}
# ```
# Returns a signed ID for this blob that's suitable for reference on the client-side without fear of tampering.
def signed_id(purpose: :blob_id, expires_in: nil, expires_at: nil)
super
end
# Returns the key pointing to the file on the service that's associated with this blob. The key is the
# secure-token format from \Rails in lower case. So it'll look like: xtapjjcjiudrlk3tmwyjgpuobabd.
# This key is not intended to be revealed directly to the user.
# Always refer to blobs using the signed_id or a verified form of the key.
def key
# We can't wait until the record is first saved to have a key for it
self[:key] ||= self.class.generate_unique_secure_token(length: MINIMUM_TOKEN_LENGTH)
end
# Returns an ActiveStorage::Filename instance of the filename that can be
# queried for basename, extension, and a sanitized version of the filename
# that's safe to use in URLs.
def filename
ActiveStorage::Filename.new(self[:filename])
end
# Returns a Hash of the custom metadata to be stored on the cloud storage provider.
def custom_metadata
self[:metadata][:custom] || {}
end
# Sets custom metadata to be stored on the cloud storage provider.
# Keys should not contain the cloud storage provider prefix as these get added automatically.
#
# ```
# blob = ActiveStorage::Blob.new
# blob.custom_metadata = { optimized: true }
# blob.service_headers_for_direct_upload["x-amz-meta-optimized"]
# => true
# ```
def custom_metadata=(metadata)
self[:metadata] = self[:metadata].merge(custom: metadata)
end
# Returns true if the content_type of this blob is in the image range, like image/png.
def image?
content_type&.start_with?("image")
end
# Returns true if the content_type of this blob is in the audio range, like audio/mpeg.
def audio?
content_type&.start_with?("audio")
end
# Returns true if the content_type of this blob is in the video range, like video/mp4.
def video?
content_type&.start_with?("video")
end
# Returns true if the content_type of this blob is in the text range, like text/plain.
def text?
content_type&.start_with?("text")
end
# Returns the URL of the blob on the service. This returns a permanent URL for public files, and returns a
# short-lived URL for private files. Private files are signed, and not for public use. Instead,
# the URL should only be exposed as a redirect from a stable, possibly authenticated URL. Hiding the
# URL behind a redirect also allows you to change services without updating all URLs.
def url(expires_in: ActiveStorage.service_urls_expire_in, disposition: :inline, filename: nil, **options)
service.url key, expires_in: expires_in, filename: ActiveStorage::Filename.wrap(filename || self.filename),
content_type: content_type_for_serving, disposition: forced_disposition_for_serving || disposition, **options
end
# Returns a URL that can be used to directly upload a file for this blob on the service. This URL is intended to be
# short-lived for security and only generated on-demand by the client-side JavaScript responsible for doing the uploading.
def service_url_for_direct_upload(expires_in: ActiveStorage.service_urls_expire_in)
service.url_for_direct_upload key, expires_in: expires_in, content_type: content_type, content_length: byte_size, checksum: checksum, custom_metadata: custom_metadata
end
# Returns a Hash of headers for `service_url_for_direct_upload` requests.
def service_headers_for_direct_upload
service.headers_for_direct_upload key, filename: filename, content_type: content_type, content_length: byte_size, checksum: checksum, custom_metadata: custom_metadata
end
# Uploads the `io` to the service on the `key` for this blob. Blobs are intended to be immutable, so you shouldn't be
# using this method after a file has already been uploaded to fit with a blob. If you want to create a derivative blob,
# you should instead simply create a new blob based on the old one.
#
# Prior to uploading, we compute the checksum, which is sent to the service for transit integrity validation. If the
# checksum does not match what the service receives, an exception will be raised. We also measure the size of the `io`
# and store that in `byte_size` on the blob record. The content type is automatically extracted from the `io` unless
# you specify a `content_type` and pass `identify` as false.
#
# Normally, you do not have to call this method directly at all. Use the `create_and_upload!` class method instead.
# If you do use this method directly, make sure you are using it on a persisted Blob as otherwise another blob's
# data might get overwritten on the service.
def upload(io, identify: true)
unfurl io, identify: identify
upload_without_unfurling io
end
def unfurl(io, identify: true) # :nodoc:
self.checksum = service&.compute_checksum(io)
self.content_type = extract_content_type(io) if content_type.nil? || identify
self.byte_size = io.size
self.identified = true
end
def upload_without_unfurling(io) # :nodoc:
service.upload key, io, checksum: checksum, **service_metadata
end
def compose(keys) # :nodoc:
self.composed = true
service.compose(keys, key, **service_metadata)
end
def service_metadata # :nodoc:
if forcibly_serve_as_binary?
{ content_type: ActiveStorage.binary_content_type, disposition: :attachment, filename: filename, custom_metadata: custom_metadata }
elsif !allowed_inline?
{ content_type: content_type, disposition: :attachment, filename: filename, custom_metadata: custom_metadata }
else
{ content_type: content_type, custom_metadata: custom_metadata }
end
end
# Downloads the file associated with this blob. If no block is given, the entire file is read into memory and returned.
# 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.
def download(&block)
service.download key, &block
end
# Downloads a part of the file associated with this blob.
def download_chunk(range)
service.download_chunk key, range
end
# Downloads the blob to a temporary file on disk. If a block is given, the file is automatically closed and unlinked
# after the block executed. Otherwise the file is returned and you are responsible for closing and unlinking.
#
# The tempfile's name is prefixed with `ActiveStorage-` and the blob's ID. It keeps the blob filename's
# extension when that extension denotes the blob's content type, and has no extension otherwise.
#
# By default, the tempfile is created in `Dir.tmpdir`. Pass `tmpdir:` to create it in a different directory:
#
# ```
# blob.open(tmpdir: "/path/to/tmp") do |file|
# # ...
# end
# ```
#
# Raises ActiveStorage::IntegrityError if the downloaded data does not match the blob's checksum.
def open(tmpdir: nil, &block)
if local_io
open_local_io(tmpdir: tmpdir, &block)
else
service.open(
key,
checksum: checksum,
verify: !composed,
name: [ "ActiveStorage-#{id}-", tempfile_extension ],
tmpdir: tmpdir,
&block
)
end
end
def mirror_later # :nodoc:
service.mirror_later key, checksum: checksum if service.respond_to?(:mirror_later)
end
# Deletes the files on the service associated with the blob. This should only be done if the blob is going to be
# deleted as well or you will essentially have a dead reference. It's recommended to use #purge and #purge_later
# methods in most circumstances.
def delete
service.delete(key)
service.delete_prefixed("variants/#{key}/") if image?
end
# Destroys the blob record and then deletes the file on the service. This is the recommended way to dispose of unwanted
# blobs. Note, though, that deleting the file off the service will initiate an HTTP connection to the service, which may
# be slow or prevented, so you should not use this method inside a transaction or in callbacks. Use #purge_later instead.
def purge
destroy
delete if previously_persisted?
rescue ActiveRecord::InvalidForeignKey
end
# Enqueues an ActiveStorage::PurgeJob to call #purge. This is the recommended way to purge blobs from a transaction,
# an Active Record callback, or in any other real-time scenario.
def purge_later
ActiveStorage::PurgeJob.perform_later(self)
end
def sync_metadata # :nodoc:
service.update_metadata key, **service_metadata if service_metadata.any?
end
# Returns an instance of service, which can be configured globally or per attachment
def service
services.fetch(service_name) if service_name
end
private
# The extension is there for tools that cannot tell the type from the bytes (316d87e4: ImageMagick
# needs it for an SVG with no XML declaration), but some tools choose a decoder on it alone: FFmpeg
# runs a `.avs` file as an AviSynth script. So the uploaded extension is kept only when it denotes
# the blob's media type.
def tempfile_extension
extension = filename.extension_with_delimiter
return "" if extension.blank?
type_for_extension = Marcel::MimeType.for(name: filename.to_s)
return "" if type_for_extension == Marcel::MimeType::BINARY || type_for_extension != media_type
extension
end
# Marcel answers in canonical lowercase, so drop parameters and case before comparing.
def media_type
content_type.to_s.split(";").first.to_s.strip.downcase
end
# Opens a local io for reading, yielding a file-like object. If the io
# is already a File with a path, yield it directly. Otherwise, copy to
# a tempfile first since analyzers need file paths.
#
# Rack's multipart tempfile carries the uploaded filename's extension, so it is only yielded
# directly when that is the extension #tempfile_extension would give it.
def open_local_io(tmpdir:)
if local_io.respond_to?(:path) && local_io.path && File.exist?(local_io.path) &&
File.extname(local_io.path) == tempfile_extension
local_io.rewind if local_io.respond_to?(:rewind)
yield local_io
else
Tempfile.open([ "ActiveStorage-#{id}-", tempfile_extension ], tmpdir) do |file|
file.binmode
local_io.rewind if local_io.respond_to?(:rewind)
IO.copy_stream(local_io, file)
local_io.rewind if local_io.respond_to?(:rewind)
file.rewind
yield file
end
end
end
def extract_content_type(io)
Marcel::MimeType.for io, name: filename.to_s, declared_type: content_type
end
def web_image?
ActiveStorage.web_image_content_types.include?(content_type)
end
def touch_attachments
# The cascade exists to invalidate cache keys; it must not bump
# +lock_version+ on parents, since blob analysis does not modify any
# field of theirs and would otherwise race with concurrent edits.
ActiveRecord::Locking::Optimistic.preserve_lock_version_on_touch do
attachments.then do |relation|
if ActiveStorage.touch_attachment_records
relation.includes(:record)
else
relation
end
end.each do |attachment|
attachment.touch unless attachment.new_record?
end
end
end
def sync_metadata_later
ActiveStorage::SyncMetadataJob.perform_later(self)
end
end
ActiveSupport.run_load_hooks :active_storage_blob, ActiveStorage::Blob