—
title: Shrine 2.6.0¶ ↑
New plugins¶ ↑
- 
Added
signatureplugin which allows you to calculate a SHA{1,256,384,512}/MD5/CRC32 hash of a file, in raw, hex or base64 encoding. 
Shrine.plugin :signature Shrine.calculate_signature(io, :md5) #=> "8c7dd922ad47494fc02c388e12c00eac" Shrine.calculate_signature(io, :md5, format: :base64) #=> "jH3ZIq1HSU/ALDiOEsAOrA==\n"
You can then easily create a new metadata field with the calculated hash:
Shrine.plugin :signature Shrine.plugin :add_metadata MyUploader.add_metadata :md5 do |io, context| calculate_signature(io, :md5) end
- 
Added
metadata_attibutesplugin which allows you to sync attachment metadata to additional record attributes. 
Shrine.plugin :metadata_attributes # Syncs `size` metadata to `#<attachment>_size` record attribute, and # `mime_type` metadata to `#<attachment>_type` record attribute. Shrine::Attacher.metadata_attributes :size => :size, :mime_type => :type # ... user.avatar = image user.avatar.metadata["size"] #=> 95724 user.avatar_size #=> 95724 user.avatar.metadata["mime_type"] #=> "image/jpeg" user.avatar_type #=> "image/jpeg" user.avatar = nil user.avatar_size #=> nil user.avatar_type #=> nil
- 
Added
refresh_metadataplugin (extracted from therestore_cached_dataplugin) which allows you to re-extract metadata from an uploaded file. 
Shrine.plugin :refresh_metadata # ... uploaded_file.refresh_metadata! uploaded_file.metadata # re-extracted metadata
New features¶ ↑
- 
Added
Shrine.determine_mime_typeandShrine.mime_type_analyzersmethods todetermine_mime_typeplugin for extracting MIME type from a file. 
Shrine.plugin :determine_mime_type Shrine.determine_mime_type(io) # calls the defined analyzer #=> "image/jpeg" Shrine.mime_type_analyzers[:file].call(io) # calls a built-in analyzer #=> "image/jpeg"
- 
Added
Shrine.extract_dimensionsandShrine.dimensions_analyzersmethods tostore_dimensionsplugin for extracting dimensions from a file. 
Shrine.plugin :store_dimensions Shrine.extract_dimensions(io) # calls the defined analyzer #=> [300, 400] Shrine.dimensions_analyzers[:fastimage].call(io) # calls a built-in analyzer #=> [300, 400]
- 
Added
Shrine.data_urimethod todata_uriplugin for converting a data URI into an IO object. 
Shrine.plugin :data_uri io = Shrine.data_uri("data:image/png;base64,9kfg07a80ihtiort87t85") io.content_type #=> "image/png" io.size #=> 482
- 
Added
Shrine.rack_filemethod torack_fileplugin for converting the Rack uploaded file hash into an IO object. 
Shrine.plugin :rack_file file_hash #=> # { # :name => "file", # :filename => "cats.png", # :type => "image/png", # :tempfile => #<Tempfile:/var/folders/3n/3asd/-Tmp-/RackMultipart201-1476-nfw2-0>, # :head => "Content-Disposition: form-data; ...", # } io = Shrine.rack_file(file_hash) io.original_filename #=> "cats.png" io.content_type #=> "image/png" io.size #=> 58342
Performance improvements¶ ↑
- 
Improved performance of parsing data URIs up to 10x by switching from regex matching to
StringScanner, and the intermediary base64-encoded content string is now deallocated after it's decoded. - 
UploadedFile#base64now deallocates the intermediary binary content string after converting it to base64 format indata_uriplugin. - 
The default multipart copy threshold for
Shrine::Storage::S3has been bumped from 15MB to 100MB, which is about the size where parallelized multipart copy starts being faster. - 
Uploading files from
FileSystemstorage toS3storage will now utilize aws-sdk's parallelized multipart upload on files with 15MB or above, which is the scenario when your temporary storage isFileSystemand permanent storage isS3. - 
Shrine::Storage::S3#downloadnow uses only one file descriptor. - 
The
loggingplugin doesn't require thebenchmarkstandard library anymore, which reduces the memory footprint. 
Other improvements¶ ↑
- 
Fixed wrong implementation of
FileSystem#clear!(older_than: expiration_date), where in common scenarios it wouldn't delete old files, or it would incorrectly delete files which still didn't pass the expiration date. - 
The
delete_rawplugin now deletes any uploaded IO objects that respond to#path, not justTempfileobjects (this includesFile,ActionDispatch::Http::UploadedFile, andShrine::Plugins::RackFile::UploadedFileobjects). - 
The
rack_fileplugin now works with Grape. - 
The
data_uriplugin now accepts URI-encoded raw data URIs. - 
The
data_uriplugin now accepts data URIs with additional media type params. - 
Shrine::Storage::S3now accepts separate multipart thresholds for uploading and for copying. - 
Shrine::Storage::S3#objectis now public, which returns anAws::S3::Objectrepresenting the file. 
s3.object("image.jpg") #=> #<Aws::S3::Object bucket_name="your-bucket" key="image.jpg">
- 
Added
Shrine::Storage::S3#clientmethod for quicker access to theAws::S3::Clientinstance. - 
The "S3" component of the
aws-sdkgem is now eagerly loaded inlib/shrine/storage/s3.rb, to remove possibility of autoloading causing errors in multi-threaded systems. - 
Shrine::Storage::FileSystem#pathis now public, which returns aPathnameto the file. 
filesystem.path("image.jpg") #=> #<Pathname:public/image.jpg>
- 
Shrinenow checks whetherShrine#generate_locationhappened to return nil, and raisesShrine::Error. - 
Attacher#finalizecan now be called even when the attachment hasn’t changed. - 
Added
Attacher#changed?alias toAttacher#attached?. - 
The logic for rejecting attachments that are already the current attachment has been moved from
Attacher#assigntoAttacher#set. - 
The
remove_invalidplugin will now only remove new invalid attachments. - 
The
tempfilestandard library is now correctly required inlib/shrine.rb. - 
Improved default validation error messages in
validation_helpersplugin. - 
Blacklisted MIME types and extensions are now excluded from the default error message for security reasons.
 - 
