lib/carrierwave/sanitized_file.rb in carrierwave-2.2.6 vs lib/carrierwave/sanitized_file.rb in carrierwave-3.0.0.beta

- old
+ new

@@ -1,8 +1,7 @@ require 'pathname' require 'active_support/core_ext/string/multibyte' -require 'mini_mime' require 'marcel' module CarrierWave ## @@ -12,10 +11,11 @@ # Strings and Pathnames. # # It's probably needlessly comprehensive and complex. Help is appreciated. # class SanitizedFile + include CarrierWave::Utilities::FileName attr_reader :file class << self attr_writer :sanitize_regexp @@ -58,33 +58,10 @@ end alias_method :identifier, :filename ## - # Returns the part of the filename before the extension. So if a file is called 'test.jpeg' - # this would return 'test' - # - # === Returns - # - # [String] the first part of the filename - # - def basename - split_extension(filename)[0] if filename - end - - ## - # Returns the file extension - # - # === Returns - # - # [String] the extension - # - def extension - split_extension(filename)[1] if filename - end - - ## # Returns the file's size. # # === Returns # # [Integer] the file's size in bytes. @@ -157,11 +134,11 @@ elsif is_path? File.open(@file, "rb") {|file| file.read} else @file.try(:rewind) @content = @file.read - @file.try(:close) unless @file.try(:closed?) + @file.try(:close) unless @file.class.ancestors.include?(::StringIO) || @file.try(:closed?) @content end end ## @@ -259,12 +236,12 @@ # [String] the content type of the file # def content_type @content_type ||= existing_content_type || - marcel_magic_content_type || - mini_mime_content_type + marcel_magic_by_mime_type || + marcel_magic_by_path end ## # Sets the content type of the file. # @@ -312,59 +289,44 @@ File.chmod(permissions, path) if permissions end # Sanitize the filename, to prevent hacking def sanitize(name) + name = name.scrub name = name.tr("\\", "/") # work-around for IE name = File.basename(name) name = name.gsub(sanitize_regexp,"_") name = "_#{name}" if name =~ /\A\.+\z/ name = "unnamed" if name.size.zero? return name.mb_chars.to_s end def existing_content_type if @file.respond_to?(:content_type) && @file.content_type - Marcel::MimeType.for(declared_type: @file.content_type.to_s.chomp) + @file.content_type.to_s.chomp end end - def marcel_magic_content_type - if path - type = File.open(path) do |file| - Marcel::Magic.by_magic(file).try(:type) - end + def marcel_magic_by_mime_type + return unless path - if type.nil? - type = Marcel::Magic.by_path(path).try(:type) - type = 'invalid/invalid' unless type.nil? || type.start_with?('text/') - end + type = File.open(path) do |file| + Marcel::Magic.by_magic(file).try(:type) + end - type + if type.nil? + type = Marcel::Magic.by_path(path).try(:type) + type = 'invalid/invalid' unless type.nil? || type.start_with?('text/') || type.start_with?('application/json') end + + type rescue Errno::ENOENT nil end - def mini_mime_content_type + def marcel_magic_by_path return unless path - mime_type = ::MiniMime.lookup_by_filename(path) - @content_type = (mime_type && mime_type.content_type).to_s - end - def split_extension(filename) - # regular expressions to try for identifying extensions - extension_matchers = [ - /\A(.+)\.(tar\.([glx]?z|bz2))\z/, # matches "something.tar.gz" - /\A(.+)\.([^\.]+)\z/ # matches "something.jpg" - ] - - extension_matchers.each do |regexp| - if filename =~ regexp - return $1, $2 - end - end - return filename, "" # In case we weren't able to split the extension + Marcel::Magic.by_path(path).to_s end - end # SanitizedFile end # CarrierWave