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>
* 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
......
......@@ -14,6 +14,7 @@ module Paperclip
: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 @@ module Paperclip
: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 @@ module Paperclip
end
def cleanup_filename(filename)
filename.gsub(/[&$+,\/:;=?@<>\[\]\{\}\|\\\^~%# ]/, '_')
if @options[:restricted_characters]
filename.gsub(@options[:restricted_characters], '_')
end
end
end
end
......@@ -714,22 +714,45 @@ class AttachmentTest < Test::Unit::TestCase
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
......
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