Ruby on rails 使用Carrierwave和minimagic,我可以测试文件是否可成像吗?
我有一个文件上传器,打算采取pdf,rft,txt,doc,docx 我希望尽可能创建缩略图。Ruby on rails 使用Carrierwave和minimagic,我可以测试文件是否可成像吗?,ruby-on-rails,carrierwave,minimagick,Ruby On Rails,Carrierwave,Minimagick,我有一个文件上传器,打算采取pdf,rft,txt,doc,docx 我希望尽可能创建缩略图。 txt文件和pdf的工作与此非常好 process resize_to_fill: [150, 150], convert: :jpg 运行时,doc和docx将失败 Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: MiniMagick::Invalid 关于这一点,我有两个问题。 1
txt文件和pdf的工作与此非常好
process resize_to_fill: [150, 150], convert: :jpg
运行时,doc和docx将失败
Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: MiniMagick::Invalid
关于这一点,我有两个问题。1.如何在错误变成错误之前处理此错误?或者至少让用户保存他们的附件,而不向他们吐口水。
2.是否有办法将doc/docx转换为缩略图(不需要调用ourside services?要上载文件并仅在可能时创建缩略图,您可以使用添加版本,然后创建检查有效内容类型的方法
class FileUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
include CarrierWave::MimeTypes
version :thumb, if: :imageable? do
process resize_to_fill: [150,150]
process convert: "jpg"
end
protected
def imageable?(new_file)
is_image = new_file.content_type.start_with? 'image'
is_pdf = new_file.content_type.end_with? 'pdf'
is_image || is_pdf
end
end
class FileUploader