Class: MiniPaperclip::Validators::MediaTypeSpoofValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/mini_paperclip/validators/media_type_spoof_validator.rb

Instance Method Summary collapse

Instance Method Details

#normalize(content_type) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/mini_paperclip/validators/media_type_spoof_validator.rb', line 15

def normalize(content_type)
  case content_type
  when "image/jpg"
    "image/jpeg"
  else
    content_type
  end
end

#validate_each(record, attribute, attachment) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/mini_paperclip/validators/media_type_spoof_validator.rb', line 6

def validate_each(record, attribute, attachment)
  return unless attachment.meta_content_type

  attachment_content_type = record.read_attribute_for_validation("#{attribute}_content_type")
  unless normalize(attachment.meta_content_type) == normalize(attachment_content_type)
    record.errors.add(attribute, :spoofed_media_type)
  end
end