Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add `:restricted_characters` default options

Introducing `:restricted_characters` in Paperclip::Attachment.default_options  so people can override their blacklist characters by override that setting.
  • Loading branch information...
commit 83535184b0a40bb6b73987e210472f5457d15d79 1 parent 604304e
@sikachu sikachu authored
Showing with 43 additions and 14 deletions.
  1. +3 −0  ChangeLog
  2. +6 −3 lib/paperclip/attachment.rb
  3. +34 −11 test/attachment_test.rb
View
3  ChangeLog
@@ -1,5 +1,8 @@
2012-01-27 Prem Sichanugrist <psichanugrist@thoughtbot.com>
+* 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
View
9 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
View
45 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
Please sign in to comment.
Something went wrong with that request. Please try again.