Shrine 2.7.0
New Plugins
- The
direct_uploadplugin has been split intoupload_endpointandpresign_endpointplugins. 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` endpointShrine.plugin :presign_endpoint
# config.ru
map "/presign" do
run Shrine.presign_endpoint(:cache)
end # creates a `GET /presign` endpoint- The
rack_responseplugin has been extracted fromdownload_endpoint. It gives the ability to convert aShrine::UploadedFileobject into a Rack response triple.
Shrine.plugin :rack_responseclass 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
endNew Features
- The
determine_mime_typeplugin 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_mimeShrine::Attachment.newnow 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
activerecordplugin 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::S3now supports the newaws-sdk-s3gem. See the AWS announcement for more details on the modularization of theaws-sdkgem.
gem "aws-sdk-s3", "~> 1.2"Shrine::Storage::S3#opennow uses streaming with theaws-sdk-s3gem instead ofDown. Previously this discrepancy would causeaws-sdk-s3options like:proxyto not have any effect inShrine::Storage::S3#open.Shrine::Storage::S3now raises anArgumentErrorwhen:bucketoption isnil.A Shrine uploader can now call file validations from its superclass.
class ImageUploader < Shrine
Attacher.validate do
# ...
end
endclass ProfileImageUploader < ImageUploader
Attacher.validate do
super() # empty parentheses are required
# ...
end
endThe
download_endpointnow encodes uploaded file metadata in the URL, allowing it to use them when generating a file response. Now thefilenamemetadata will be used forContent-Dispositionif available. Also, themime_typemetadata will be used forContent-Typeinstead ofRack::Mimewhen available.The
download_endpointnow returns a404 Not Foundwhen uploaded file is missing.The
download_endpointnow returnsCache-Control: max-age=31536000header which tells clients to cache the response for 1 year.The
download_endpointnow usesRack::BodyProxyinstead of Roda'sstreamingplugin, which means thedownload_endpointplugin loads less code in total.Fixed
determine_mime_typeplugin raising an exception when empty files are given tofileorfilemagicanalyzers.Un-deprecated
Shrine.uploaded_fileaccepting file data as JSON string.The
Shrine::UploadedFile#base64andShrine::UploadedFile#data_urimethods from thedata_uriplugin don't wrap the base64-encoded content to 60 columns anymore.The
signatureplugin doesn't wrap base64-formatted signatures to 60 columns anymore.The
signatureplugin doesn't add a newline at the end of the base64-formatted signature anymore.The
data_uriplugin 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_classclass variable.Fixed
direct_uploadplugin printing a deprecation warning when generating fake presigns when query parameters are passed in.Update Down dependency to the latest
4.xversion.
Backwards compatibility
With the release of
upload_endpointandpresign_endpointplugins, thedirect_uploadplugin should now be considered deprecated. It will be officially deprecated in Shrine 3, and removed in Shrine 4.The
Shrine::DownloadEndpointconstant has been deprecated over theShrine.download_endpointmethod, and it will be removed in Shrine 3. In Shrine 3 the returned app won't be aRodasubclass 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.useto add middlewares, you can instead wrap the endpoint in the middlewares directly:
map "/attachments" do
use MyMiddleware
run Shrine.download_endpoint
end- The
download_endpointdoesn'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_endpointrequests, you should probably use the newrack_responseplugin instead ofdownload_endpoint.