James Mitchell
James Mitchell

Reputation: 2467

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

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.

Upvotes: 9

Views: 7164

Answers (3)

Vedant Agarwala
Vedant Agarwala

Reputation: 18819

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

Upvotes: 9

Alex V
Alex V

Reputation: 18316

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

Upvotes: 1

James Mitchell
James Mitchell

Reputation: 2467

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

Upvotes: 8

Related Questions