Sha256: 93cd5e94668fc40e52e269dd1a58f389e8978e890b0e1d7e66a5aea41057930e

Contents?: true

Size: 1.56 KB

Versions: 22

Compression:

Stored size: 1.56 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 black list 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 black list, `\A` and `\z` are automatically added to
      # the Regexp expression, also case insensitive.
      #
      # === Returns

      # [NilClass, String, Regexp, Array[String, Regexp]] a black list of extensions which are prohibited to be uploaded
      #
      # === Examples
      #
      #     def extension_blacklist
      #       %w(swf tiff)
      #     end
      #
      # Basically the same, but using a Regexp:
      #
      #     def extension_blacklist
      #       [/swf/, 'tiff']
      #     end
      #

      def extension_blacklist; end

    private

      def check_extension_blacklist!(new_file)
        extension = new_file.extension.to_s
        if extension_blacklist && blacklisted_extension?(extension)
          raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.extension_blacklist_error", extension: new_file.extension.inspect, prohibited_types: Array(extension_blacklist).join(", "))
        end
      end

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

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
carrierwave-1.3.4 lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-1.3.3 lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-2.1.1 lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-1.3.2 lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-2.1.0 lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-2.0.2 lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-2.0.1 lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-2.0.0 lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-2.0.0.rc lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-1.3.1 lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-1.3.0 lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-1.2.3 lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-1.2.2 lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-1.2.1 lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-1.2.0 lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-1.1.0 lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-1.0.0 lib/carrierwave/uploader/extension_blacklist.rb
carrierwave-1.0.0.rc lib/carrierwave/uploader/extension_blacklist.rb
card-1.19.3 vendor/carrierwave/lib/carrierwave/uploader/extension_blacklist.rb
card-1.19.2 vendor/carrierwave/lib/carrierwave/uploader/extension_blacklist.rb