Sha256: 9e3bf53c84695b62926d75cc0aefda66d3c5fb24a574a977da6a407ddaa18c23

Contents?: true

Size: 1.01 KB

Versions: 33

Compression:

Stored size: 1.01 KB

Contents

require 'mimemagic'

module Bizside
  module Uploader
    module ContentTypeValidator
      extend ActiveSupport::Concern
      
      included do
        begin
          require 'carrierwave-magic'
          include CarrierWave::Magic
          process :set_magic_content_type => true
        rescue
          raise '[Bizside.gem ERROR] you need to add carrierwave-magic.gem.'
        end
        before :cache, :validate_content_type!
      end

      def content_type_checklist
        %w(jpg jpeg gif png)
      end

      private

      def validate_content_type!(new_file)
        return if new_file.path.nil?
        extension = new_file.extension.to_s

        if content_type_checklist.include?(extension.downcase)
          by_path = MimeMagic.by_extension(extension).to_s
          unless new_file.content_type == by_path
            raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.content_type_whitelist_error", content_type: new_file.content_type)
          end
        end
      end

    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
bizside-2.3.9 lib/bizside/uploader/content_type_validator.rb
bizside-3.0.3 lib/bizside/uploader/content_type_validator.rb
bizside-2.3.8 lib/bizside/uploader/content_type_validator.rb
bizside-3.0.2 lib/bizside/uploader/content_type_validator.rb
bizside-3.0.1 lib/bizside/uploader/content_type_validator.rb
bizside-3.0.0 lib/bizside/uploader/content_type_validator.rb
bizside-2.3.7 lib/bizside/uploader/content_type_validator.rb
bizside-2.3.6 lib/bizside/uploader/content_type_validator.rb
bizside-2.3.5 lib/bizside/uploader/content_type_validator.rb
bizside-2.3.4 lib/bizside/uploader/content_type_validator.rb
bizside-2.3.3 lib/bizside/uploader/content_type_validator.rb
bizside-2.3.2 lib/bizside/uploader/content_type_validator.rb
bizside-2.3.1 lib/bizside/uploader/content_type_validator.rb
bizside-2.3.0 lib/bizside/uploader/content_type_validator.rb
bizside-2.2.3 lib/bizside/uploader/content_type_validator.rb
bizside-2.2.2 lib/bizside/uploader/content_type_validator.rb
bizside-2.2.1 lib/bizside/uploader/content_type_validator.rb
bizside-2.2.0 lib/bizside/uploader/content_type_validator.rb
bizside-2.1.12 lib/bizside/uploader/content_type_validator.rb
bizside-2.1.11 lib/bizside/uploader/content_type_validator.rb