β€”

title: Shrine 2.19.0

New features

β€œβ€˜rb Shrine.plugin :instrumentation

uploaded_file = Shrine.upload(io, :store) uploaded_file.exists? uploaded_file.download uploaded_file.delete Metadata (32ms) – {:storage=>:store, :io=>StringIO, :uploader=>Shrine} Upload (1523ms) – {:storage=>:store, :location=>β€œed0e30ddec8b97813f2c1f4cfd1700b4”, :io=>StringIO, :upload_options=>{}, :uploader=>Shrine} Exists (755ms) – {:storage=>:store, :location=>β€œed0e30ddec8b97813f2c1f4cfd1700b4”, :uploader=>Shrine} Download (1002ms) – {:storage=>:store, :location=>β€œed0e30ddec8b97813f2c1f4cfd1700b4”, :download_options=>{}, :uploader=>Shrine} Delete (700ms) – {:storage=>:store, :location=>β€œed0e30ddec8b97813f2c1f4cfd1700b4”, :uploader=>Shrine} β€œβ€˜

It supports ActiveSupport::Notifications (default) and dry-monitor notification backends out of the box.

β€œβ€˜rb require β€œdry-monitor”

Shrine.plugin :instrumentation, notifications: Dry::Monitor::Notifications.new(:test) β€œβ€˜

rb uploaded_file = uploader.upload(io, metadata: false) # skips metadata extraction uploaded_file.metadata #=> {}

rb uploaded_file = uploader.upload(another_uploaded_file, metadata: true) # forces metadata extraction uploaded_file.metadata #=> re-extracted metadata

β€œβ€˜rb plugin :pretty_location, identifier: β€œuuid” # β€œuser/aa357797-5845-451b-8662-08eecdc9f762/profile_picture/493g82jf23.jpg”

plugin :pretty_location, identifier: :email # β€œuser/foo@bar.com/profile_picture/493g82jf23.jpg” β€œβ€˜

rb plugin :store_dimensions, on_error: :warn # prints a warning message (default) plugin :store_dimensions, on_error: :fail # raises the exception plugin :store_dimensions, on_error: :ignore # ignores the exception plugin :store_dimensions, on_error: -> (error) { # custom handler # report the exception to your exception handler }

rb uploader.upload(file, move: true) File.exist?(file.path) #=> false

rb file_system.clear! { |path| path.mtime < Time.now - 7*24*60*60 } # delete files older than 1 week

Other improvements

| Plugin | Instrumentation | | :—– | :————– | | derivation_endpoint | instruments file processing | | determine_mime_type | instruments analyzing MIME type | | store_dimensions | instruments extracting image dimensions | | signature | instruments calculating signature | | infer_extension | instruments inferring extension | | remote_url | instruments remote URL downloading | | data_uri | instruments data URI parsing |

rb # log messages into the Rails logger Shrine.logger = Rails.logger

rb def generate_location(io, record: nil, **context) identifier = record.email if record.is_a?(User) pretty_location(io, record: record, identifier: identifier, **context) end

rb uploaded_file.metadata["duration"] # can now be just uploaded_file["duration"]

rb Shrine.determine_mime_type(io) # can now be just Shrine.mime_type(io) rb Shrine.extract_dimensions(io) # can now be just Shrine.dimensions(io) rb Shrine.calculate_signature(io) # can now be just Shrine.signature(io)

β€œβ€˜rb validate_min_width 10 validate_min_height 10 validate_max_width 5000 validate_max_height 5000

# can now be written as

validate_min_dimensions [10, 10] validate_max_dimensions [5000, 5000]

# which can be additionally shortened to

validate_dimensions [10..5000, 10..5000] β€œβ€˜

β€œβ€˜rb validate_min_size 1024 validate_max_size 10_1024_1024

# can now be shortned to

validate_size 1024..10_1024_1024 rb validate_min_width 10 validate_max_width 5000

# can now be shortened to

validate_width 10..5000 rb validate_min_height 10 validate_max_height 5000

# can now be shortened to

validate_height 10..5000 β€œβ€˜

rb validate_mime_type_inclusion %w[image/jpeg image/png image/webp] # can now be just validate_mime_type %w[image/jpeg image/png image/webp] rb validate_extension_inclusion %w[jpeg png webp] # can now be just validate_extension %w[jpeg png webp]

Backwards compatibility

rb file_system.clear!(older_than: Time.now - 7*24*60*60) # should now be replaced with file_system.clear! { |path| path.mtime < Time.now - 7*24*60*60 }