From cdb404296169c6679825f6d6032d5ad2ca7eb8bd Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 5 Mar 2019 03:52:54 +0100 Subject: [PATCH] Fixes issue ##2504 - Login into Zammad not possible, application server `puma` will raise 100% or more cpu. --- app/models/store.rb | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/app/models/store.rb b/app/models/store.rb index 52ed5292e..d6b7ca6fe 100644 --- a/app/models/store.rb +++ b/app/models/store.rb @@ -281,6 +281,10 @@ returns file.provider end + def self.semaphore + @semaphore ||= Mutex.new + end + private def image_resize(content, width) @@ -295,19 +299,25 @@ returns temp_file.binmode temp_file.write(content) temp_file.close - image = Rszr::Image.load(temp_file.path) - # do not resize image if image is smaller or already same size - return if image.width <= width + temp_file_resize = nil + self.class.semaphore.synchronize do + image = Rszr::Image.load(temp_file.path) - # do not resize image if new height is smaller then 7px (images - # with small height are usally usefull to resize) - ratio = image.width / width - return if image.height / ratio <= 6 + # do not resize image if image is smaller or already same size + return if image.width <= width + + # do not resize image if new height is smaller then 7px (images + # with small height are usally usefull to resize) + ratio = image.width / width + return if image.height / ratio <= 6 + + image.resize!(width, :auto) + temp_file_resize = ::Tempfile.new.path + image.save(temp_file_resize) + image.send(:handle).finalize! + end - image.resize!(width, :auto) - temp_file_resize = ::Tempfile.new.path - image.save(temp_file_resize) image_resized = ::File.binread(temp_file_resize) Cache.write(cache_key, image_resized, { expires_in: 6.months })