title: Shrine 2.15.0

New features

Rather than encoding processing steps in the URL (like Dragonfly and Active Storage do), with derivation_endpoint you generate URLs to named “derivation” blocks, passing any arguments you need for the processing.

rb photo.image.derivation_url(:thumbnail, 600, 400) #=> "derivations/image/thumbnail/600/400/eyJpZCI6ImZvbyIsInN0b3JhZ2UiOiJzdG9yZSJ9?signature=..."

When the URL is requested, it’s routed to a Rack app that you mount in your router, which knows how to resolve the HTTP request, call the appropriate processing, and generate an HTTP response.

rb # config/routes.rb (Rails) Rails.application.routes.draw do mount ImageUploader.derivation_endpoint => "derivations/images" end

In this case the endpoint will call the :thumbnail derivation block defined in our uploader, passing it the source file and derivation arguments (in this case thumbnail dimensions). We can then generate the thumbnail inside the block:

“‘rb require “image_processing/mini_magick”

class ImageUploader < Shrine derivation :thumbnail do |file, width, height| ImageProcessing::MiniMagick .source(file) .resize_to_limit!(width.to_i, height.to_i) end end “‘

The derivation URLs are signed with the secret key provided when loading the plugin, to prevent tampering and potential DoS attacks.

rb plugin :derivation_endpoint, secret_key: "<YOUR SECRET KEY>"

The derivation_endpoint plugin is highly configurable, you can set a CDN host, change response headers of derivatives (Content-Type, Content-Disposition), add URL expiration, cache generated derivatives to a Shrine storage and more. Check out the documentation for more details.

Other improvements

Backwards compatibility