Ruby on Rails - Paperclip 错误

16

参考了这篇教程:https://devcenter.heroku.com/articles/paperclip-s3。但是我现在正在本地测试,所以我安装了Figaro gem并将我的S3信息放在application.yml中。

使用Rails v4、Cocaine v0.5.3和Paperclip v4.1.0(不确定是否需要提及其他gem版本号)。

我有一个名为SubmissionDetails的模型,在它的new.html.erb中,我试图上传一个jpg到名为attachment的列中。以下是相关的模型代码:

has_attached_file :attachment, styles: {
thumb: '200x200>',
large: '800x800>'
}

validates_attachment_content_type :attachment, content_type: /\Aimage\/.*\Z/

当我尝试上传JPG文件时,它会返回到表单,并显示以下错误消息:
1 error prohibited this submission_detail from being saved:
Attachment translation missing:
en.activerecord.errors.models.submission_detail.attributes.attachment.spoofed_media_type

我已经理解了一部分错误,即我的en.yml文件中缺少显示此错误消息的文本,但关于伪造的媒体类型部分呢?

这出现在我的服务器控制台中,不确定是否相关:

[paperclip] Content Type Spoof: Filename header.jpg (["image/jpeg"]), content type discovered from file command: . See documentation to allow this combination.
(0.0ms)  rollback transaction

显然,PaperClip认为这不是一个有效的jpeg文件,无法使用file命令确定真实的文件类型。你尝试过不同的文件类型(png,gif)了吗? - pdu
是的,我刚刚尝试了一个png文件 - 相同的错误。 - Rachel9494
3个回答

45

这是由于内容欺骗验证检查而引发的消息。

对于Paperclip v.4,这将生成一个错误 https://github.com/thoughtbot/paperclip/issues/1429

而对于Paperclip v.3,似乎只会抛出一个弃用警告,https://github.com/thoughtbot/paperclip/issues/1423

因此,在使用版本4之前,我建议等待Paperclip团队解决此问题。目前我宁愿继续使用版本3。

gem "paperclip", "~> 3.5.3"

或者将以下内容添加到初始化程序中以禁用仿冒保护:

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

请参见无法使用Paperclip 4.0 Rails 3上传图像


这个 bug 真是个大麻烦!感谢你提供的解决方法。 - RickyD
1
目前在 4.1.1 版本中仍存在该问题。使用“spoofed?”覆盖可以暂时解决它。 - Joshua Pinter
2
仍然存在于4.2.0版本中的问题。感谢修复“伪造”的问题。 - pabo

8

1
我认为被接受的答案不是处理这个问题的更好方式。一年后,使用paperclip v4.3,通过设置command_path解决了这个问题。 - gcstr
你确定已经检查过系统中“identify”命令的安装位置了吗?($ whereis identify) - ZeDalaye

0
在同一问题上,我发现另一个可以应用于模型级别的解决方法,而不需要编辑任何初始化器:
class PaperclipModel < ActiveRecord::Base
  has_attached_file :attachment, { validate_media_type: false }

  validates_attachment :attachment, {
    # tweak as desired
    content_type: { content_type: ["text/csv", "text/plain", Paperclip::ContentTypeDetector::SENSIBLE_DEFAULT] }
  }
end

基本上,这个跳过了 media_typecontent_type 的验证,以避免在 PaperclipModel 附件上出现伪造错误。更多细节请查看 这里


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接