Skip to content

Instantly share code, notes, and snippets.

@Pilooz
Created July 1, 2013 12:13
Show Gist options
  • Save Pilooz/5900307 to your computer and use it in GitHub Desktop.
Save Pilooz/5900307 to your computer and use it in GitHub Desktop.
guess mime type from filename extension
# Internal: Lookup mime type for extension.
#
# Returns a MIME::Type
def _mime_type
if defined? @_mime_type
@_mime_type
else
guesses = ::MIME::Types.type_for(extname.to_s)
# Prefer text mime types over binary
@_mime_type = guesses.detect { |type| type.ascii? } ||
# Otherwise use the first guess
guesses.first
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment