回形针:发现以下错误:图像的内容不是报告的内容

时间:2015-06-06 18:20:54

标签: ruby-on-rails paperclip grape

我正在使用带有rails应用程序的paperclip,并且我总是收到以下错误:

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 -X POST -i -F image=@/home/user/workspace/ruby.png -F name=mock http://localhost:3000/api/mock

我正在追踪发送一个图像和一个名为&#34; name&#34;到服务器并使用此属性和图像创建模型。

型号:

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

控制器(葡萄):

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 

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我有一个用户在各种浏览器中看到它..我无法在我的本地开发服务器或生产中复制它。它对我有用,对他们不起作用。无法隔离任何不同的东西(尝试过PC与Mac,各种浏览器等)

将回形针从4.3推回到4.2.1会使问题消失。这将activemodel和activesupport从3.2切换回3.0,消除了对mimemagic的依赖。我的猜测是,这在mimemagic中是微妙而复杂的。

答案 2 :(得分:0)

这不是答案,但我没有足够的声誉对已接受的答案发表评论。

当我运行'没有指定URL'时,我收到卷曲错误

curl -i -X POST -H "Content-Type:multipart/form-data" -F image=@\"/app/assets/images/rails.png\";type=image/png;filename=\"rails.png\" http://localhost:3000/api/1.0/user/new

该答案中是否缺少引号或额外引用或其他内容?