Commit 2fe66872 by Zach Millman Committed by Jon Yurek

Improve content type detector tests and logic

parent f1f446c2
......@@ -31,8 +31,12 @@ module Paperclip
SENSIBLE_DEFAULT
elsif empty_file?
EMPTY_TYPE
elsif best_from_possible_types
best_from_possible_types.content_type
elsif types_matching_file.any?
types_matching_file.first
elsif official_types.any?
official_types.first
elsif possible_types.any?
possible_types.first
else
type_from_file_command || SENSIBLE_DEFAULT
end.to_s
......@@ -53,19 +57,15 @@ module Paperclip
end
def possible_types
@possible_types ||= MIME::Types.type_for(@filename)
@possible_types ||= MIME::Types.type_for(@filename).collect(&:content_type)
end
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
def types_matching_file
possible_types.select{|type| 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)
@types_matching_file ||= possible_types.select{|content_type| content_type == type_from_file_command}
end
def type_from_file_command
......
......@@ -14,9 +14,12 @@ class ContentTypeDetectorTest < Test::Unit::TestCase
assert_equal "text/plain", Paperclip::ContentTypeDetector.new(tempfile.path).detect
end
should 'return content type of uploaded file if it is an acceptable type' do
@filename = fixture_file("sample_mpeg4.mp4")
should 'return content type of file if it is an acceptable type' do
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
end
......@@ -41,7 +44,5 @@ class ContentTypeDetectorTest < Test::Unit::TestCase
@filename = "/path/to/something"
assert_equal "application/octet-stream", Paperclip::ContentTypeDetector.new(@filename).detect
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