Paperclip Gem - "Image has contents that are not what they are reported to be" Error
Asked Answered
L

3

9

The website's function is to post a Blog Post. It's running locally on Windows 7. I've tried on Paperclip gem (both versions 4.2.4 and 4.3) and the server goes into an infinite loop in cmd (doesn't happen on 4.2.4 but still get the error). I did bundle install and it's definitely installed.

Gemfile:

gem "paperclip", "~> 4.3"

Here's the model:

class Post < ActiveRecord::Base

    has_attached_file :image, :default_url => ":style/rails1.jpg"
    validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/

end

This is the error I get when trying to submit an image (png or jpg):

Image has contents that are not what they are reported to be

I'm new to this so detailed explanations would be appreciated. I read some other fixes on here but nothing worked.

Liege answered 23/7, 2015 at 20:22 Comment(0)
E
9

The correct way disable spoof checking is to use: validate_media_type: false in your attachment definition i.e.

has_attached_file :image, :default_url => ":style/rails1.jpg", validate_media_type: false
Emmert answered 15/2, 2017 at 12:16 Comment(0)
L
8

Figured out a temporary solution:

Add this file

config/initializers/paperclip_media_type_spoof_detector_override.rb

require 'paperclip/media_type_spoof_detector'
module Paperclip
  class MediaTypeSpoofDetector
    def spoofed?
      false
    end
  end
end
Liege answered 23/7, 2015 at 20:41 Comment(0)
B
1

Thats not the best way. But this way is a bit safer and less monkey patching...
Just add this to your model:

do_not_validate_attachment_file_type :image
Bottom answered 7/10, 2015 at 16:3 Comment(2)
hi! I am also having this same issue where do I place this line? o_not_validate_attachment_file_type :image in the model?Iddo
This doesn't work either. It seems really weird that paperclip checks the validation even though we explicitly put the code 'do not validate' there.Byerly

© 2022 - 2024 — McMap. All rights reserved.