module Shrine::Plugins::DetermineMimeType

  1. lib/shrine/plugins/determine_mime_type.rb

The determine_mime_type plugin allows you to determine and store the actual MIME type of the file analyzed from file content.

plugin :determine_mime_type

By default the UNIX file utility is used to determine the MIME type, and the result is automatically written to the mime_type metadata field. You can choose a different built-in MIME type analyzer:

plugin :determine_mime_type, analyzer: :filemagic

The following analyzers are accepted:


(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 reading the #content_type attribute of the IO object, which might not hold the actual MIME type of the file.

A single analyzer is not going to properly recognize all types of files, so you can build your own custom analyzer for your requirements, where you can combine the built-in analyzers. For example, if you want to correctly determine MIME type of .css, .js, .json, .csv, .xml, or similar text-based files, you can combine file and mime_types analyzers:

plugin :determine_mime_type, analyzer: ->(io, analyzers) do
  mime_type = analyzers[:file].call(io)
  mime_type = analyzers[:mime_types].call(io) if mime_type == "text/plain"

You can also use methods for determining the MIME type directly:

# or YourUploader.determine_mime_type(io)
Shrine.determine_mime_type(io) # calls the defined analyzer
#=> "image/jpeg"

# or YourUploader.mime_type_analyzers
Shrine.mime_type_analyzers[:file].call(io) # calls a built-in analyzer
#=> "image/jpeg"


Public Class

  1. configure

Public Class methods

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