Module: Linguist::Mime

Defined in:
lib/linguist/mime.rb

Class Method Summary collapse

Class Method Details

.lookup_mime_type_for(ext_or_mime_type) ⇒ Object

Internal: Lookup mime type for extension or mime type

ext_or_mime_type - A file extension “.txt” or mime type “text/plain”.

Returns a MIME::Type



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/linguist/mime.rb', line 72

def self.lookup_mime_type_for(ext_or_mime_type)
  ext_or_mime_type ||= ''

  if ext_or_mime_type =~ /\w+\/\w+/
    guesses = ::MIME::Types[ext_or_mime_type]
  else
    guesses = ::MIME::Types.type_for(ext_or_mime_type)
  end

  # Use custom override first
  guesses.detect { |type| type.override } ||

    # Prefer text mime types over binary
    guesses.detect { |type| type.ascii? } ||

    # Otherwise use the first guess
    guesses.first
end

.mime_for(ext) ⇒ Object

Internal: Look up mime type for extension.

ext - The extension String. May include leading “.”

Examples

Mime.mime_for('.html')
# => 'text/html'

Mime.mime_for('txt')
# => 'text/plain'

Return mime type String otherwise falls back to ‘text/plain’.



62
63
64
65
# File 'lib/linguist/mime.rb', line 62

def self.mime_for(ext)
  mime_type = lookup_mime_type_for(ext)
  mime_type ? mime_type.to_s : 'text/plain'
end