开发者

How to disable postprocessing for pdf files in Paperclip?

开发者 https://www.devze.com 2022-12-20 22:48 出处:网络
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 so

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 metho开发者_JAVA技巧ds.

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


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


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号