Commit 7fee0b1a by Jon Yurek

Adds a FilenameCleaner class

You can either specifiy :restricted_characters or you can define your
own :filename_cleaner object. This object needs to respond to #call and
takes the filename that will be cleaned. It should return the cleaned
filenme.
parent a5427dc3
...@@ -52,6 +52,7 @@ require 'paperclip/logger' ...@@ -52,6 +52,7 @@ require 'paperclip/logger'
require 'paperclip/helpers' require 'paperclip/helpers'
require 'paperclip/has_attached_file' require 'paperclip/has_attached_file'
require 'paperclip/tasks/attachments' require 'paperclip/tasks/attachments'
require 'paperclip/filename_cleaner'
require 'mime/types' require 'mime/types'
require 'logger' require 'logger'
require 'cocaine' require 'cocaine'
......
...@@ -14,7 +14,7 @@ module Paperclip ...@@ -14,7 +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, :filename_cleaner => 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,
...@@ -99,12 +99,7 @@ module Paperclip ...@@ -99,12 +99,7 @@ module Paperclip
return nil if file.nil? return nil if file.nil?
@queued_for_write[:original] = file @queued_for_write[:original] = file
cleaned_filename = if @options[:filename_sanitizer] instance_write(:file_name, cleanup_filename(file.original_filename))
@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)
...@@ -366,14 +361,6 @@ module Paperclip ...@@ -366,14 +361,6 @@ 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
...@@ -494,6 +481,14 @@ module Paperclip ...@@ -494,6 +481,14 @@ module Paperclip
end end
end end
def filename_cleaner
@options[:filename_cleaner] || FilenameCleaner.new(@options[:restricted_characters])
end
def cleanup_filename(filename)
filename_cleaner.call(filename)
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)
......
# encoding: utf-8
module Paperclip
class FilenameCleaner
def initialize(invalid_character_regex)
@invalid_character_regex = invalid_character_regex
end
def call(filename)
if @invalid_character_regex
filename.gsub(@invalid_character_regex, "_")
else
filename
end
end
end
end
...@@ -917,7 +917,7 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -917,7 +917,7 @@ class AttachmentTest < Test::Unit::TestCase
end end
end end
context 'with specified sanitizer proc' do context 'with specified cleaner' do
setup do setup do
@old_defaults = Paperclip::Attachment.default_options.dup @old_defaults = Paperclip::Attachment.default_options.dup
end end
...@@ -926,11 +926,10 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -926,11 +926,10 @@ class AttachmentTest < Test::Unit::TestCase
Paperclip::Attachment.default_options.merge! @old_defaults Paperclip::Attachment.default_options.merge! @old_defaults
end end
should 'call proc and take it result as cleaned filename' do should 'call the given proc and take the result as cleaned filename' do
proc = Proc.new do |str, attachment| Paperclip::Attachment.default_options[:filename_cleaner] = lambda do |str|
"from_proc_#{str}" "from_proc_#{str}"
end end
Paperclip::Attachment.default_options[:filename_sanitizer] = proc
@file.stubs(:original_filename).returns("goood.png") @file.stubs(:original_filename).returns("goood.png")
@dummy = Dummy.new @dummy = Dummy.new
...@@ -938,16 +937,18 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -938,16 +937,18 @@ class AttachmentTest < Test::Unit::TestCase
assert_equal "from_proc_goood.png", @dummy.avatar.original_filename assert_equal "from_proc_goood.png", @dummy.avatar.original_filename
end end
should 'be able to call cleanup_filename from proc' do should 'call the given object and take the result as the cleaned filename' do
proc = Proc.new do |str, attachment| class MyCleaner
attachment.cleanup_filename "from_proc_#{str}" def call(filename)
"foo"
end end
Paperclip::Attachment.default_options[:filename_sanitizer] = proc end
Paperclip::Attachment.default_options[:filename_cleaner] = MyCleaner.new
@file.stubs(:original_filename).returns("go/ood.png") @file.stubs(:original_filename).returns("goood.png")
@dummy = Dummy.new @dummy = Dummy.new
@dummy.avatar = @file @dummy.avatar = @file
assert_equal "from_proc_go_ood.png", @dummy.avatar.original_filename assert_equal "foo", @dummy.avatar.original_filename
end end
end end
end end
......
# encoding: utf-8
require './test/helper'
class FilenameCleanerTest < Test::Unit::TestCase
should 'convert invalid characters to underscores' do
cleaner = Paperclip::FilenameCleaner.new(/[aeiou]/)
assert_equal "b_s_b_ll", cleaner.call("baseball")
end
should 'not convert anything if the character regex is nil' do
cleaner = Paperclip::FilenameCleaner.new(nil)
assert_equal "baseball", cleaner.call("baseball")
end
end
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