How to disable postprocessing for pdf files in Paperclip?
Asked Answered
P

3

8

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?

Pomace answered 17/2, 2010 at 17:50 Comment(0)
M
16

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
Mezzo answered 17/2, 2010 at 22:36 Comment(0)
P
2

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.

Pomace answered 17/2, 2010 at 22:26 Comment(0)
C
0

You can solve it with one single line:

before_post_process { avatar_content_type.match? %r{\Aimage\/.*\z} }

Don't forget to replace avatar with your attribute (eg.: receipt_content_type).

Contractile answered 24/4, 2017 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.