【问题标题】:Weird paperclip error message奇怪的回形针错误消息
【发布时间】:2011-04-04 22:35:43
【问题描述】:

我有一个使用 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.'

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

/var/folders/cs/cs-jiL3ZH1WOkgLrcqa5Ck+++TI/-Tmp-/stream20110404-43533-vm7eza.pdf
is not recognized by the 'identify' command.

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

-- 编辑--

对于它的价值,我已经从 cmets 中提到的类似问题中介绍了 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

【问题讨论】:

标签: ruby-on-rails ruby-on-rails-3 paperclip


【解决方案1】:

我想我找到了问题所在。

尝试从您的模型中删除 :styles,您将看到 'identify' error message 正常运行,并且模型按预期进行验证。

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

/var/folders/cs/cs-jiL3ZH1WOkgLrcqa5Ck+++TI/-Tmp-/stream20110404-43533-vm7eza.pdf
is not recognized by the 'identify' command.

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

before_post_process :skip_if_invalid

def skip_if_invalid
  return false unless self.valid?
end

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

【讨论】:

  • 伟大的收获,复活这个问题的方法!
【解决方案2】:

这并不奇怪。这是回形针最常见的错误。实际上,这不是关于回形针,而是关于 ImageMagick。

  1. 你安装了ImageMagick吗?
  2. 您是否通过初始化程序添加了 image_magick command_path?

如果您已安装 IM,请立即查看其位置:

which identify
#=> it will return some path

在您的 Rails 应用程序中创建新文件config/initializers/paperclip.rb

Paperclip.options[:command_path] = "path/to/identify"

您也可以将:whiny =&gt; false 选项添加到您的has_attached_file

has_attached_file :picture, :styles => { ... }, :whiny => false

所以如果出现问题它不会抛出任何错误

如果您想将图片和文件存储在一个模型中并希望忽略非图像文件的样式,也可以阅读此处:

Paperclip process images only

【讨论】:

  • 嗯,我确实有 ImageMagick,我使用 which identify 检查了路径,并将命令路径选项添加到初始化程序并重新启动服务器,但这并没有解决错误... ,这似乎不适用于我的生产环境(Heroku),以及如何解决那里的问题?
  • 另外,在查看您发布的其他问题时,我的 gemfile 中也有 rmagick
  • Whiny false 不给出错误消息,只是静默失败,这对我不起作用——对于我的用户来说,一个时髦的错误消息比静默失败要好。
  • 如果你上传文本文件会发生什么?同样的问题?
  • 是的,每种文件类型。我在对问题的编辑中做了另一个可能的猜测......
猜你喜欢
  • 2014-02-25
  • 2012-05-29
  • 2010-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多