diff --git a/ChangeLog b/ChangeLog index 295272dba..2d15ee787 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2012-01-27 Prem Sichanugrist +* Introducing `:restricted_characters` in Paperclip::Attachment.default_options + so people can override their blacklist characters by override that setting. + * Paperclip will now replace all the special characters in the filename to an underscore. This is a more desired behavior against having to deal with URL escaping and unescaping later. You can see the list of blacklist characters diff --git a/lib/paperclip/attachment.rb b/lib/paperclip/attachment.rb index fc83105a5..41f848aa6 100644 --- a/lib/paperclip/attachment.rb +++ b/lib/paperclip/attachment.rb @@ -14,6 +14,7 @@ def self.default_options :convert_options => {}, :default_style => :original, :default_url => "/:attachment/:style/missing.png", + :restricted_characters => /[&$+,\/:;=?@<>\[\]\{\}\|\\\^~%# ]/, :hash_data => ":class/:attachment/:id/:style/:updated_at", :hash_digest => "SHA1", :interpolator => Paperclip::Interpolations, @@ -25,10 +26,10 @@ def self.default_options :storage => :filesystem, :styles => {}, :url => "/system/:attachment/:id/:style/:filename", - :url_generator => Paperclip::UrlGenerator + :url_generator => Paperclip::UrlGenerator, :use_default_time_zone => true, :use_timestamp => true, - :whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails], + :whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails] } end @@ -479,7 +480,9 @@ def after_flush_writes end def cleanup_filename(filename) - filename.gsub(/[&$+,\/:;=?@<>\[\]\{\}\|\\\^~%# ]/, '_') + if @options[:restricted_characters] + filename.gsub(@options[:restricted_characters], '_') + end end end end diff --git a/test/attachment_test.rb b/test/attachment_test.rb index 3a3287447..5ff83e7dd 100644 --- a/test/attachment_test.rb +++ b/test/attachment_test.rb @@ -714,22 +714,45 @@ def do_after_all; end end context "Attachment with reserved filename" do - "&$+,/:;=?@<>[]{}|\^~%# ".split(//).each do |character| - context "with character #{character}" do - setup do - rebuild_model + setup do + rebuild_model + @file = StringIO.new(".") + end - file = StringIO.new(".") - file.stubs(:original_filename).returns("file#{character}name.png") - @dummy = Dummy.new - @dummy.avatar = file - end + context "with default configuration" do + "&$+,/:;=?@<>[]{}|\^~%# ".split(//).each do |character| + context "with character #{character}" do + setup do + @file.stubs(:original_filename).returns("file#{character}name.png") + @dummy = Dummy.new + @dummy.avatar = @file + end - should "convert special character into underscore" do - assert_equal "file_name.png", @dummy.avatar.original_filename + should "convert special character into underscore" do + assert_equal "file_name.png", @dummy.avatar.original_filename + end end end end + + context "with specified regexp replacement" do + setup do + @old_defaults = Paperclip::Attachment.default_options.dup + Paperclip::Attachment.default_options.merge! :restricted_characters => /o/ + + @file.stubs(:original_filename).returns("goood.png") + @dummy = Dummy.new + @dummy.avatar = @file + end + + teardown do + Paperclip::Attachment.default_options.merge! @old_defaults + end + + should "match and convert that character" do + assert_equal "g___d.png", @dummy.avatar.original_filename + end + end end context "Attachment with uppercase extension and a default style" do