Mis*_*hko 2 ruby-on-rails imagemagick paperclip ruby-on-rails-3
我使用以下代码Asset从上传的文件中创建s:
def upload
uploader = User.find_by_id(params[:uploader_id])
params[:assets].each do |file|
new_asset = uploader.assets.build(:asset => file) # Here the error appears
new_asset.save
end
...
end
Run Code Online (Sandbox Code Playgroud)
我注意到,当我上传非图像文件时,例如my.xlsx,我收到以下错误:
[paperclip] identify -format %wx%h "C:/temp/stream20110628-460-3vqjnd.xlsx[0]" 2>NUL
[paperclip] An error was received while processing:
#<Paperclip::NotIdentifiedByImageMagickError: C:/temp/stream20110628-460-3vqjnd.xlsx is
not recognized by the 'identify' command.>
Run Code Online (Sandbox Code Playgroud)
(对于图像文件,一切正常:创建缩略图,没有错误.)
那是因为Paperclip试图从中创建缩略图my.xlsx吗?
什么配置只为图像文件创建缩略图?
这是一些相关的代码:
class Asset < ActiveRecord::Base
belongs_to :uploader, :class_name => "User"
has_attached_file :asset, :styles => { :thumb => "80x80#" }
end
Run Code Online (Sandbox Code Playgroud)
我用了以下很好的解决方案:
before_post_process :image?
def image?
(asset_content_type =~ SUPPORTED_IMAGES_REGEX).present?
end
Run Code Online (Sandbox Code Playgroud)
哪里:
SUPPORTED_IMAGE_FORMATS = ["image/jpeg", "image/png", "image/gif", "image/bmp"]
SUPPORTED_IMAGES_REGEX = Regexp.new('\A(' + SUPPORTED_IMAGE_FORMATS.join('|') + ')\Z')
Run Code Online (Sandbox Code Playgroud)