Commit 3103da5d by Matthew Schulkind Committed by Jon Yurek

Fix :content_type_extension when a style :format is specified.

parent 8110922e
......@@ -97,9 +97,12 @@ module Paperclip
File.extname(attachment.original_filename).gsub(/^\.+/, "")
end
# Returns an extension based on the content type. e.g. "jpeg" for "image/jpeg".
# Returns an extension based on the content type. e.g. "jpeg" for
# "image/jpeg". If the style has a specified format, it will override the
# content-type detection.
#
# Each mime type generally has multiple extensions associated with it, so
# if the extension from teh original filename is one of these extensions,
# if the extension from the original filename is one of these extensions,
# that extension is used, otherwise, the first in the list is used.
def content_type_extension attachment, style_name
mime_type = MIME::Types[attachment.content_type]
......@@ -110,7 +113,10 @@ module Paperclip
end
original_extension = extension(attachment, style_name)
if extensions_for_mime_type.include? original_extension
style = attachment.styles[style_name.to_s.to_sym]
if style && style[:format]
style[:format].to_s
elsif extensions_for_mime_type.include? original_extension
original_extension
elsif !extensions_for_mime_type.empty?
extensions_for_mime_type.first
......
......@@ -56,6 +56,7 @@ class InterpolationsTest < Test::Unit::TestCase
should "return the extension of the file based on the content type" do
attachment = mock
attachment.expects(:content_type).returns('image/jpeg')
attachment.expects(:styles).returns({})
interpolations = Paperclip::Interpolations
interpolations.expects(:extension).returns('random')
assert_equal "jpeg", interpolations.content_type_extension(attachment, :style)
......@@ -64,6 +65,7 @@ class InterpolationsTest < Test::Unit::TestCase
should "return the original extension of the file if it matches a content type extension" do
attachment = mock
attachment.expects(:content_type).returns('image/jpeg')
attachment.expects(:styles).returns({})
interpolations = Paperclip::Interpolations
interpolations.expects(:extension).returns('jpe')
assert_equal "jpe", interpolations.content_type_extension(attachment, :style)
......@@ -72,11 +74,21 @@ class InterpolationsTest < Test::Unit::TestCase
should "return the latter half of the content type of the extension if no match found" do
attachment = mock
attachment.expects(:content_type).at_least_once().returns('not/found')
attachment.expects(:styles).returns({})
interpolations = Paperclip::Interpolations
interpolations.expects(:extension).returns('random')
assert_equal "found", interpolations.content_type_extension(attachment, :style)
end
should "return the format if defined in the style, ignoring the content type" do
attachment = mock
attachment.expects(:content_type).returns('image/jpeg')
attachment.expects(:styles).returns({:style => {:format => "png"}})
interpolations = Paperclip::Interpolations
interpolations.expects(:extension).returns('random')
assert_equal "png", interpolations.content_type_extension(attachment, :style)
end
should "be able to handle numeric style names" do
attachment = mock(
:styles => {:"4" => {:format => :expected_extension}}
......
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