Class: Paperclip::ContentTypeDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/paperclip/content_type_detector.rb

Constant Summary collapse

EMPTY_TYPE =

The content-type detection strategy is as follows:

  1. Blank/Empty files: If there’s no filename or the file is empty, provide a sensible default (application/octet-stream or inode/x-empty)

  2. Calculated match: Return the first result that is found by both the ‘file` command and MIME::Types.

  3. Standard types: Return the first standard (without an x- prefix) entry in MIME::Types

  4. Experimental types: If there were no standard types in MIME::Types list, try to return the first experimental one

  5. Raw ‘file` command: Just use the output of the `file` command raw, or a sensible default. This is cached from Step 2.

"inode/x-empty"
SENSIBLE_DEFAULT =
"application/octet-stream"

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ ContentTypeDetector

Returns a new instance of ContentTypeDetector.



23
24
25
# File 'lib/paperclip/content_type_detector.rb', line 23

def initialize(filename)
  @filename = filename
end

Instance Method Details

#detectObject

Returns a String describing the file’s content type



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/paperclip/content_type_detector.rb', line 28

def detect
  if blank_name?
    SENSIBLE_DEFAULT
  elsif empty_file?
    EMPTY_TYPE
  elsif calculated_type_matches.any?
    calculated_type_matches.first
  else
    type_from_file_command || SENSIBLE_DEFAULT
  end.to_s
end