Commit a5427dc3 by Max Melentiev Committed by Jon Yurek

ability to define custom sanitizers for filenames

parent 8b0f6d34
...@@ -14,6 +14,7 @@ module Paperclip ...@@ -14,6 +14,7 @@ module Paperclip
:default_url => "/:attachment/:style/missing.png", :default_url => "/:attachment/:style/missing.png",
:escape_url => true, :escape_url => true,
:restricted_characters => /[&$+,\/:;=?@<>\[\]\{\}\|\\\^~%# ]/, :restricted_characters => /[&$+,\/:;=?@<>\[\]\{\}\|\\\^~%# ]/,
:filename_sanitizer => nil,
: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,
...@@ -98,7 +99,12 @@ module Paperclip ...@@ -98,7 +99,12 @@ module Paperclip
return nil if file.nil? return nil if file.nil?
@queued_for_write[:original] = file @queued_for_write[:original] = file
instance_write(:file_name, cleanup_filename(file.original_filename)) cleaned_filename = if @options[:filename_sanitizer]
@options[:filename_sanitizer].call(file.original_filename, self)
else
cleanup_filename(file.original_filename)
end
instance_write(:file_name, cleaned_filename)
instance_write(:content_type, file.content_type.to_s.strip) instance_write(:content_type, file.content_type.to_s.strip)
instance_write(:file_size, file.size) instance_write(:file_size, file.size)
instance_write(:fingerprint, file.fingerprint) if instance_respond_to?(:fingerprint) instance_write(:fingerprint, file.fingerprint) if instance_respond_to?(:fingerprint)
...@@ -360,6 +366,14 @@ module Paperclip ...@@ -360,6 +366,14 @@ module Paperclip
end end
end end
def cleanup_filename(filename)
if @options[:restricted_characters]
filename.gsub(@options[:restricted_characters], '_')
else
filename
end
end
private private
def path_option def path_option
...@@ -480,14 +494,6 @@ module Paperclip ...@@ -480,14 +494,6 @@ module Paperclip
end end
end end
def cleanup_filename(filename)
if @options[:restricted_characters]
filename.gsub(@options[:restricted_characters], '_')
else
filename
end
end
# Check if attachment database table has a created_at field # Check if attachment database table has a created_at field
def able_to_store_created_at? def able_to_store_created_at?
@instance.respond_to?("#{name}_created_at".to_sym) @instance.respond_to?("#{name}_created_at".to_sym)
......
...@@ -916,6 +916,40 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -916,6 +916,40 @@ class AttachmentTest < Test::Unit::TestCase
end end
end end
end end
context 'with specified sanitizer proc' do
setup do
@old_defaults = Paperclip::Attachment.default_options.dup
end
teardown do
Paperclip::Attachment.default_options.merge! @old_defaults
end
should 'call proc and take it result as cleaned filename' do
proc = Proc.new do |str, attachment|
"from_proc_#{str}"
end
Paperclip::Attachment.default_options[:filename_sanitizer] = proc
@file.stubs(:original_filename).returns("goood.png")
@dummy = Dummy.new
@dummy.avatar = @file
assert_equal "from_proc_goood.png", @dummy.avatar.original_filename
end
should 'be able to call cleanup_filename from proc' do
proc = Proc.new do |str, attachment|
attachment.cleanup_filename "from_proc_#{str}"
end
Paperclip::Attachment.default_options[:filename_sanitizer] = proc
@file.stubs(:original_filename).returns("go/ood.png")
@dummy = Dummy.new
@dummy.avatar = @file
assert_equal "from_proc_go_ood.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