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'
require 'paperclip/helpers'
require 'paperclip/has_attached_file'
require 'paperclip/tasks/attachments'
require 'paperclip/filename_cleaner'
require 'mime/types'
require 'logger'
require 'cocaine'
......
......@@ -14,7 +14,7 @@ module Paperclip
:default_url => "/:attachment/:style/missing.png",
:escape_url => true,
:restricted_characters => /[&$+,\/:;=?@<>\[\]\{\}\|\\\^~%# ]/,
:filename_sanitizer => nil,
:filename_cleaner => nil,
:hash_data => ":class/:attachment/:id/:style/:updated_at",
:hash_digest => "SHA1",
:interpolator => Paperclip::Interpolations,
......@@ -99,12 +99,7 @@ module Paperclip
return nil if file.nil?
@queued_for_write[:original] = file
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(:file_name, cleanup_filename(file.original_filename))
instance_write(:content_type, file.content_type.to_s.strip)
instance_write(:file_size, file.size)
instance_write(:fingerprint, file.fingerprint) if instance_respond_to?(:fingerprint)
......@@ -366,14 +361,6 @@ module Paperclip
end
end
def cleanup_filename(filename)
if @options[:restricted_characters]
filename.gsub(@options[:restricted_characters], '_')
else
filename
end
end
private
def path_option
......@@ -494,6 +481,14 @@ module Paperclip
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
def able_to_store_created_at?
@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
end
end
context 'with specified sanitizer proc' do
context 'with specified cleaner' do
setup do
@old_defaults = Paperclip::Attachment.default_options.dup
end
......@@ -926,11 +926,10 @@ class AttachmentTest < Test::Unit::TestCase
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|
should 'call the given proc and take the result as cleaned filename' do
Paperclip::Attachment.default_options[:filename_cleaner] = lambda do |str|
"from_proc_#{str}"
end
Paperclip::Attachment.default_options[:filename_sanitizer] = proc
@file.stubs(:original_filename).returns("goood.png")
@dummy = Dummy.new
......@@ -938,16 +937,18 @@ class AttachmentTest < Test::Unit::TestCase
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}"
should 'call the given object and take the result as the cleaned filename' do
class MyCleaner
def call(filename)
"foo"
end
end
Paperclip::Attachment.default_options[:filename_sanitizer] = proc
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.avatar = @file
assert_equal "from_proc_go_ood.png", @dummy.avatar.original_filename
assert_equal "foo", @dummy.avatar.original_filename
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