Skip to content
Browse files

added sanitizing of filenames

  • Loading branch information...
1 parent edfff2f commit 14fb769806084f77b23e795e82867694b158d5a0 @rolfb rolfb committed Jun 9, 2008
Showing with 28 additions and 1 deletion.
  1. +1 −1 lib/paperclip/attachment.rb
  2. +27 −0 test/test_attachment.rb
View
2 lib/paperclip/attachment.rb
@@ -58,7 +58,7 @@ def assign uploaded_file
return nil if uploaded_file.nil?
@queued_for_write[:original] = uploaded_file.to_tempfile
- @instance[:"#{@name}_file_name"] = uploaded_file.original_filename.strip
+ @instance[:"#{@name}_file_name"] = uploaded_file.original_filename.strip.gsub /[^A-Za-z0-9\.]/, '_'
@instance[:"#{@name}_content_type"] = uploaded_file.content_type.strip
@instance[:"#{@name}_file_size"] = uploaded_file.size.to_i
View
27 test/test_attachment.rb
@@ -113,6 +113,33 @@ class AttachmentTest < Test::Unit::TestCase
should "strip whitespace from content_type field" do
assert_equal "image/png", @dummy.avatar.instance.avatar_content_type
end
+
+ end
+
+ context "Attachment with strange letters" do
+ setup do
+ rebuild_model
+
+ @not_file = mock
+ @not_file.stubs(:nil?).returns(false)
+ @not_file.expects(:to_tempfile).returns(self)
+ @not_file.expects(:original_filename).returns("sheep_say_bæ.png\r\n")
+ @not_file.expects(:content_type).returns("image/png\r\n")
+ @not_file.expects(:size).returns(10)
+
+ @dummy = Dummy.new
+ @attachment = @dummy.avatar
+ @attachment.expects(:valid_assignment?).with(@not_file).returns(true)
+ @attachment.expects(:queue_existing_for_delete)
+ @attachment.expects(:post_process)
+ @attachment.expects(:validate)
+ @dummy.avatar = @not_file
+ end
+
+ should "remove strange letters and replace with underscore (_)" do
+ assert_equal "sheep_say_b__.png", @dummy.avatar.original_filename
+ end
+
end
context "An attachment" do

0 comments on commit 14fb769

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