Skip to content
This repository
Browse code

Add a :content_type_extension interpolation to derive a file extensio…

…n from the content type.
  • Loading branch information...
commit fdbd7bfa1033cf597d3d23432827d57f457f8e48 1 parent dcad54d
Matthew Schulkind authored July 12, 2011
25  lib/paperclip/interpolations.rb
@@ -94,6 +94,31 @@ def extension attachment, style_name
94 94
         File.extname(attachment.original_filename).gsub(/^\.+/, "")
95 95
     end
96 96
 
  97
+    # Returns an extension based on the content type. e.g. "jpeg" for "image/jpeg".
  98
+    # Each mime type generally has multiple extensions associated with it, so
  99
+    # if the extension from teh original filename is one of these extensions,
  100
+    # that extension is used, otherwise, the first in the list is used.
  101
+    def content_type_extension attachment, style_name
  102
+      mime_type = MIME::Types[attachment.content_type]
  103
+      extensions_for_mime_type = unless mime_type.empty?
  104
+        mime_type.first.extensions
  105
+      else
  106
+        []
  107
+      end
  108
+
  109
+      original_extension = extension(attachment, style_name)
  110
+      if extensions_for_mime_type.include? original_extension
  111
+        original_extension
  112
+      elsif !extensions_for_mime_type.empty?
  113
+        extensions_for_mime_type.first
  114
+      else
  115
+        # It's possible, though unlikely, that the mime type is not in the
  116
+        # database, so just use the part after the '/' in the mime type as the
  117
+        # extension.
  118
+        %r{/([^/]*)$}.match(attachment.content_type)[1]
  119
+      end
  120
+    end
  121
+
97 122
     # Returns the id of the instance.
98 123
     def id attachment, style_name
99 124
       attachment.instance.id
24  test/interpolations_test.rb
@@ -50,6 +50,30 @@ class InterpolationsTest < Test::Unit::TestCase
50 50
     assert_equal "png", Paperclip::Interpolations.extension(attachment, :style)
51 51
   end
52 52
 
  53
+  should "return the extension of the file based on the content type" do
  54
+    attachment = mock
  55
+    attachment.expects(:content_type).returns('image/jpeg')
  56
+    interpolations = Paperclip::Interpolations
  57
+    interpolations.expects(:extension).returns('random')
  58
+    assert_equal "jpeg", interpolations.content_type_extension(attachment, :style)
  59
+  end
  60
+
  61
+  should "return the original extension of the file if it matches a content type extension" do
  62
+    attachment = mock
  63
+    attachment.expects(:content_type).returns('image/jpeg')
  64
+    interpolations = Paperclip::Interpolations
  65
+    interpolations.expects(:extension).returns('jpe')
  66
+    assert_equal "jpe", interpolations.content_type_extension(attachment, :style)
  67
+  end
  68
+
  69
+  should "return the latter half of the content type of the extension if no match found" do
  70
+    attachment = mock
  71
+    attachment.expects(:content_type).at_least_once().returns('not/found')
  72
+    interpolations = Paperclip::Interpolations
  73
+    interpolations.expects(:extension).returns('random')
  74
+    assert_equal "found", interpolations.content_type_extension(attachment, :style)
  75
+  end
  76
+
53 77
   should "return the #to_param of the attachment" do
54 78
     attachment = mock
55 79
     attachment.expects(:to_param).returns("23-awesome")

0 notes on commit fdbd7bf

Please sign in to comment.
Something went wrong with that request. Please try again.