Permalink
Browse files

Fix ability to override sanitize_regexp

  • Loading branch information...
1 parent e8657c1 commit f04a52775375d9c85ec41c50df2c803f9be4060d @trevorturk trevorturk committed May 19, 2011
Showing with 12 additions and 12 deletions.
  1. +2 −10 README.rdoc
  2. +9 −1 lib/carrierwave/sanitized_file.rb
  3. +1 −1 lib/carrierwave/uploader/cache.rb
View
@@ -137,20 +137,12 @@ all *non*-allowed symbols.
With Ruby 1.9 and higher you can simply write (as it has {Oniguruma}[http://oniguruma.rubyforge.org/oniguruma/]
built-in):
- class MyUploader < CarrierWave::Uploader::Base
- def sanitize_regexp
- /[^[:word:]\.\-\+]/
- end
- end
+ CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:]\.\-\+]/
With Ruby 1.8.* you have to manually specify all character ranges. For example, for files which may
contain Russian letters:
- class MyUploader < CarrierWave::Uploader::Base
- def sanitize_regexp
- /[^a-zA-Zа-яА-ЯёЁ0-9\.\-\+_]/u
- end
- end
+ CarrierWave::SanitizedFile.sanitize_regexp = /[^a-zA-Zа-яА-ЯёЁ0-9\.\-\+_]/u
Also make sure that allowing non-latin characters won't cause a compatibility issue with a third-party
plugins or client-side software.
@@ -16,6 +16,14 @@ module CarrierWave
class SanitizedFile
attr_accessor :file
+
+ class << self
+ attr_writer :sanitize_regexp
+
+ def sanitize_regexp
+ @sanitize_regexp ||= /[^a-zA-Z0-9\.\-\+_]/
+ end
+ end
def initialize(file)
self.file = file
@@ -230,7 +238,7 @@ def content_type
# [Regexp] the regexp for sanitizing the file name
#
def sanitize_regexp
- /[^a-zA-Z0-9\.\-\+_]/
+ CarrierWave::SanitizedFile.sanitize_regexp
end
private
@@ -151,7 +151,7 @@ def cache_id=(cache_id)
end
def original_filename=(filename)
- raise CarrierWave::InvalidParameter, "invalid filename" unless filename =~ /\A[a-z0-9\.\-\+_]+\z/i
+ raise CarrierWave::InvalidParameter, "invalid filename" if filename =~ CarrierWave::SanitizedFile.sanitize_regexp
@original_filename = filename
end

0 comments on commit f04a527

Please sign in to comment.