gpt4 book ai didi

ruby-on-rails - 奇怪的回形针错误消息

转载 作者:行者123 更新时间:2023-12-04 06:19:10 24 4
gpt4 key购买 nike

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

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

-- 编辑--

对于它的值(value),我已经介绍了评论中提到的类似问题的 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

最佳答案

我想我找到了问题。

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

问题是 Paperclip 正在处理样式,即使 content_type 验证失败。它尝试将您的 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 就不会尝试将非图像文件转换为缩略图 :)

关于ruby-on-rails - 奇怪的回形针错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5545097/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com