Skip to content

Commit

Permalink
added sanitizing of filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
rolfb committed Jun 9, 2008
1 parent edfff2f commit 14fb769
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/paperclip/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 27 additions & 0 deletions test/test_attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 14fb769

Please sign in to comment.