Last Update: 2023-07-30 18:26:06 +0200

title: Shrine 2.7.0

New Plugins

  • The direct_upload plugin has been split into upload_endpoint and presign_endpoint plugins. These plugins provide respective endpoints that accept requests to the root URL, allowing users to fully choose the URL. They also drop the Roda dependency.

Shrine.plugin :upload_endpoint

# config.ru
map "/images/upload" do
  run ImageUploader.upload_endpoint(:cache)
end # creates a `POST /images/upload` endpoint
Shrine.plugin :presign_endpoint

# config.ru
map "/presign" do
  run Shrine.presign_endpoint(:cache)
end # creates a `GET /presign` endpoint
  • The rack_response plugin has been extracted from download_endpoint. It gives the ability to convert a Shrine::UploadedFile object into a Rack response triple.

Shrine.plugin :rack_response
class FilesController < ActionController::Base
  def download
    # ...
    file_response = record.attachment.to_rack_response # returns a Rack response triple

    response.status = file_response[0]
    self.response_body = file_response[2]

New Features

  • The determine_mime_type plugin now supports the mini_mime gem for determining the MIME type from file extension. The mini_mime gem is aimed to be a lightweight replacement for the mime-types gem.

Shrine.plugin :determine_mime_type, analyzer: :mini_mime
  • Shrine::Attachment.new now accepts additional options that are forwarded to Shrine::Attacher.new. This allows for overriding temporary and permanent storages per attachment declaration.

class Photo
  include ImageUploader::Attachment.new(:image, cache: :other_cache, store: :other_store)
  • The activerecord plugin now allows validation error messages to be an array, which is passed as arguments to record.errors.add. This allows users to delegate validation error message generation and i18n to ActiveRecord.

validate_max_size 256 * 1024**2, message: -> (max) { [:too_large, max: max] }

Other Improvements

  • Shrine::Storage::S3 now supports the new aws-sdk-s3 gem. See the AWS announcement for more details on the modularization of the aws-sdk gem.

gem "aws-sdk-s3", "~> 1.2"
  • Shrine::Storage::S3#open now uses streaming with the aws-sdk-s3 gem instead of Down. Previously this discrepancy would cause aws-sdk-s3 options like :proxy to not have any effect in Shrine::Storage::S3#open.

  • Shrine::Storage::S3 now raises an ArgumentError when :bucket option is nil.

  • A Shrine uploader can now call file validations from its superclass.

class ImageUploader < Shrine
  Attacher.validate do
    # ...
class ProfileImageUploader < ImageUploader
  Attacher.validate do
    super() # empty parentheses are required
    # ...
  • The download_endpoint now encodes uploaded file metadata in the URL, allowing it to use them when generating a file response. Now the filename metadata will be used for Content-Disposition if available. Also, the mime_type metadata will be used for Content-Type instead of Rack::Mime when available.

  • The download_endpoint now returns a 404 Not Found when uploaded file is missing.

  • The download_endpoint now returns Cache-Control: max-age=31536000 header which tells clients to cache the response for 1 year.

  • The download_endpoint now uses Rack::BodyProxy instead of Roda’s streaming plugin, which means the download_endpoint plugin loads less code in total.

  • Fixed determine_mime_type plugin raising an exception when empty files are given to file or filemagic analyzers.

  • Un-deprecated Shrine.uploaded_file accepting file data as JSON string.

  • The Shrine::UploadedFile#base64 and Shrine::UploadedFile#data_uri methods from the data_uri plugin don’t wrap the base64-encoded content to 60 columns anymore.

  • The signature plugin doesn’t wrap base64-formatted signatures to 60 columns anymore.

  • The signature plugin doesn’t add a newline at the end of the base64-formatted signature anymore.

  • The data_uri plugin doesn’t raise an exception on Ruby 2.4.1 on raw data URIs anymore.

  • Reduce model pollution by eliminating the need for an internal @@<attachment>_attacher_class class variable.

  • Fixed direct_upload plugin printing a deprecation warning when generating fake presigns when query parameters are passed in.

  • Update Down dependency to the latest 4.x version.

Backwards compatibility

  • With the release of upload_endpoint and presign_endpoint plugins, the direct_upload plugin should now be considered deprecated. It will be officially deprecated in Shrine 3, and removed in Shrine 4.

  • The Shrine::DownloadEndpoint constant has been deprecated over the Shrine.download_endpoint method, and it will be removed in Shrine 3. In Shrine 3 the returned app won’t be a Roda subclass anymore, instead it will be a pure Rack class. If you are relying on this, you should update your code. For example, if you were using Roda.use to add middlewares, you can instead wrap the endpoint in the middlewares directly:

map "/attachments" do
  use MyMiddleware
  run Shrine.download_endpoint
  • The download_endpoint doesn’t generate URLs which end with uploaded file ID anymore; instead it generates URLs with base64-encoded uploaded file data. The old URLs will remain supported indefinitely. If you were relying on URLs including uploaded file ID, you’ll need to update your code. If this was becase you wanted to authenticate download_endpoint requests, you should probably use the new rack_response plugin instead of download_endpoint.