Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Add :restricted_characters default options
Browse files Browse the repository at this point in the history
Introducing `:restricted_characters` in Paperclip::Attachment.default_options  so people can override their blacklist characters by override that setting.
  • Loading branch information
sikachu committed Jan 27, 2012
1 parent 604304e commit 8353518
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
3 changes: 3 additions & 0 deletions 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
Expand Down
9 changes: 6 additions & 3 deletions lib/paperclip/attachment.rb
Expand Up @@ -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,
Expand All @@ -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

Expand Down Expand Up @@ -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
45 changes: 34 additions & 11 deletions test/attachment_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 8353518

Please sign in to comment.