奇怪的回形针错误消息 [英] Weird paperclip error message

查看:55
本文介绍了奇怪的回形针错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Rails 3 app using Paperclip 2.3.8. I have the following specified in my model:

validates_attachment_content_type :file,
  :content_type => ['image/jpeg', 'image/png', 'image/gif',
                    'image/pjpeg', 'image/x-png'], 
  :message => 'Not a valid image file.'

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

But, when I test a bogus upload, instead of "Not a valid image file." I get this weird error message:

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

知道这里出了什么问题吗??

Any ideas what's going wrong here??

-- 编辑 --

我已经从评论中提到的类似问题中介绍了 ImageMagick/Rmagick 步骤(感谢 fl00r!).

For what it's worth I have already covered the ImageMagick/Rmagick steps from the similar question mentioned in the comments (Thanks fl00r!).

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

One thing that occurs to me (now that I'm on the track of it being an ImageMagick error) is that I have a watermark processor on this image attachment.

那么,也许它在尝试验证之前尝试执行水印处理器,是错误消息的来源?

So, maybe it's trying to do the watermark processor before it tries to validate and that is where the error message is coming from?

-- 编辑 --

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

I tried removing the processor and that didn't change the error message... so, not sure what to try next.

-- 编辑 --

:) 这是要求的整个模型.

:) Here's the whole model, as requested.

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

推荐答案

我想我找到了问题所在.

I think I figured out the problem.

尝试从您的模型中删除 :styles,您将看到 'identify' 错误消息 正常运行,并且模型按预期进行验证.

Try removing the :styles from your model and you will see that the 'identify' error message goes way and the model validates as expected.

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

The problem is Paperclip is processing the styles even though the content_type validation has failed. It tries to process your pdf as an image and then you get the error:

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

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

The solution is to skip the processing if the validation fails, by adding this to your model:

before_post_process :skip_if_invalid

def skip_if_invalid
  return false unless self.valid?
end

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

This way Paperclip won't try to turn files that are not images into thumbnails :)

这篇关于奇怪的回形针错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