views:

623

answers:

2

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?

A: 

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.

klew
+4  A: 

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
Toby Hede