The
add_metadataplugin now also accepts hashes with symbol keys. - 
The loaded plugin module is now returned when calling
Shrine.plugin. - 
Improved visibility of
Shrinedeprecation warnings. 
Backwards compatibility¶ ↑
- 
Passing regexes to
validate_(mime_type|extension)_(inclusion|exclusion)has been deprecated due to lack of usefulness and for security reasons, from now on you should always pass plain strings. 
# deprecated validate_mime_type_inclusion [/image\//] validate_extension_inclusion [/jpe?g/] # use strings instead validate_mime_type_inclusion %w[image/jpeg image/png image/gif ...] validate_extension_inclusion %w[jpg jpeg png gif ...]
- 
Previously the
validate_(max|min)_(width|height)validations would ignorewidthorheightbeing nil, but this is now deprecated and inShrine3 this will return an error. You should make sure that the file is of the right type before validating dimensions. 
if validate_mime_type_inclusion %w[image/jpeg image/png image/gif] validate_max_width 1000 validate_max_height 1000 end
- 
Passing a Rack uploaded file hash to
Shrine#uploadandShrine#storeis now deprecated inrack_fileplugin, useShrine.rack_fileto first convert the Rack hash into an IO object. - 
Deprecated passing
:multipart_thresholdoption as an integer inShrine::Storage::S3, use a hash with:uploadand:copykeys. - 
Deprecated
Shrine::Storage::S3#s3in favour ofShrine::Storage::S3#client. - 
The
Shrine::Plugins::DataUri::DataFileisn't a subclass ofStringIOanymore, instead it uses composition to delegate only a subset of IO methods to the underlyingStringIOobject. This shouldn't cause any backwards compatibility unless the user was relying on it to be a subclass ofStringIO.