Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable postprocessing for pdf files in Paperclip?

Paperclip by default try to process every image file to generate thumbnail. But it also try to do it with pdf files, which can be really time consuming task. I tried looking on google and found one solution, but it changes Paperclip methods.

How to disable pdf postprocessing in Paperclip without changing Paperclip sources?

like image 297
klew Avatar asked Feb 17 '10 17:02

klew


2 Answers

From my current production app, similar to above, but explicitly looks for images (in this case my uploader pretty much accepts any type of file, so I process only images and ignore all others):

before_post_process :is_image?

def is_image?
  ["image/jpeg", "image/pjpeg", "image/png", "image/x-png", "image/gif"].include?(self.asset_content_type) 
end
like image 164
Toby Hede Avatar answered Nov 20 '22 11:11

Toby Hede


One solution is to use before_post_process callback:

 # Model with has_attached_file
 before_post_process :forbid_pdf  # should be placed after line with has_attached_file 

 private
 def forbid_pdf
   return false if (data_content_type =~ /application\/.*pdf/)
 end

data_content_type should be changed to corresponding field in your model.

Another solution I came up with is to create custom processor for images in which we should check file type and if it is not pdf run standard processor Paperclip::Thumbnail.

like image 42
klew Avatar answered Nov 20 '22 10:11

klew