2014-11-12 7 views
0

pdf, rft, txt, doc, docx를 가져 오는 파일 업 로더가 있습니다.Carrierwave 및 minimagic을 사용하면 파일을 이미지 가능하게 테스트 할 수 있습니까?

할 수있을 때 축소판을 만들고 싶습니다.
TXT 파일 및 PDF의이

process resize_to_fill: [150, 150], convert: :jpg 

문서와 함께 잘 작동하고 실행이

Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: MiniMagick::Invalid 

이것에 대해 두 가지 질문이있을 때 DOCX 년대에 실패합니다.
1. 오류가 발생하기 전에이 오류를 어떻게 처리 할 수 ​​있습니까? 또는 적어도 사용자가 첨부물을 다시 저장할 수 있도록합니다.
2. 썸네일에 문서/DOCX로 변환하는 방법은 (하지 ourside 서비스에 대한 호출로?)

답변

0

그런 만들려면 conditional processing.과 버전을 추가 할 수 있습니다 가능하면 파일을 업로드 만 썸네일을 만들 수 있습니까 유효한 내용 유형을 확인하는 메소드.

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