From 2af1b9e93cb8ab2255b812a1c9b844d6321403dc Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 20 Apr 2017 15:18:42 +0800 Subject: [PATCH] Add time out when optimizing images. --- .image_optim.yml | 3 ++- app/models/upload.rb | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/.image_optim.yml b/.image_optim.yml index 4a9ad0555cc..746b85dc3b4 100644 --- a/.image_optim.yml +++ b/.image_optim.yml @@ -2,10 +2,11 @@ skip_missing_workers: true allow_lossy: false # PNG advpng: false -optipng: +optipng: level: 2 pngcrush: false pngout: false pngquant: false # JPG jpegrecompress: false +timeout: 15 diff --git a/app/models/upload.rb b/app/models/upload.rb index 621bfb1f25b..1ea66d12d2c 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -155,7 +155,12 @@ class Upload < ActiveRecord::Base # optimize image (except GIFs, SVGs and large PNGs) if should_optimize?(file.path, [w, h]) - ImageOptim.new.optimize_image!(file.path) rescue nil + begin + ImageOptim.new.optimize_image!(file.path) + rescue ImageOptim::Worker::TimeoutExceeded + # Don't optimize if it takes too long + Rails.logger.warn("ImageOptim timed out while optimizing #{filename}") + end # update the file size filesize = File.size(file.path) end