Ensure our custom mime types override mime-types lib

This commit is contained in:
Joshua Peek
2012-03-28 10:22:32 -05:00
parent 801200a203
commit 4c0a8b1ccf
5 changed files with 35 additions and 23 deletions

View File

@@ -53,7 +53,7 @@ module Linguist
#
# Returns a content type String.
def content_type
@content_type ||= binary? ? mime_type :
@content_type ||= (binary_mime_type? || binary?) ? mime_type :
(encoding ? "text/plain; charset=#{encoding.downcase}" : "text/plain")
end

View File

@@ -1,6 +1,10 @@
require 'mime/types'
require 'yaml'
class MIME::Type
attr_accessor :override
end
# Register additional mime type extensions
#
# Follows same format as mime-types data file
@@ -33,6 +37,8 @@ File.read(File.expand_path("../mimes.yml", __FILE__)).lines.each do |line|
mime_type.encoding = encoding
end
mime_type.override = true
# Kind of hacky, but we need to reindex the mime type after making changes
MIME::Types.add_type_variant(mime_type)
MIME::Types.index_extensions(mime_type)
@@ -72,8 +78,11 @@ module Linguist
guesses = ::MIME::Types.type_for(ext_or_mime_type)
end
# Prefer text mime types over binary
guesses.detect { |type| type.ascii? } ||
# 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