From a12bde58e0714dd81c0de67640034429026d0a13 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Thu, 2 Jun 2011 12:42:56 -0500 Subject: [PATCH] Kill attachment stuff, just use viewable --- lib/linguist/blob_helper.rb | 6 +- lib/linguist/mime.rb | 37 +-------- lib/linguist/pathname.rb | 31 -------- test/test_mime.rb | 150 +++++++++++++++--------------------- 4 files changed, 71 insertions(+), 153 deletions(-) diff --git a/lib/linguist/blob_helper.rb b/lib/linguist/blob_helper.rb index 1d04de0e..0f16d69c 100644 --- a/lib/linguist/blob_helper.rb +++ b/lib/linguist/blob_helper.rb @@ -62,7 +62,11 @@ module Linguist # # Returns a content disposition String. def disposition - pathname.disposition + if viewable? + 'inline' + else + "attachment; filename=#{EscapeUtils.escape_url(pathname.basename)}" + end end # Public: Is the blob text? diff --git a/lib/linguist/mime.rb b/lib/linguist/mime.rb index 42c8f1b9..22dd754f 100644 --- a/lib/linguist/mime.rb +++ b/lib/linguist/mime.rb @@ -15,16 +15,6 @@ module MIME @encoding == 'base64' end end - - attr_accessor :attachment - - def attachment? - if defined? @attachment - @attachment - else - binary? - end - end end end @@ -35,8 +25,7 @@ mime_extensions.each do |mime_type, options| (options['extensions'] || []).each { |ext| mime.extensions << ext } - mime.binary = options['binary'] if options.key?('binary') - mime.attachment = options['attachment'] if options.key?('attachment') + mime.binary = options['binary'] if options.key?('binary') MIME::Types.add_type_variant(mime) MIME::Types.index_extensions(mime) @@ -58,9 +47,8 @@ module Linguist # # Return mime type String otherwise falls back to 'text/plain'. def self.mime_for(ext) - ext ||= '' - guesses = ::MIME::Types.type_for(ext) - guesses.first ? guesses.first.simplified : 'text/plain' + mime_type = lookup_mime_type_for(ext) + mime_type ? mime_type.simplified : 'text/plain' end Special = YAML.load_file(File.expand_path("../content_types.yml", __FILE__)) @@ -103,25 +91,6 @@ module Linguist mime_type.nil? || mime_type.binary? end - # Internal: Determine if extension or mime type is an attachment. - # - # ext_or_mime_type - A file extension ".txt" or mime type "text/plain". - # - # Attachments are files that should be downloaded rather than be - # displayed in the browser. - # - # This is used to set our Content-Disposition headers. - # - # Attachment files should generally binary files but non- - # attachments do not imply plain text. For an example Images are - # not treated as attachments. - # - # Returns true or false - def self.attachment?(ext_or_mime_type) - mime_type = lookup_mime_type_for(ext_or_mime_type) - mime_type.nil? || mime_type.attachment? - end - # Internal: Lookup mime type for extension or mime type # # Returns a MIME::Type diff --git a/lib/linguist/pathname.rb b/lib/linguist/pathname.rb index 956d41c7..31a84a4b 100644 --- a/lib/linguist/pathname.rb +++ b/lib/linguist/pathname.rb @@ -110,37 +110,6 @@ module Linguist @content_type ||= Mime.content_type_for(extname) end - # Public: Determine if the Pathname is a binary mime type. - # - # Returns true or false. - def binary? - @binary ||= language? ? false : Mime.binary?(extname) - end - - # Public: Determine if the Pathname should be served as an - # attachment. - # - # Returns true or false. - def attachment? - @attachment ||= language? ? false : Mime.attachment?(extname) - end - - # Public: Get the Content-Disposition header value - # - # This value is used when serving raw blobs. - # - # # => "attachment; filename=file.tar" - # # => "inline" - # - # Returns a content disposition String. - def disposition - if attachment? - "attachment; filename=#{EscapeUtils.escape_url(basename)}" - else - 'inline' - end - end - def to_s @path.dup end diff --git a/test/test_mime.rb b/test/test_mime.rb index 58bb47a3..5bbd5dfd 100644 --- a/test/test_mime.rb +++ b/test/test_mime.rb @@ -54,12 +54,58 @@ class TestMime < Test::Unit::TestCase assert Mime.binary?("application/octet-stream") assert !Mime.binary?("text/plain") + assert Mime.binary?("application/java-archive") + assert Mime.binary?("application/ogg") + assert Mime.binary?("application/pdf") + assert Mime.binary?("application/x-gzip") + 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") assert Mime.binary?("java-archive") assert Mime.binary?("x-shockwave-flash") + 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?(".lib") + assert Mime.binary?(".mcz") + assert Mime.binary?(".mov") + assert Mime.binary?(".mp3") + assert Mime.binary?(".nib") + 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?(".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") + assert !Mime.binary?("application/atom+xml") assert !Mime.binary?("application/javascript") assert !Mime.binary?("application/json") @@ -68,97 +114,27 @@ class TestMime < Test::Unit::TestCase assert !Mime.binary?("application/x-perl") assert !Mime.binary?("application/x-python") assert !Mime.binary?("application/x-ruby") + assert !Mime.binary?("application/xhtml+xml") + assert !Mime.binary?("application/xml") + assert !Mime.binary?("text/css") + assert !Mime.binary?("text/csv") + assert !Mime.binary?("text/html") + assert !Mime.binary?("text/javascript") + assert !Mime.binary?("text/plain") + assert !Mime.binary?(".js") + assert !Mime.binary?(".latex") assert !Mime.binary?(".ms") assert !Mime.binary?(".nc") + assert !Mime.binary?(".pl") + assert !Mime.binary?(".ps") + assert !Mime.binary?(".py") + assert !Mime.binary?(".rb") + assert !Mime.binary?(".sh") assert !Mime.binary?(".src") + assert !Mime.binary?(".tcl") + assert !Mime.binary?(".texi") + assert !Mime.binary?(".texinfo") assert !Mime.binary?(".xul") end - - def test_attachment - assert Mime.attachment?("application/octet-stream") - assert !Mime.attachment?("text/plain") - - assert Mime.attachment?("application/java-archive") - assert Mime.attachment?("application/ogg") - assert Mime.attachment?("application/pdf") - assert Mime.attachment?("application/x-gzip") - assert Mime.attachment?("application/zip") - assert Mime.attachment?("audio/mp4") - - assert Mime.attachment?(".a") - assert Mime.attachment?(".air") - assert Mime.attachment?(".blend") - assert Mime.attachment?(".crx") - assert Mime.attachment?(".deb") - assert Mime.attachment?(".dmg") - assert Mime.attachment?(".exe") - assert Mime.attachment?(".gem") - assert Mime.attachment?(".graffle") - assert Mime.attachment?(".gz") - assert Mime.attachment?(".icns") - assert Mime.attachment?(".ipa") - assert Mime.attachment?(".lib") - assert Mime.attachment?(".mcz") - assert Mime.attachment?(".mov") - assert Mime.attachment?(".mp3") - assert Mime.attachment?(".nib") - assert Mime.attachment?(".o") - assert Mime.attachment?(".odp") - assert Mime.attachment?(".ods") - assert Mime.attachment?(".odt") - assert Mime.attachment?(".ogg") - assert Mime.attachment?(".ogv") - assert Mime.attachment?(".otf") - assert Mime.attachment?(".pfx") - assert Mime.attachment?(".pigx") - assert Mime.attachment?(".plgx") - assert Mime.attachment?(".pptx") - assert Mime.attachment?(".psd") - assert Mime.attachment?(".sib") - assert Mime.attachment?(".so") - assert Mime.attachment?(".spl") - assert Mime.attachment?(".sqlite3") - assert Mime.attachment?(".swc") - assert Mime.attachment?(".swf") - assert Mime.attachment?(".tar") - assert Mime.attachment?(".ucode") - assert Mime.attachment?(".xpi") - assert Mime.attachment?(".zip") - - assert !Mime.attachment?("application/atom+xml") - assert !Mime.attachment?("application/javascript") - assert !Mime.attachment?("application/json") - assert !Mime.attachment?("application/rdf+xml") - assert !Mime.attachment?("application/sh") - assert !Mime.attachment?("application/xhtml+xml") - assert !Mime.attachment?("application/xml") - assert !Mime.attachment?("image/gif") - assert !Mime.attachment?("image/jpeg") - assert !Mime.attachment?("image/png") - assert !Mime.attachment?("text/css") - assert !Mime.attachment?("text/csv") - assert !Mime.attachment?("text/html") - assert !Mime.attachment?("text/javascript") - assert !Mime.attachment?("text/plain") - - assert !Mime.attachment?(".gif") - assert !Mime.attachment?(".jpeg") - assert !Mime.attachment?(".jpg") - assert !Mime.attachment?(".js") - assert !Mime.attachment?(".latex") - assert !Mime.attachment?(".ms") - assert !Mime.attachment?(".nc") - assert !Mime.attachment?(".pl") - assert !Mime.attachment?(".png") - assert !Mime.attachment?(".ps") - assert !Mime.attachment?(".py") - assert !Mime.attachment?(".rb") - assert !Mime.attachment?(".sh") - assert !Mime.attachment?(".src") - assert !Mime.attachment?(".tcl") - assert !Mime.attachment?(".texi") - assert !Mime.attachment?(".texinfo") - assert !Mime.attachment?(".xul") - end end