module Shrine::Plugins::DetermineMimeType

  1. lib/shrine/plugins/determine_mime_type.rb

The determine_mime_type plugin stores the actual MIME type of the uploaded file.

plugin :determine_mime_type

By default the UNIX file utility is used to determine the MIME type, but you can change it:

plugin :determine_mime_type, analyzer: :filemagic

The plugin accepts the following analyzers:


(Default). Uses the file utility to determine the MIME type from file contents. It is installed by default on most operating systems, but the Windows equivalent needs to be installed separately.


Uses the ruby-filemagic gem to determine the MIME type from file contents, using a similar MIME database as the file utility. Unlike the file utility, ruby-filemagic works on Windows without any setup.


Uses the mimemagic gem to determine the MIME type from file contents. Unlike ruby-filemagic, mimemagic is a pure-ruby solution, so it will work across all Ruby implementations.


Uses the mime-types gem to determine the MIME type from the file extension. Note that unlike other solutions, this analyzer is not guaranteed to return the actual MIME type of the file.


Uses the default way of extracting the MIME type, and that is from the “Content-Type” request header, which might not hold the actual MIME type of the file.

Not all analyzers can recognize all types of files. For those cases you can build your own analyzer, where you can reuse built-in analyzers:

plugin :determine_mime_type, analyzer: ->(io, analyzers) do
  analyzers[:mimemagic].call(io) || analyzers[:file].call(io)


Public Class

  1. configure


MAGIC_NUMBER = 256 * 1024  

How many bytes we need to read in order to determine the MIME type.

Public Class methods

configure (uploader, opts = {})
[show source]
# File lib/shrine/plugins/determine_mime_type.rb, line 53
def self.configure(uploader, opts = {})
  uploader.opts[:mime_type_analyzer] = opts.fetch(:analyzer, uploader.opts.fetch(:mime_type_analyzer, :file))
  uploader.opts[:mime_type_magic_header] = opts.fetch(:magic_header, uploader.opts.fetch(:mime_type_magic_header, MAGIC_NUMBER))