我有一个文件上传器,用于获取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.在这个错误变成错误之前,我该如何处理它?或者至少让用户保存他们的附件,而不会对他们吐口水
2.有没有办法将doc/docx转换为缩略图(不需要调用我们的服务?)
要上传文件并在可能的情况下只创建缩略图,您可以添加带有条件处理的版本。然后创建一个方法来检查有效的内容类型。
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