class
ActiveStorage::Blob
- ActiveStorage::Blob
- Marten::DB::Model
- Reference
- Object
Overview
Rails has no Purgeable concern: #delete and #purge sit on ActiveStorage::Blob itself,
and the rows they remove go through associations. Neither can live in blob.cr here, because
the cascade has to be written out and variant_record.cr requires blob.cr, so naming
VariantRecord from there is a require cycle. blob/representable.cr reopens Blob for the
same reason.
Defined in:
active_storage/blob.cractive_storage/blob/purgeable.cr
active_storage/blob/representable.cr
active_storage/blob/servable.cr
Constant Summary
-
FIELDS_ =
{"id" => {type: "big_int", kwargs: {primary_key: true, auto: true}}, "key" => {type: "string", kwargs: {max_size: 255}}, "filename" => {type: "string", kwargs: {max_size: 255}}, "service_name" => {type: "string", kwargs: {max_size: 255}}, "content_type" => {type: "string", kwargs: {max_size: 255, null: true, blank: true}}, "metadata" => {type: "text", kwargs: {null: true, blank: true}}, "byte_size" => {type: "big_int", kwargs: ::NamedTuple.new}, "checksum" => {type: "string", kwargs: {max_size: 255, null: true, blank: true}}, "created_at" => {type: "date_time", kwargs: {auto_now_add: true, null: false}}} of Nil => Nil -
MINIMUM_TOKEN_LENGTH =
28 -
Length of a generated blob key, matching Rails
MINIMUM_TOKEN_LENGTH. https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/blob.rb -
TOKEN_ALPHABET =
"0123456789abcdefghijklmnopqrstuvwxyz" -
Alphabet of a generated blob key: Rails keys come from
SecureRandom.base36. -
VARIATIONS =
{"thumb" => {"format" => "jpg", "resize_to_fill" => [125, 125]}, "medium" => {"format" => "jpg", "resize_to_fill" => [500, 500]}, "landing" => {"format" => "jpg", "resize_to_fill" => [650, 430]}, "big_landing" => {"format" => "jpg", "resize_to_fill" => [1200, 800]}}
Constructors
-
.create_and_upload!(io : IO, filename : String, service_name : String, content_type : String | Nil = nil, key : String | Nil = nil, metadata : String | Nil = nil) : Blob
Creates a blob row for the content of the passed IO and uploads that content to the service.
-
.find_signed!(signed_id : String | Nil, purpose : String = "blob_id") : Blob
Returns the blob matching the passed signed ID.
Class Method Summary
-
.find_signed(signed_id : String | Nil, purpose : String = "blob_id") : Blob | Nil
Returns the blob matching the passed signed ID, or
nilwhen the signed ID is not valid. -
.generate_unique_secure_token(length : Int32 = MINIMUM_TOKEN_LENGTH) : String
Returns a random key of base36 characters, the way Rails generates a blob key.
- .host : String
- .host=(val : String)
- .services
- .services=(val : Hash(String, ActiveStorage::Service))
- .signed_id_verifier : ActiveSupport::MessageVerifier
Instance Method Summary
- #attachable_sgid
- #byte_size : Int32 | Int64 | Nil | Nil
- #byte_size!
- #byte_size=(byte_size : Int32 | Int64 | Nil | Nil)
- #byte_size?
- #checksum : String | Nil | Nil
- #checksum!
- #checksum=(checksum : String | Nil | Nil)
- #checksum?
- #content_type : String | Nil | Nil
- #content_type!
- #content_type=(content_type : String | Nil | Nil)
- #content_type?
-
#content_type_for_serving : String | Nil
Returns the content type this blob must be served with.
- #created_at : Time | Nil | Nil
- #created_at!
- #created_at=(created_at : Time | Nil | Nil)
- #created_at?
-
#delete_files : Nil
Removes the objects of this blob from its service, leaving its row.
-
#download : String
Returns the whole content of the file associated with this blob.
-
#download(& : Bytes -> ) : Nil
Yields the content of the file associated with this blob, one chunk at a time.
-
#download_chunk(range : Range(Int64, Int64)) : String
Returns the content of the file associated with this blob for the passed inclusive byte range.
- #filename : String | Nil | Nil
- #filename!
- #filename=(filename : String | Nil | Nil)
- #filename?
-
#forced_disposition_for_serving : String | Nil
Returns
"attachment"when this blob must not be rendered inline,nilotherwise. -
#global_id
TODO Support expires_at url param
- #id : Int32 | Int64 | Nil | Nil
- #id!
- #id=(id : Int32 | Int64 | Nil | Nil)
- #id?
- #image?
- #key : String | Nil | Nil
- #key!
- #key=(key : String | Nil | Nil)
- #key?
- #metadata : String | Nil | Nil
- #metadata!
- #metadata=(metadata : String | Nil | Nil)
- #metadata?
-
#proxy_url : String
Returns the URL of the proxy endpoint for this blob, the one Rails names
rails_storage_proxy_url. -
#purge : Bool
Removes this blob with everything derived from it: its variant records, the image blob of each of those, its own row, and the objects of all of them.
- #redirect_url
-
#representation_proxy_url(variation : String) : String
Returns the URL of the representations proxy endpoint, the one Rails names
rails_representation_proxy_url. - #representation_url(variation : String)
- #service : ActiveStorage::Service
- #service_name : String | Nil | Nil
- #service_name!
- #service_name=(service_name : String | Nil | Nil)
- #service_name?
- #signed_global_id(purpose : String = "blob_id")
- #signed_id(purpose : String = "blob_id")
- #signed_variation(variation : String, purpose : String = "variation")
-
#upload_without_unfurling(io : IO) : Nil
Uploads the content of the passed IO to this blob's service, under this blob's key.
-
#url(expires_in : Time::Span = ActiveStorage.service_urls_expire_in, disposition : String | Nil = "inline", filename : String | Nil = nil)
Returns a URL serving this blob, following the Rails serving rules.
-
#variable? : Bool
Returns whether a variant can be processed from this blob.
-
#variant(variation : Variation) : Variant | VariantWithRecord
Returns the variant of this blob for the passed variation.
Constructor Detail
Creates a blob row for the content of the passed IO and uploads that content to the service.
The row is inserted first, with the #byte_size and the #checksum read from the IO, and the
upload follows, the way Rails orders create_after_unfurling! and #upload_without_unfurling.
The row is deleted again when the upload fails, so a failure leaves nothing behind; a failure
between the two leaves the object in the service with no row pointing at it.
The IO is read twice, once to unfurl it and once to upload it, so it has to be rewindable,
which is what Rails requires of it too (compute_checksum_in_chunks). It also has to be at
its start: the checksum covers what is left to read, the upload sends what the rewind returns
to.
https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/blob.rb
Returns the blob matching the passed signed ID.
Raises ActiveSupport::MessageVerifier::InvalidSignature when the signed ID is tampered with,
carries another purpose or has expired, and Marten::DB::Errors::RecordNotFound when the
signed ID is valid but no matching blob exists.
https://github.com/rails/rails/blob/v8.1.3/activerecord/lib/active_record/signed_id.rb
Class Method Detail
Returns the blob matching the passed signed ID, or nil when the signed ID is not valid.
https://github.com/rails/rails/blob/v8.1.3/activerecord/lib/active_record/signed_id.rb
Returns a random key of base36 characters, the way Rails generates a blob key. https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/blob.rb
Instance Method Detail
Returns the content type this blob must be served with.
Content types that a browser would execute are served as a binary download instead. https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/blob/servable.rb
Removes the objects of this blob from its service, leaving its row.
Rails calls this #delete. That name is taken: Marten::Model#delete removes the row, which
is what Rails calls #destroy, and both .create_and_upload! and
ActiveStorage::VariantWithRecord#process call it for the row. Defining #delete here would
shadow it and change what blob.delete means in every file that requires this one.
The second call clears the derived-key layout, where the variants of a blob are stored under
variants/<key>/ instead of as blobs of their own.
https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/blob.rb
Returns the whole content of the file associated with this blob.
https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/blob.rb
Yields the content of the file associated with this blob, one chunk at a time.
https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/blob.rb
Returns the content of the file associated with this blob for the passed inclusive byte range.
https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/blob.rb
Returns "attachment" when this blob must not be rendered inline, nil otherwise.
https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/blob/servable.rb
Returns the URL of the proxy endpoint for this blob, the one Rails names
rails_storage_proxy_url.
The endpoint streams the content of the blob through the app, so the client never sees a
storage URL. MartenRailsBridge::ActiveStorage::Blobs::ProxyHandler is the handler serving it.
https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/blob.rb
Removes this blob with everything derived from it: its variant records, the image blob of each of those, its own row, and the objects of all of them.
Returns false and removes nothing when something is still attached to the blob. Rails raises
ActiveRecord::InvalidForeignKey from a before_destroy guard and #purge swallows it, so a
purge of an attached blob is silent there. The refusal is kept — active_storage_attachments
carries no foreign key in a Marten schema, so nothing underneath would stop the orphan that
guard exists to prevent — but it is reported instead of swallowed.
Rails cascades the rows through has_many :variant_records, dependent: :destroy and
has_one_attached :image. Marten relation fields default to on_delete: :do_nothing, so
every step is written out.
Synchronous, where Rails hands the whole thing to #purge_later, so a blob with many variants
makes one slow request.
https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/blob.rb
Returns the URL of the representations proxy endpoint, the one Rails names
rails_representation_proxy_url.
The bytes are streamed through the app instead of being redirected to, so the response can be cached by anything sitting in front of it. https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/blob.rb
Uploads the content of the passed IO to this blob's service, under this blob's key.
The row is left untouched: #byte_size and #checksum are expected to describe the content
already, which is why this is the "without unfurling" half of .create_and_upload!.
The headers stored next to the object are the ones the blob must be served with, following
the Rails service_metadata rules.
https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/blob.rb
Returns a URL serving this blob, following the Rails serving rules.
The content type and the disposition are the ones the blob must be served with, which is not
always the ones it was stored with: see #content_type_for_serving and
#forced_disposition_for_serving.
https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/blob.rb
Returns whether a variant can be processed from this blob.
Not #image?: ActiveStorage.variable_content_types is the narrower list.
https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/blob/representable.rb
Returns the variant of this blob for the passed variation.
Rails picks between the two variant classes up front, from
config.active_storage.track_variants, so it only ever looks in the layout it writes itself.
The bridge reads a database and a bucket a Rails app wrote, so it resolves the other way
round: a tracked record is used whenever one exists, then the derived key when its object is
already stored, and only a variation nothing has processed is left to
config.active_storage.track_variants, which decides which layout to write.
The middle step costs one Service#exist? call on a tracked miss. That is what lets one
deployment serve both layouts without being told which one the Rails app runs.
Nothing is processed here: the returned variant does that in its own #processed, as in Rails.
https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/blob/representable.rb