—
title: Shrine
2.7.0¶ ↑
New Plugins¶ ↑
-
The
direct_upload
plugin has been split intoupload_endpoint
andpresign_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 fromdownload_endpoint
. It gives the ability to convert aShrine::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] response.headers.merge!(file_response[1]) self.response_body = file_response[2] end end
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 toShrine::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) end
-
The
activerecord
plugin now allows validation error messages to be an array, which is passed as arguments torecord.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 newaws-sdk-s3
gem. See the AWS announcement for more details on the modularization of theaws-sdk
gem.
gem "aws-sdk-s3", "~> 1.2"
-
Shrine::Storage::S3#open
now uses streaming with theaws-sdk-s3
gem instead ofDown
. Previously this discrepancy would causeaws-sdk-s3
options like:proxy
to not have any effect inShrine::Storage::S3#open
. -
Shrine::Storage::S3
now raises anArgumentError
when:bucket
option isnil
. -
A
Shrine
uploader can now call file validations from its superclass.
class ImageUploader < Shrine Attacher.validate do # ... end end
class ProfileImageUploader < ImageUploader Attacher.validate do super() # empty parentheses are required # ... end end
-
The
download_endpoint
now encodes uploaded file metadata in the URL, allowing it to use them when generating a file response. Now thefilename
metadata will be used forContent-Disposition
if available. Also, themime_type
metadata will be used forContent-Type
instead ofRack::Mime
when available. -
The
download_endpoint
now returns a404 Not Found
when uploaded file is missing. -
The
download_endpoint
now returnsCache-Control: max-age=31536000
header which tells clients to cache the response for 1 year. -
The
download_endpoint
now usesRack::BodyProxy
instead of Roda’sstreaming
plugin, which means thedownload_endpoint
plugin loads less code in total. -
Fixed
determine_mime_type
plugin raising an exception when empty files are given tofile
orfilemagic
analyzers. -
Un-deprecated
Shrine.uploaded_file
accepting file data as JSON string. -
The
Shrine::UploadedFile#base64
andShrine::UploadedFile#data_uri
methods from thedata_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
andpresign_endpoint
plugins, thedirect_upload
plugin should now be considered deprecated. It will be officially deprecated inShrine
3, and removed inShrine
4. -
The
Shrine::DownloadEndpoint
constant has been deprecated over theShrine.download_endpoint
method, and it will be removed inShrine
3. InShrine
3 the returned app won’t be aRoda
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 usingRoda.use
to add middlewares, you can instead wrap the endpoint in the middlewares directly:
map "/attachments" do use MyMiddleware run Shrine.download_endpoint end
-
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 authenticatedownload_endpoint
requests, you should probably use the newrack_response
plugin instead ofdownload_endpoint
.