Commit 9fc74e07 by Jon Yurek

Extra checking on the content type detector

parent f407412f
module Paperclip module Paperclip
class ContentTypeDetector class ContentTypeDetector
EMPTY_TYPE = "inode/x-empty"
SENSIBLE_DEFAULT = "application/octet-stream"
def initialize(filename) def initialize(filename)
@filename = filename @filename = filename
end end
def detect def detect
if !match? if blank?
SENSIBLE_DEFAULT
elsif empty?
EMPTY_TYPE
elsif !match?
type_from_file_command type_from_file_command
elsif !multiple? elsif !multiple?
possible_types.first possible_types.first
...@@ -16,6 +23,14 @@ module Paperclip ...@@ -16,6 +23,14 @@ module Paperclip
private private
def empty?
File.exists?(@filename) && File.size(@filename) == 0
end
def blank?
@filename.nil? || @filename.empty?
end
def possible_types def possible_types
@possible_types ||= MIME::Types.type_for(@filename) @possible_types ||= MIME::Types.type_for(@filename)
end end
...@@ -37,7 +52,7 @@ module Paperclip ...@@ -37,7 +52,7 @@ module Paperclip
# On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist. # On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
type = Paperclip.run("file", "-b --mime :file", :file => @filename) type = Paperclip.run("file", "-b --mime :file", :file => @filename)
if type.match(/\(.*?\)/) if type.match(/\(.*?\)/)
type = "application/octet-stream" type = SENSIBLE_DEFAULT
end end
type.split(/[:;\s]+/)[0] type.split(/[:;\s]+/)[0]
end end
......
...@@ -15,6 +15,17 @@ class ContentTypeDetectorTest < Test::Unit::TestCase ...@@ -15,6 +15,17 @@ class ContentTypeDetectorTest < Test::Unit::TestCase
assert_equal "text/plain", Paperclip::ContentTypeDetector.new(tempfile.path).detect assert_equal "text/plain", Paperclip::ContentTypeDetector.new(tempfile.path).detect
end end
should 'return an empty content type if the file is empty' do
tempfile = Tempfile.new("something")
tempfile.rewind
assert_equal "inode/x-empty", Paperclip::ContentTypeDetector.new(tempfile.path).detect
end
should 'return a sensible default if no filename is supplied' do
assert_equal "application/octet-stream", Paperclip::ContentTypeDetector.new('').detect
end
should 'return a sensible default if something goes wrong' do should 'return a sensible default if something goes wrong' do
@filename = "/path/to/something" @filename = "/path/to/something"
assert_equal "application/octet-stream", Paperclip::ContentTypeDetector.new(@filename).detect assert_equal "application/octet-stream", Paperclip::ContentTypeDetector.new(@filename).detect
......
...@@ -5,15 +5,14 @@ class AbstractAdapterTest < Test::Unit::TestCase ...@@ -5,15 +5,14 @@ class AbstractAdapterTest < Test::Unit::TestCase
attr_accessor :original_file_name, :tempfile attr_accessor :original_file_name, :tempfile
def content_type def content_type
type_from_file_command Paperclip::ContentTypeDetector.new(path).detect
end end
end end
context "content type from file command" do context "content type from file command" do
setup do setup do
@adapter = TestAdapter.new @adapter = TestAdapter.new
@adapter.stubs(:path) @adapter.stubs(:path).returns("image.png")
Paperclip.stubs(:run).returns("image/png\n")
end end
should "return the content type without newline" do should "return the content type without newline" do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment