Sha256: 8ab3cda0666542662a5773f73a6891f9cbb67ef11a9053fbab4b32ab0c59f2f6

Contents?: true

Size: 1.1 KB

Versions: 14

Compression:

Stored size: 1.1 KB

Contents

require "active_model/validations/presence"

module Paperclip
  module Validators
    class MediaTypeSpoofDetectionValidator < ActiveModel::EachValidator
      def validate_each(record, attribute, value)
        adapter = Paperclip.io_adapters.for(value)
        if Paperclip::MediaTypeSpoofDetector.using(adapter, value.original_filename, value.content_type).spoofed?
          record.errors.add(attribute, :spoofed_media_type)
        end

        adapter.tempfile.close(true) if adapter.tempfile
      end
    end

    module HelperMethods
      # Places ActiveModel validations on the presence of a file.
      # Options:
      # * +if+: A lambda or name of an instance method. Validation will only
      #   be run if this lambda or method returns true.
      # * +unless+: Same as +if+ but validates if lambda or method returns false.
      def validates_media_type_spoof_detection(*attr_names)
        options = _merge_attributes(attr_names)
        validates_with MediaTypeSpoofDetectionValidator, options.dup
        validate_before_processing MediaTypeSpoofDetectionValidator, options.dup
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
kt-paperclip-7.2.2 lib/paperclip/validators/media_type_spoof_detection_validator.rb
kt-paperclip-7.2.1 lib/paperclip/validators/media_type_spoof_detection_validator.rb
kt-paperclip-7.2.0 lib/paperclip/validators/media_type_spoof_detection_validator.rb
kt-paperclip-6.4.2 lib/paperclip/validators/media_type_spoof_detection_validator.rb
kt-paperclip-7.1.1 lib/paperclip/validators/media_type_spoof_detection_validator.rb
kt-paperclip-7.1.0 lib/paperclip/validators/media_type_spoof_detection_validator.rb
kt-paperclip-7.0.1 lib/paperclip/validators/media_type_spoof_detection_validator.rb
kt-paperclip-7.0.0 lib/paperclip/validators/media_type_spoof_detection_validator.rb
kt-paperclip-6.4.1 lib/paperclip/validators/media_type_spoof_detection_validator.rb
kt-paperclip-6.4.0 lib/paperclip/validators/media_type_spoof_detection_validator.rb
kt-paperclip-6.3.0 lib/paperclip/validators/media_type_spoof_detection_validator.rb
kt-paperclip-6.2.2 lib/paperclip/validators/media_type_spoof_detection_validator.rb
kt-paperclip-6.2.1 lib/paperclip/validators/media_type_spoof_detection_validator.rb
kt-paperclip-6.2.0 lib/paperclip/validators/media_type_spoof_detection_validator.rb