Sha256: 434ce54460fdd7c16ea1deb059d4ee4331a90a8fabcbf31bb5dbbfa1ae6dda9e

Contents?: true

Size: 1.41 KB

Versions: 12

Compression:

Stored size: 1.41 KB

Contents

module CarrierWave
  module Uploader
    module ContentTypeWhitelist
      extend ActiveSupport::Concern

      included do
        before :cache, :check_content_type_whitelist!
      end

      ##
      # Override this method in your uploader to provide a whitelist of files content types
      # which are allowed to be uploaded.
      # Not only strings but Regexp are allowed as well.
      #
      # === Returns
      #
      # [NilClass, String, Regexp, Array[String, Regexp]] a whitelist of content types which are allowed to be uploaded
      #
      # === Examples
      #
      #     def content_type_whitelist
      #       %w(text/json application/json)
      #     end
      #
      # Basically the same, but using a Regexp:
      #
      #     def content_type_whitelist
      #       [/(text|application)\/json/]
      #     end
      #
      def content_type_whitelist; end

    private

      def check_content_type_whitelist!(new_file)
        content_type = new_file.content_type
        if content_type_whitelist && !whitelisted_content_type?(content_type)
          raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.content_type_whitelist_error", content_type: content_type)
        end
      end

      def whitelisted_content_type?(content_type)
        Array(content_type_whitelist).any? { |item| content_type =~ /#{item}/ }
      end

    end # ContentTypeWhitelist
  end # Uploader
end # CarrierWave

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
carrierwave-1.2.3 lib/carrierwave/uploader/content_type_whitelist.rb
carrierwave-1.2.2 lib/carrierwave/uploader/content_type_whitelist.rb
carrierwave-1.2.1 lib/carrierwave/uploader/content_type_whitelist.rb
carrierwave-1.2.0 lib/carrierwave/uploader/content_type_whitelist.rb
carrierwave-1.1.0 lib/carrierwave/uploader/content_type_whitelist.rb
carrierwave-1.0.0 lib/carrierwave/uploader/content_type_whitelist.rb
carrierwave-1.0.0.rc lib/carrierwave/uploader/content_type_whitelist.rb
card-1.19.3 vendor/carrierwave/lib/carrierwave/uploader/content_type_whitelist.rb
card-1.19.2 vendor/carrierwave/lib/carrierwave/uploader/content_type_whitelist.rb
carrierwave-1.0.0.beta lib/carrierwave/uploader/content_type_whitelist.rb
card-1.19.1 vendor/carrierwave/lib/carrierwave/uploader/content_type_whitelist.rb
carrierwave-0.11.2 lib/carrierwave/uploader/content_type_whitelist.rb