class ActiveStorage::Variation

Overview

A set of transformations that identifies a variant of a blob.

The bridge only reads variations: .decode turns the signed key carried by a representation URL back into its transformations, and #key hands that signed key to ActiveStorage::Variant#key, which digests it to derive the storage key the variant was written to. Running the transformations is out of scope: the Rails app writes the variants, the bridge serves them. https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/variation.rb

Defined in:

active_storage/variation.cr

Constant Summary

DEFAULT_FORMAT = "png"

Format used when the transformations do not carry one.

PURPOSE = "variation"

Purpose a signed variation key is generated and verified with.

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.decode(key : String, verifier : ActiveSupport::MessageVerifier) : Variation #

Returns the variation encoded in the passed signed key.

Raises ActiveSupport::MessageVerifier::InvalidSignature when the key is tampered with, carries another purpose or has expired. https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/variation.rb


def self.new(transformations : JSON::Any, verifier : ActiveSupport::MessageVerifier, signed_key : String | Nil = nil) #

Class Method Detail

def self.encode(transformations, verifier : ActiveSupport::MessageVerifier) : String #

Returns a signed key for the passed transformations.

https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/variation.rb


Instance Method Detail

def content_type : String #

Returns the content type a variant carrying this variation is served with.

https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/variation.rb


def digest : String #

Returns the fingerprint of these transformations, as stored in the variation_digest column of active_storage_variant_records.

This is not #key: Rails hashes a Ruby Marshal dump of the transformations rather than the signed key, so reproducing it requires reproducing that dump byte for byte. See Ruby::Marshal. Rails symbolizes the keys before dumping, which dump_symbolized does too, so a key that arrived as JSON still digests to the same value. https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/variation.rb


def digest_candidates : Array(String) #

Returns the fingerprints a variant record for these transformations may be stored under, the serve-path one first.

Rails digests Marshal.dump with no normalization, so a value the app wrote as a Symbol (format: :jpg) digests differently from the same value after it travelled through a signed URL key, where JSON hands it over as a String. #digest is the one Rails' own controllers compute after decoding a URL key — the record it finds decides which image Rails serves, so the bridge prefers it too. The second candidate symbolizes every string value, which reaches the records the app's own passes (preprocessed: true jobs, variant.processed in code) wrote from the spec as written. Transformations without string values offer a single candidate.

Symbolizing is all or nothing, so only those two forms are covered. A spec that mixes them — background: "white" next to format: :jpg in a resize_and_pad, or a string inside saver — dumps to neither candidate, so its record is not found and Blob#variant falls back to the derived key, which a tracked app never wrote. Covering the mixed forms would take one candidate per subset of the string values, so they are left out. https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/variation.rb


def format : String #

Returns the format the variant is encoded in, without a leading dot.

Rails falls back to png and rejects a format it knows no MIME type for. Note that Rails resolves the fallback from the blob before signing the key (Blob#variant defaults the variation to the blob format), so a key issued by Rails always carries its own format. https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/variation.rb


def key : String #

Returns the signed key of this variation.

Rails re-encodes the transformations here, which is lossless because its encoder round-trips. A variation decoded from a URL returns the key it came from instead, so the digest that ActiveStorage::Variant#key takes stays byte-identical to the one the signing app computed even when its Base64 alphabet differs from ours. https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/variation.rb


def process(source : Bytes) : Bytes #

Returns the passed source image with these transformations applied, encoded in #format.

#format names the saver rather than an operation, so Rails hands the transformer transformations.except(:format) and the format on its own. Which processor runs is ActiveStorage.variant_processor. https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/variation.rb


def transformations : JSON::Any #