Sha256: 7de20463c0000c7ecf3c3dc4264f0463fcad752afecca47e8b389e781fc9fd3b

Contents?: true

Size: 1.98 KB

Versions: 8

Compression:

Stored size: 1.98 KB

Contents

module CarrierWave
  module Uploader
    module ExtensionBlacklist
      extend ActiveSupport::Concern

      included do
        before :cache, :check_extension_blacklist!
      end

      ##
      # Override this method in your uploader to provide a denylist of extensions which
      # are prohibited to be uploaded. Compares the file's extension case insensitive.
      # Furthermore, not only strings but Regexp are allowed as well.
      #
      # When using a Regexp in the denylist, `\A` and `\z` are automatically added to
      # the Regexp expression, also case insensitive.
      #
      # === Returns

      # [NilClass, String, Regexp, Array[String, Regexp]] a deny list of extensions which are prohibited to be uploaded
      #
      # === Examples
      #
      #     def extension_denylist
      #       %w(swf tiff)
      #     end
      #
      # Basically the same, but using a Regexp:
      #
      #     def extension_denylist
      #       [/swf/, 'tiff']
      #     end
      #
      def extension_denylist
        if respond_to?(:extension_blacklist)
          ActiveSupport::Deprecation.warn "#extension_blacklist is deprecated, use #extension_denylist instead." unless instance_variable_defined?(:@extension_blacklist_warned)
          @extension_blacklist_warned = true
          extension_blacklist
        end
      end

    private

      def check_extension_blacklist!(new_file)
        return unless extension_denylist

        extension = new_file.extension.to_s
        if blacklisted_extension?(extension)
          raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.extension_blacklist_error", extension: new_file.extension.inspect,
                                                            prohibited_types: Array(extension_denylist).join(", "), default: :"errors.messages.extension_denylist_error")
        end
      end

      def blacklisted_extension?(extension)
        Array(extension_denylist).any? { |item| extension =~ /\A#{item}\z/i }
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/carrierwave-2.2.6/lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-2.2.6 lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-2.2.5 lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-2.2.4 lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-2.2.3 lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-2.2.2 lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-2.2.1 lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-2.2.0 lib/carrierwave/uploader/extension_blacklist.rb