奇怪的回形针错误消息

And*_*rew 3 ruby-on-rails paperclip ruby-on-rails-3

我有一个使用Paperclip 2.3.8的Rails 3应用程序.我在我的模型中指定了以下内容:

validates_attachment_content_type :file,
  :content_type => ['image/jpeg', 'image/png', 'image/gif',
                    'image/pjpeg', 'image/x-png'], 
  :message => 'Not a valid image file.'
Run Code Online (Sandbox Code Playgroud)

但是,当我测试虚假上传时,而不是"不是有效的图像文件".我收到这个奇怪的错误消息:

/var/folders/cs/cs-jiL3ZH1WOkgLrcqa5Ck+++TI/-Tmp-/stream20110404-43533-vm7eza.pdf
is not recognized by the 'identify' command.
Run Code Online (Sandbox Code Playgroud)

任何想法在这里出了什么问题?

- 编辑 -

对于它的价值,我已经从评论中提到的类似问题中覆盖了ImageMagick/Rmagick的步骤(谢谢fl00r!).

我发生的一件事(现在我正在追踪它是一个ImageMagick错误)是我在这个图像附件上有一个水印处理器.

所以,也许它试图做水印处理器尝试之前验证和就是错误消息是从哪里来的?

- 编辑 -

我尝试删除处理器,但没有更改错误消息...所以,不知道接下来要尝试什么.

- 编辑 -

:)按照要求,这是整个模型.

require 'paperclip_processors/watermark'

class Attachment < ActiveRecord::Base
  # RELATIONSHIPS
  belongs_to :photo
  belongs_to :user
  has_attached_file :file,
    :processors => [:watermark],
    :styles =>  {
      :full => "960",
      :half => "470",
      :third => "306",
      :fourth => "225",
      :fifth => "176x132#",
      :tile => "176x158>",
      :sixth => "145x109#",
      :eighth => "106x80#",
      :tenth => "87x65#",
      :marked => { :geometry => "470",
        :watermark_path => "#{Rails.root}/public/images/watermark.png",
        :position => 'Center' }
    },
    :storage => :s3,
    :s3_credentials => "#{Rails.root}/config/s3.yml",
    :path => "photos/:user_id/:id/:username_:id_:style.:extension"

  # VALIDATIONS
  validates_attachment_presence :file
  validates_attachment_content_type :file,
    :content_type => ['image/jpeg', 'image/png', 'image/gif',
                      'image/pjpeg', 'image/x-png'],
    :message => 'Not a valid image file.'
  validate :file_dimensions, :unless => "errors.any?"

  # CUSTOM VALIDATIONS
  def file_dimensions
    dimensions = Paperclip::Geometry.from_file(file.to_file(:original))
    self.width = dimensions.width
    self.height = dimensions.height
    if dimensions.width < 1600 && dimensions.height < 1600
      errors.add(:file,'Width or height must be at least 1600px')
    end
  end

  # MAINTENANCE METHODS
  def self.orphans
    where( :photo_id => nil )
  end
end
Run Code Online (Sandbox Code Playgroud)

deb*_*deb 5

我想我弄明白了这个问题.

尝试:styles从模型中删除,您将看到'identify' error message前进的方式,模型按预期验证.

问题是Paperclip正在处理样式,即使content_type验证失败了.它会尝试将您的pdf作为图像处理,然后您会收到错误:

/var/folders/cs/cs-jiL3ZH1WOkgLrcqa5Ck+++TI/-Tmp-/stream20110404-43533-vm7eza.pdf
is not recognized by the 'identify' command.
Run Code Online (Sandbox Code Playgroud)

解决方案是在验证失败时跳过处理,方法是将其添加到模型中:

before_post_process :skip_if_invalid

def skip_if_invalid
  return false unless self.valid?
end
Run Code Online (Sandbox Code Playgroud)

这样Paperclip就不会尝试将不是图像的文件转换为缩略图:)