Commit 83535184 by Prem Sichanugrist

Add `:restricted_characters` default options

Introducing `:restricted_characters` in Paperclip::Attachment.default_options  so people can override their blacklist characters by override that setting.
parent 604304e3
2012-01-27 Prem Sichanugrist <psichanugrist@thoughtbot.com> 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 * 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 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 escaping and unescaping later. You can see the list of blacklist characters
......
...@@ -14,6 +14,7 @@ module Paperclip ...@@ -14,6 +14,7 @@ module Paperclip
:convert_options => {}, :convert_options => {},
:default_style => :original, :default_style => :original,
:default_url => "/:attachment/:style/missing.png", :default_url => "/:attachment/:style/missing.png",
:restricted_characters => /[&$+,\/:;=?@<>\[\]\{\}\|\\\^~%# ]/,
:hash_data => ":class/:attachment/:id/:style/:updated_at", :hash_data => ":class/:attachment/:id/:style/:updated_at",
:hash_digest => "SHA1", :hash_digest => "SHA1",
:interpolator => Paperclip::Interpolations, :interpolator => Paperclip::Interpolations,
...@@ -25,10 +26,10 @@ module Paperclip ...@@ -25,10 +26,10 @@ module Paperclip
:storage => :filesystem, :storage => :filesystem,
:styles => {}, :styles => {},
:url => "/system/:attachment/:id/:style/:filename", :url => "/system/:attachment/:id/:style/:filename",
:url_generator => Paperclip::UrlGenerator :url_generator => Paperclip::UrlGenerator,
:use_default_time_zone => true, :use_default_time_zone => true,
:use_timestamp => true, :use_timestamp => true,
:whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails], :whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails]
} }
end end
...@@ -479,7 +480,9 @@ module Paperclip ...@@ -479,7 +480,9 @@ module Paperclip
end end
def cleanup_filename(filename) def cleanup_filename(filename)
filename.gsub(/[&$+,\/:;=?@<>\[\]\{\}\|\\\^~%# ]/, '_') if @options[:restricted_characters]
filename.gsub(@options[:restricted_characters], '_')
end
end end
end end
end end
...@@ -714,22 +714,45 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -714,22 +714,45 @@ class AttachmentTest < Test::Unit::TestCase
end end
context "Attachment with reserved filename" do context "Attachment with reserved filename" do
"&$+,/:;=?@<>[]{}|\^~%# ".split(//).each do |character| setup do
context "with character #{character}" do rebuild_model
setup do @file = StringIO.new(".")
rebuild_model end
file = StringIO.new(".") context "with default configuration" do
file.stubs(:original_filename).returns("file#{character}name.png") "&$+,/:;=?@<>[]{}|\^~%# ".split(//).each do |character|
@dummy = Dummy.new context "with character #{character}" do
@dummy.avatar = file setup do
end @file.stubs(:original_filename).returns("file#{character}name.png")
@dummy = Dummy.new
@dummy.avatar = @file
end
should "convert special character into underscore" do should "convert special character into underscore" do
assert_equal "file_name.png", @dummy.avatar.original_filename assert_equal "file_name.png", @dummy.avatar.original_filename
end
end end
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 end
context "Attachment with uppercase extension and a default style" do context "Attachment with uppercase extension and a default style" do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment