Permalink
Browse files

Sanitizing filenames fix & test coverage

  • Loading branch information...
1 parent a328a05 commit 381982356a01873a1f892379c17399b6cb3b8293 @nashbridges nashbridges committed Jan 31, 2011
Showing with 27 additions and 1 deletion.
  1. +2 −1 lib/carrierwave/sanitized_file.rb
  2. +25 −0 spec/sanitized_file_spec.rb
@@ -1,6 +1,7 @@
# encoding: utf-8
require 'pathname'
+require 'active_support/core_ext'
module CarrierWave
@@ -262,7 +263,7 @@ def sanitize(name)
name = name.gsub(sanitize_regexp,"_")
name = "_#{name}" if name =~ /\A\.+\z/
name = "unnamed" if name.size == 0
- return name.downcase
+ return name.mb_chars.downcase.to_s
end
def split_extension(filename)
@@ -160,6 +160,31 @@
end
+ describe '#some unicode filenames with an overridden sanitize_regexp' do
+
+ before do
+ @sanitized_file = CarrierWave::SanitizedFile.new(nil)
+ regexp = RUBY_VERSION >= '1.9' ? /[^[:word:]\.\-\+_]/ : /[^ЁА-ЯA-ZÀ-Ü\.\-\+_]/i
+ @sanitized_file.stub(:sanitize_regexp).and_return(regexp)
+ end
+
+ it "should default to the original filename if it is valid" do
+ @sanitized_file.should_receive(:original_filename).at_least(:once).and_return("тестовый.jpg")
+ @sanitized_file.filename.should == "тестовый.jpg"
+ end
+
+ it "should downcase characters properly" do
+ @sanitized_file.should_receive(:original_filename).at_least(:once).and_return("ТестоВый Ёжик.jpg")
+ @sanitized_file.filename.should == "тестовый_ёжик.jpg"
+ end
+
+ it "should downcase characters properly" do
+ @sanitized_file.should_receive(:original_filename).at_least(:once).and_return("CONTRÔLE.jpg")
+ @sanitized_file.filename.should == "contrôle.jpg"
+ end
+
+ end
+
shared_examples_for "all valid sanitized files" do
describe '#empty?' do

0 comments on commit 3819823

Please sign in to comment.