From d7b446c174b50bf37a8dc98c95a3b0b9621db729 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 20 Apr 2017 14:40:16 +0800 Subject: [PATCH 1/2] Install `discourse_image_optim` gem for now. --- Gemfile | 4 +++- Gemfile.lock | 14 +++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Gemfile b/Gemfile index faa614e12d..5d1deb8890 100644 --- a/Gemfile +++ b/Gemfile @@ -66,7 +66,9 @@ gem 'unf', require: false gem 'email_reply_trimmer', '0.1.6' -gem 'image_optim' +# TODO Use official image_optim gem once https://github.com/toy/image_optim/pull/149 +# is merged. +gem 'discourse_image_optim', require: 'image_optim' gem 'multi_json' gem 'mustache' gem 'nokogiri' diff --git a/Gemfile.lock b/Gemfile.lock index e7357029e2..3dda61b6d9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -79,6 +79,12 @@ GEM discourse-qunit-rails (0.0.9) railties discourse_fastimage (2.0.3) + discourse_image_optim (0.24.4) + exifr (~> 1.2, >= 1.2.2) + fspath (~> 3.0) + image_size (~> 1.5) + in_threads (~> 1.3) + progress (~> 3.0, >= 3.0.1) domain_name (0.5.25) unf (>= 0.0.5, < 1.0.0) email_reply_trimmer (0.1.6) @@ -126,12 +132,6 @@ GEM domain_name (~> 0.5) http_accept_language (2.0.5) i18n (0.8.1) - image_optim (0.24.2) - exifr (~> 1.2, >= 1.2.2) - fspath (~> 3.0) - image_size (~> 1.5) - in_threads (~> 1.3) - progress (~> 3.0, >= 3.0.1) image_size (1.5.0) in_threads (1.4.0) jmespath (1.3.1) @@ -405,6 +405,7 @@ DEPENDENCIES certified discourse-qunit-rails discourse_fastimage (= 2.0.3) + discourse_image_optim email_reply_trimmer (= 0.1.6) ember-handlebars-template (= 0.7.5) ember-rails (= 0.18.5) @@ -423,7 +424,6 @@ DEPENDENCIES hiredis htmlentities http_accept_language (~> 2.0.5) - image_optim listen logster lru_redux From 2af1b9e93cb8ab2255b812a1c9b844d6321403dc Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 20 Apr 2017 15:18:42 +0800 Subject: [PATCH 2/2] 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 4a9ad0555c..746b85dc3b 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 621bfb1f25..1ea66d12d2 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