Sha256: af618b67edb58827dd5ff72d653159cf69c75462c3bf9b224fd99d40ecf5a77c

Contents?: true

Size: 1.02 KB

Versions: 7

Compression:

Stored size: 1.02 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 => e
          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

7 entries across 7 versions & 1 rubygems

Version Path
bizside-2.0.7 lib/bizside/uploader/content_type_validator.rb
bizside-2.0.6 lib/bizside/uploader/content_type_validator.rb
bizside-2.0.5 lib/bizside/uploader/content_type_validator.rb
bizside-2.0.4 lib/bizside/uploader/content_type_validator.rb
bizside-2.0.3 lib/bizside/uploader/content_type_validator.rb
bizside-2.0.2 lib/bizside/uploader/content_type_validator.rb
bizside-2.0.1 lib/bizside/uploader/content_type_validator.rb