Use mime binary

This commit is contained in:
Joshua Peek
2011-06-03 10:44:21 -05:00
parent d9990aed06
commit 40efc2bd44
3 changed files with 20 additions and 12 deletions

View File

@@ -62,18 +62,25 @@ module Linguist
# #
# Returns a content disposition String. # Returns a content disposition String.
def disposition def disposition
if viewable? if image? || viewable?
'inline' 'inline'
else else
"attachment; filename=#{EscapeUtils.escape_url(pathname.basename)}" "attachment; filename=#{EscapeUtils.escape_url(pathname.basename)}"
end end
end end
# Public: Is the blob binary?
#
# Return true or false
def binary?
pathname.binary?
end
# Public: Is the blob text? # Public: Is the blob text?
# #
# Return true or false # Return true or false
def text? def text?
content_type[/(text|json)/] !binary?
end end
# Public: Is the blob a supported image format? # Public: Is the blob a supported image format?
@@ -83,13 +90,6 @@ module Linguist
['.png', '.jpg', '.jpeg', '.gif'].include?(extname) ['.png', '.jpg', '.jpeg', '.gif'].include?(extname)
end end
# Public: Is the blob binary?
#
# Return true or false
def binary?
content_type.include?('octet') || !(text? || image?)
end
MEGABYTE = 1024 * 1024 MEGABYTE = 1024 * 1024
# Public: Is the blob too big to load? # Public: Is the blob too big to load?
@@ -105,7 +105,7 @@ module Linguist
# #
# Return true or false # Return true or false
def viewable? def viewable?
!image? && !binary? && !large? text? && !large?
end end
vendored_paths = YAML.load_file(File.expand_path("../vendor.yml", __FILE__)) vendored_paths = YAML.load_file(File.expand_path("../vendor.yml", __FILE__))

View File

@@ -110,6 +110,13 @@ module Linguist
@content_type ||= Mime.content_type_for(extname) @content_type ||= Mime.content_type_for(extname)
end end
# Public: Is the path binary?
#
# Return true or false
def binary?
@binary ||= Mime.binary?(extname)
end
def to_s def to_s
@path.dup @path.dup
end end

View File

@@ -43,9 +43,10 @@ class TestBlob < Test::Unit::TestCase
assert_equal "attachment; filename=foo.bin", blob("foo.bin").disposition assert_equal "attachment; filename=foo.bin", blob("foo.bin").disposition
assert_equal "attachment; filename=linguist.gem", blob("pkg/linguist.gem").disposition assert_equal "attachment; filename=linguist.gem", blob("pkg/linguist.gem").disposition
assert_equal "attachment; filename=foo+bar.jar", blob("foo bar.jar").disposition assert_equal "attachment; filename=foo+bar.jar", blob("foo bar.jar").disposition
assert_equal "inline", blob("README").disposition
assert_equal "inline", blob("foo.txt").disposition assert_equal "inline", blob("foo.txt").disposition
assert_equal "inline", blob("grit.rb").disposition assert_equal "inline", blob("grit.rb").disposition
assert_equal "inline", blob("README").disposition assert_equal "inline", blob("octocat.png").disposition
end end
def test_data def test_data
@@ -72,10 +73,10 @@ class TestBlob < Test::Unit::TestCase
assert blob("linguist.gem").binary? assert blob("linguist.gem").binary?
assert blob("git.deb").binary? assert blob("git.deb").binary?
assert blob("git.exe").binary? assert blob("git.exe").binary?
assert blob("octocat.png").binary?
assert !blob("README").binary? assert !blob("README").binary?
assert !blob("file.txt").binary? assert !blob("file.txt").binary?
assert !blob("foo.rb").binary? assert !blob("foo.rb").binary?
assert !blob("octocat.png").binary?
assert !blob("script.pl").binary? assert !blob("script.pl").binary?
end end