Skip to content
This repository
Browse code

Return a URI.escape'd URL from attachment

It's Paperclip's responsibility to escape special characters from the URL to make sure that it's comply with standard.

Closes #577, Closes #563, and reverse my judgement on #482.
  • Loading branch information...
commit 23cb822de3807404589706ead964f2ef0d759ec9 1 parent 0ca98d1
Prem Sichanugrist authored September 24, 2011
4  lib/paperclip/attachment.rb
... ...
@@ -1,4 +1,6 @@
1 1
 # encoding: utf-8
  2
+require 'uri'
  3
+
2 4
 module Paperclip
3 5
   # The Attachment class manages the files for a given attachment. It saves
4 6
   # when the model saves, deletes when the model is destroyed, and processes
@@ -155,7 +157,7 @@ def assign uploaded_file
155 157
     def url(style_name = default_style, use_timestamp = @use_timestamp)
156 158
       default_url = @default_url.is_a?(Proc) ? @default_url.call(self) : @default_url
157 159
       url = original_filename.nil? ? interpolate(default_url, style_name) : interpolate(@url, style_name)
158  
-      use_timestamp && updated_at ? [url, updated_at].compact.join(url.include?("?") ? "&" : "?") : url
  160
+      URI.escape(use_timestamp && updated_at ? [url, updated_at].compact.join(url.include?("?") ? "&" : "?") : url)
159 161
     end
160 162
 
161 163
     # Returns the path of the attachment as defined by the :path option. If the
15  test/attachment_test.rb
@@ -918,7 +918,22 @@ def do_after_all; end
918 918
           end
919 919
         end
920 920
       end
  921
+    end
921 922
 
  923
+    context "with a file that has space in file name" do
  924
+      setup do
  925
+        @attachment.stubs(:instance_read).with(:file_name).returns("spaced file.png")
  926
+        @attachment.stubs(:instance_read).with(:content_type).returns("image/png")
  927
+        @attachment.stubs(:instance_read).with(:file_size).returns(12345)
  928
+        dtnow = DateTime.now
  929
+        @now = Time.now
  930
+        Time.stubs(:now).returns(@now)
  931
+        @attachment.stubs(:instance_read).with(:updated_at).returns(dtnow)
  932
+      end
  933
+
  934
+      should "returns an escaped version of the URL" do
  935
+        assert_match /\/spaced%20file\.png/, @attachment.url
  936
+      end
922 937
     end
923 938
 
924 939
     context "when trying a nonexistant storage type" do
BIN  test/fixtures/spaced file.png
18  test/storage/filesystem_test.rb
@@ -30,5 +30,23 @@ class FileSystemTest < Test::Unit::TestCase
30 30
 
31 31
       @dummy.save!
32 32
     end
  33
+
  34
+    context "with file that has space in file name" do
  35
+      setup do
  36
+        rebuild_model :styles => { :thumbnail => "25x25#" }
  37
+        @dummy = Dummy.create!
  38
+
  39
+        @dummy.avatar = File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "spaced file.png"))
  40
+        @dummy.save
  41
+      end
  42
+
  43
+      should "store the file" do
  44
+        assert File.exists?(@dummy.avatar.path)
  45
+      end
  46
+
  47
+      should "return an escaped version of URL" do
  48
+        assert_match /\/spaced%20file\.png/, @dummy.avatar.url
  49
+      end
  50
+    end
33 51
   end
34 52
 end
21  test/storage/s3_test.rb
@@ -110,6 +110,27 @@ def rails_env(env)
110 110
     end
111 111
   end
112 112
 
  113
+  context "An attachment that uses S3 for storage and has spaces in file name" do
  114
+    setup do
  115
+      AWS::S3::Base.stubs(:establish_connection!)
  116
+      rebuild_model :styles  => { :large => ['500x500#', :jpg] },
  117
+                    :storage => :s3,
  118
+                    :bucket  => "bucket",
  119
+                    :path => ":attachment/:basename.:extension",
  120
+                    :s3_credentials => {
  121
+                      'access_key_id' => "12345",
  122
+                      'secret_access_key' => "54321"
  123
+                    }
  124
+
  125
+      @dummy = Dummy.new
  126
+      @dummy.avatar = File.new(File.join(File.dirname(__FILE__), '..', 'fixtures', 'spaced file.png'), 'rb')
  127
+    end
  128
+
  129
+    should "return an escaped version of url" do
  130
+      assert_match /.+\/spaced%20file\.png/, @dummy.avatar.url
  131
+    end
  132
+  end
  133
+
113 134
   context "" do
114 135
     setup do
115 136
       AWS::S3::Base.stubs(:establish_connection!)

0 notes on commit 23cb822

Owen

Thanks for making this change! I wish I knew that it happened though as the result was some broken enclosure links in my RSS feed because they were getting double encoded.

Filipe Giusti

We are 2 with double encoded urls.

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