2

I am using paperclip with a rails app and always I get the following error:

I, [2015-06-06T20:10:25.310071 #37358]  INFO -- : Command :: file -b --mime '/tmp/58e53d1324eef6265fdb97b08ed9aadf20150606-37358-ouvtzl.png'
I, [2015-06-06T20:10:25.317478 #37358]  INFO -- : [paperclip] Content Type Spoof: Filename ruby.png (application/octet-stream from Headers, [#<MIME::Type:0x000000053624c8 @friendly={"en"=>"Portable Network Graphics (PNG)"}, @system=nil, @obsolete=false, @registered=true, @use_instead=nil, @signature=false, @content_type="image/png", @raw_media_type="image", @raw_sub_type="png", @simplified="image/png", @i18n_key="image.png", @media_type="image", @sub_type="png", @docs=[], @encoding="base64", @extensions=["png"], @references=["IANA", "[Glenn_Randers-Pehrson]", "{image/png=http://www.iana.org/assignments/media-types/image/png}"], @xrefs={"person"=>["Glenn_Randers-Pehrson"], "template"=>["image/png"]}>] from Extension), content type discovered from file command: image/png. See documentation to allow this combination.
I, [2015-06-06T20:10:25.349416 #37358]  INFO -- : Command :: file -b --mime '/tmp/58e53d1324eef6265fdb97b08ed9aadf20150606-37358-1n574dp.png'
I, [2015-06-06T20:10:25.356667 #37358]  INFO -- : [paperclip] Content Type Spoof: Filename ruby.png (application/octet-stream from Headers, [#<MIME::Type:0x000000053624c8 @friendly={"en"=>"Portable Network Graphics (PNG)"}, @system=nil, @obsolete=false, @registered=true, @use_instead=nil, @signature=false, @content_type="image/png", @raw_media_type="image", @raw_sub_type="png", @simplified="image/png", @i18n_key="image.png", @media_type="image", @sub_type="png", @docs=[], @encoding="base64", @extensions=["png"], @references=["IANA", "[Glenn_Randers-Pehrson]", "{image/png=http://www.iana.org/assignments/media-types/image/png}"], @xrefs={"person"=>["Glenn_Randers-Pehrson"], "template"=>["image/png"]}>] from Extension), content type discovered from file command: image/png. See documentation to allow this combination.

Mongoid::Errors::Validations (
Problem:
  Validation of Mock failed.
Summary:
  The following errors were found: Image has contents that are not what they are reported to be
Resolution:
  Try persisting the document with valid data or remove the validations.):
  app/api/mockaccino/api.rb:23:in `block (2 levels) in <class:API>'

Curl command for test:

curl -X POST -i -F image=@/home/user/workspace/ruby.png -F name=mock http://localhost:3000/api/mock

I am traying send an image and a parameter called "name" to the server and creating a model with this attribute and image.

Model:

class Mock
  include Mongoid::Document
  include Mongoid::Paperclip

  field :name, type: String

  has_mongoid_attached_file :image

  validates_attachment_presence :image
  validates_attachment_size :image, :less_than => 1.megabytes
  validates_attachment_file_name :image, :matches => [/png\Z/, /jpe?g\Z/]
end

Controller (Grape):

desc "Create an mock."
      params do
        requires :name, type: String, desc: "Mock name"
        requires :image, :type => Rack::Multipart::UploadedFile, :desc => "Image file."
      end 
      post do
        mock = Mock.create!(
          name: params[:name],
          image: ActionDispatch::Http::UploadedFile.new(params[:image])
        )
      end 
Fran b
  • 3,016
  • 6
  • 38
  • 65

2 Answers2

2
curl -i -X POST -H "Content-Type:multipart/form-data" -F image=@\"./ruby.png\";type=image/png;filename=\"ruby.png\"" http://localhost:3000/api/mock
Fran b
  • 3,016
  • 6
  • 38
  • 65
0

I have one user who is seeing it with various browsers.. I can't duplicate it on my local development server or on production. It works for me, doesn't work for them. Haven't been able to isolate anything that is different (tried PC vs Mac, various browsers, etc.)

Pushing paperclip back from 4.3 to 4.2.1 makes the issue go away. This switches activemodel and activesupport from 3.2 back to 3.0 and it eliminates the dependency on mimemagic. My guess is that it's something subtle and complex in mimemagic.

Steve
  • 836
  • 11
  • 14