Ruby on Rails - Paperclip Error

放肆的年华 提交于 2019-11-27 22:53:42
Mini John

That message is raised by a validation check for content spoofing.

For Paperclip v.4 this generates a bug https://github.com/thoughtbot/paperclip/issues/1429

While for Paperclip v.3, it seems it just throws a deprecation warning, https://github.com/thoughtbot/paperclip/issues/1423

So I'd wait for Paperclip team to solve this bug before using version 4. At the moment I'd rather keep using version 3.

gem "paperclip", "~> 3.5.3"

Or add this to an initializer to disable spoofing protection:

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

see Can't upload image using Paperclip 4.0 Rails 3

As explained recently in the comments of the issue (https://github.com/thoughtbot/paperclip/issues/1429#issuecomment-49821032), adding :

Paperclip.options[:command_path] = '/usr/bin'

to config/initializers/paperclip.rb solved the problem.

On the same issue, I found another workaround that can be applied at model level, without edit any initializer:

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

Basically, this skip media_type and content_type validations to avoid spoofed error on PaperclipModel attachment. For more details see here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!