Class: Paperclip::ContentTypeDetector
- Inherits:
-
Object
- Object
- Paperclip::ContentTypeDetector
- Defined in:
- lib/paperclip/content_type_detector.rb
Constant Summary collapse
- EMPTY_TYPE =
The content-type detection strategy is as follows:
-
Blank/Empty files: If there’s no filepath or the file is empty, provide a sensible default (application/octet-stream or inode/x-empty)
-
Calculated match: Return the first result that is found by both the ‘file` command and MIME::Types.
-
Standard types: Return the first standard (without an x- prefix) entry in MIME::Types
-
Experimental types: If there were no standard types in MIME::Types list, try to return the first experimental one
-
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
-
#detect ⇒ Object
Returns a String describing the file’s content type.
-
#initialize(filepath) ⇒ ContentTypeDetector
constructor
A new instance of ContentTypeDetector.
Constructor Details
#initialize(filepath) ⇒ ContentTypeDetector
Returns a new instance of ContentTypeDetector.
23 24 25 |
# File 'lib/paperclip/content_type_detector.rb', line 23 def initialize(filepath) @filepath = filepath end |
Instance Method Details
#detect ⇒ Object
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_contents || SENSIBLE_DEFAULT end.to_s end |