Split mime type and content type

This commit is contained in:
Joshua Peek
2011-05-20 11:24:32 -05:00
parent 0f0f6d282b
commit 0651355e71
7 changed files with 83 additions and 45 deletions

View File

@@ -11,18 +11,15 @@ module Linguist
end
def mime_type
@mime_type ||= begin
guesses = MIME::Types.type_for(pathname.extname)
guesses.first ? guesses.first.simplified : 'text/plain'
end
@mime_type ||= pathname.mime_type
end
def special_mime_type
Mime.lookup(pathname.extname)
def content_type
pathname.content_type
end
def disposition
case special_mime_type
case content_type
when 'application/octet-stream', 'application/java-archive'
"attachment; filename=#{EscapeUtils.escape_url(pathname.basename)}"
else
@@ -43,7 +40,7 @@ module Linguist
end
def binary?
special_mime_type == 'octet-stream' || !(text? || image?)
content_type.include?('octet') || !(text? || image?)
end
def file?
@@ -51,8 +48,7 @@ module Linguist
end
def text?
pathname.media_type == 'text' ||
pathname.mime_type == 'application/json'
content_type[/(text|json)/]
end
def image?

View File

@@ -12,9 +12,6 @@ air: application/octet-stream
blend: application/octet-stream
crx: application/octet-stream
deb: application/octet-stream
dll: application/octet-stream
dmg: application/octet-stream
exe: application/octet-stream
gem: application/octet-stream
graffle: application/octet-stream
gz: application/octet-stream

View File

@@ -1,5 +1,11 @@
require 'mime/types'
# Register additional binary extensions
binary = MIME::Types['application/octet-stream'].first
binary.extensions << 'dmg'
binary.extensions << 'dll'
MIME::Types.index_extensions(binary)
# Register 'ear' and 'war' as java
java = MIME::Types['application/java-archive'].first
java.extensions << 'ear'
@@ -8,20 +14,19 @@ MIME::Types.index_extensions(java)
module Linguist
module Mime
Special = YAML.load_file(File.expand_path("../special_mime_types.yml", __FILE__))
Special = YAML.load_file(File.expand_path("../content_types.yml", __FILE__))
def self.lookup(ext)
def self.mime_for(ext)
ext ||= ''
guesses = ::MIME::Types.type_for(ext)
orginal_type = guesses.first ? guesses.first.simplified : 'text/plain'
type = Special[orginal_type] ||
Special[ext.sub(/^\./, '')] ||
orginal_type
guesses.first ? guesses.first.simplified : 'text/plain'
end
def self.content_type_for(ext)
ext ||= ''
type = mime_for(ext)
type = Special[type] || Special[ext.sub(/^\./, '')] || type
type += '; charset=utf-8' if type =~ /^text\//
type
end
end

View File

@@ -34,11 +34,19 @@ module Linguist
end
def mime_type
@mime_type ||= Mime.lookup(extname)
@mime_type ||= Mime.mime_for(extname)
end
def media_type
mime_type.split('/').first
mime_type.split('/')[0]
end
def sub_type
mime_type.split('/')[1]
end
def content_type
@content_type ||= Mime.content_type_for(extname)
end
def to_s