Commit 2fe66872 by Zach Millman Committed by Jon Yurek

Improve content type detector tests and logic

parent f1f446c2
...@@ -31,8 +31,12 @@ module Paperclip ...@@ -31,8 +31,12 @@ module Paperclip
SENSIBLE_DEFAULT SENSIBLE_DEFAULT
elsif empty_file? elsif empty_file?
EMPTY_TYPE EMPTY_TYPE
elsif best_from_possible_types elsif types_matching_file.any?
best_from_possible_types.content_type types_matching_file.first
elsif official_types.any?
official_types.first
elsif possible_types.any?
possible_types.first
else else
type_from_file_command || SENSIBLE_DEFAULT type_from_file_command || SENSIBLE_DEFAULT
end.to_s end.to_s
...@@ -53,19 +57,15 @@ module Paperclip ...@@ -53,19 +57,15 @@ module Paperclip
end end
def possible_types def possible_types
@possible_types ||= MIME::Types.type_for(@filename) @possible_types ||= MIME::Types.type_for(@filename).collect(&:content_type)
end end
def official_types def official_types
@official_types ||= possible_types.reject {|type| type.content_type.match(/\/x-/) } @offical_types ||= possible_types.reject{|content_type| content_type.match(/\/x-/) }
end end
def types_matching_file def types_matching_file
possible_types.select{|type| type.content_type == type_from_file_command} @types_matching_file ||= possible_types.select{|content_type| content_type == type_from_file_command}
end
def best_from_possible_types
@best_from_possible_types ||= (types_matching_file.first || official_types.first || possible_types.first)
end end
def type_from_file_command def type_from_file_command
......
...@@ -14,9 +14,12 @@ class ContentTypeDetectorTest < Test::Unit::TestCase ...@@ -14,9 +14,12 @@ 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 content type of uploaded file if it is an acceptable type' do should 'return content type of file if it is an acceptable type' do
@filename = fixture_file("sample_mpeg4.mp4") MIME::Types.stubs(:type_for).returns([MIME::Type.new('application/mp4'), MIME::Type.new('video/mp4'), MIME::Type.new('audio/mp4')])
Paperclip.stubs(:run).returns("video/mp4")
@filename = "my_file.mp4"
assert_equal "video/mp4", Paperclip::ContentTypeDetector.new(@filename).detect assert_equal "video/mp4", Paperclip::ContentTypeDetector.new(@filename).detect
end end
...@@ -41,7 +44,5 @@ class ContentTypeDetectorTest < Test::Unit::TestCase ...@@ -41,7 +44,5 @@ class ContentTypeDetectorTest < Test::Unit::TestCase
@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
end end
end end
end end
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