From caa39a2bcfd4c6d8521c9d49e19731de9f7357be Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 28 Sep 2011 09:57:33 -0500 Subject: [PATCH] Use Charlock all the time for binary detection --- lib/linguist/blob_helper.rb | 10 +--- lib/linguist/mime.rb | 20 ------- lib/linguist/pathname.rb | 21 ------- test/test_mime.rb | 116 ------------------------------------ test/test_pathname.rb | 6 -- 5 files changed, 3 insertions(+), 170 deletions(-) diff --git a/lib/linguist/blob_helper.rb b/lib/linguist/blob_helper.rb index 22ce0cd0fe..057557304c 100644 --- a/lib/linguist/blob_helper.rb +++ b/lib/linguist/blob_helper.rb @@ -53,8 +53,7 @@ def mime_type # # Returns a content type String. def content_type - @content_type ||= binary? ? - mime_type : + @content_type ||= binary? ? mime_type : (encoding ? "text/plain; charset=#{encoding.downcase}" : "text/plain") end @@ -93,11 +92,8 @@ def detect_encoding # # Return true or false def binary? - if mime_type = Mime.lookup_mime_type_for(pathname.extname) - mime_type.binary? - else - encoding.nil? || detect_encoding[:type] == :binary - end + return false if data.empty? + encoding.nil? || detect_encoding[:type] == :binary end # Public: Is the blob text? diff --git a/lib/linguist/mime.rb b/lib/linguist/mime.rb index eacd9a84dc..871669ec62 100644 --- a/lib/linguist/mime.rb +++ b/lib/linguist/mime.rb @@ -58,26 +58,6 @@ def self.mime_for(ext) mime_type ? mime_type.simplified : 'text/plain' end - # Internal: Determine if extension or mime type is binary. - # - # ext_or_mime_type - A file extension ".exe" or - # mime type "application/octet-stream". - # - # Returns true or false - def self.binary?(ext_or_mime_type) - mime_type = lookup_mime_type_for(ext_or_mime_type) - mime_type ? mime_type.binary? : false - end - - # Internal: Determine if extension or mime type is plain text. - # - # ext_or_mime_type - A file extension ".txt" or mime type "text/plain". - # - # Returns true or false - def self.text?(ext_or_mime_type) - !binary?(ext_or_mime_type) - end - # Internal: Lookup mime type for extension or mime type # # ext_or_mime_type - A file extension ".txt" or mime type "text/plain". diff --git a/lib/linguist/pathname.rb b/lib/linguist/pathname.rb index 7b029c2f17..f7c3cd78cc 100644 --- a/lib/linguist/pathname.rb +++ b/lib/linguist/pathname.rb @@ -77,27 +77,6 @@ def mime_type @mime_type ||= Mime.mime_for(extname) end - # Public: Get the Content-Type header - # - # This value is used when serving raw blobs. - # - # Examples - # - # Pathname.new('file.txt').content_type - # # => 'text/plain; charset=utf-8' - # - # Returns a content type String. - def content_type - @content_type ||= binary? ? mime_type : 'text/plain; charset=utf8' - end - - # Public: Is the path binary? - # - # Return true or false - def binary? - @binary ||= Mime.binary?(extname) - end - # Public: Return self as String # # Returns a String diff --git a/test/test_mime.rb b/test/test_mime.rb index 1ffcd84b5d..00b4899986 100644 --- a/test/test_mime.rb +++ b/test/test_mime.rb @@ -66,120 +66,4 @@ def test_extension_lookup assert_equal 'text/rust', Mime.mime_for('.rc') assert_equal 'video/quicktime', Mime.mime_for('.mov') end - - def test_binary - assert Mime.binary?('application/octet-stream') - - # Add an assertion for any binary mime types added to mimes.yml. - # Please keep this list alphabetized. - assert Mime.binary?('application/java-archive') - assert Mime.binary?('application/ogg') - assert Mime.binary?('application/pdf') - assert Mime.binary?('application/postscript') - assert Mime.binary?('application/x-gzip') - assert Mime.binary?('application/x-shockwave-flash') - assert Mime.binary?('application/x-silverlight-app') - assert Mime.binary?('application/zip') - assert Mime.binary?('audio/mp4') - assert Mime.binary?('image/gif') - assert Mime.binary?('image/jpeg') - assert Mime.binary?('image/png') - - # Legacy. Prefer testing mime types instead of extensions. - assert Mime.binary?('.a') - assert Mime.binary?('.air') - assert Mime.binary?('.blend') - assert Mime.binary?('.crx') - assert Mime.binary?('.deb') - assert Mime.binary?('.dmg') - assert Mime.binary?('.exe') - assert Mime.binary?('.gem') - assert Mime.binary?('.graffle') - assert Mime.binary?('.gz') - assert Mime.binary?('.icns') - assert Mime.binary?('.ipa') - assert Mime.binary?('.key') - assert Mime.binary?('.lib') - assert Mime.binary?('.mcz') - assert Mime.binary?('.mov') - assert Mime.binary?('.mp3') - assert Mime.binary?('.nib') - assert Mime.binary?('.numbers') - assert Mime.binary?('.o') - assert Mime.binary?('.odp') - assert Mime.binary?('.ods') - assert Mime.binary?('.odt') - assert Mime.binary?('.ogg') - assert Mime.binary?('.ogv') - assert Mime.binary?('.otf') - assert Mime.binary?('.pages') - assert Mime.binary?('.pfx') - assert Mime.binary?('.pigx') - assert Mime.binary?('.plgx') - assert Mime.binary?('.pptx') - assert Mime.binary?('.psd') - assert Mime.binary?('.sib') - assert Mime.binary?('.so') - assert Mime.binary?('.spl') - assert Mime.binary?('.sqlite3') - assert Mime.binary?('.swc') - assert Mime.binary?('.swf') - assert Mime.binary?('.tar') - assert Mime.binary?('.ucode') - assert Mime.binary?('.xpi') - assert Mime.binary?('.zip') - end - - def test_text - # By default, assume the plain text. - assert Mime.text?(nil) - assert Mime.text?('') - - assert Mime.text?('text/plain') - - # Add an assertion for any text mime types added to mimes.yml. - # Please keep this list alphabetized. - assert Mime.text?('application/atom+xml') - assert Mime.text?('application/javascript') - assert Mime.text?('application/json') - assert Mime.text?('application/perl') - assert Mime.text?('application/rdf+xml') - assert Mime.text?('application/sh') - assert Mime.text?('application/x-ms-xbap') - assert Mime.text?('application/x-perl') - assert Mime.text?('application/x-python') - assert Mime.text?('application/x-ruby') - assert Mime.text?('application/xaml+xml') - assert Mime.text?('application/xhtml+xml') - assert Mime.text?('application/xml') - assert Mime.text?('text/cache-manifest') - assert Mime.text?('text/css') - assert Mime.text?('text/csv') - assert Mime.text?('text/html') - assert Mime.text?('text/javascript') - assert Mime.text?('text/plain') - assert Mime.text?('text/x-nimrod') - assert Mime.text?('text/x-nemerle') - - # Legacy. Prefer testing mime types instead of extensions. - assert Mime.text?('.cu') - assert Mime.text?('.js') - assert Mime.text?('.latex') - assert Mime.text?('.ms') - assert Mime.text?('.nc') - assert Mime.text?('.pl') - assert Mime.text?('.pm') - assert Mime.text?('.py') - assert Mime.text?('.rb') - assert Mime.text?('.sc') - assert Mime.text?('.sh') - assert Mime.text?('.sig') - assert Mime.text?('.sml') - assert Mime.text?('.sps') - assert Mime.text?('.src') - assert Mime.text?('.tcl') - assert Mime.text?('.texi') - assert Mime.text?('.texinfo') - assert Mime.text?('.xul') - end end diff --git a/test/test_pathname.rb b/test/test_pathname.rb index 17cd13d6cd..a6a3367cf5 100644 --- a/test/test_pathname.rb +++ b/test/test_pathname.rb @@ -59,10 +59,4 @@ def test_mime_type assert_equal 'application/python', Pathname.new("itty.py").mime_type assert_equal 'text/plain', Pathname.new("defun.kt").mime_type end - - def test_content_type - assert_equal 'text/plain; charset=utf8', Pathname.new("file.txt").content_type - assert_equal 'text/plain; charset=utf8', Pathname.new("file.rb").content_type - assert_equal 'image/png', Pathname.new("octocat.png").content_type - end end