From fdfdaae07d3df59243ffec1aed66e873d8524397 Mon Sep 17 00:00:00 2001 From: natt-eyre Date: Tue, 29 Sep 2020 16:31:09 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=80=D0=B8=20=D1=81=D0=BE=D1=85=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D0=B8=20=D0=BA=D0=B0=D1=80=D1=82?= =?UTF-8?q?=D0=B8=D0=BD=D0=BA=D0=B8=20=D1=81=D0=BE=D1=85=D1=80=D0=B0=D0=BD?= =?UTF-8?q?=D1=8F=D0=B5=D0=BC=20=D0=B5=D0=B5=20=D1=88=D0=B8=D1=80=D0=B8?= =?UTF-8?q?=D0=BD=D1=83=20=D0=B8=20=D0=B2=D1=8B=D1=81=D0=BE=D1=82=D1=83=20?= =?UTF-8?q?=D0=B2=20=D0=B1=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/paperclip/attachment.rb | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/paperclip/attachment.rb b/lib/paperclip/attachment.rb index ed16b19..05ed943 100644 --- a/lib/paperclip/attachment.rb +++ b/lib/paperclip/attachment.rb @@ -114,11 +114,18 @@ module Paperclip close_uploaded_file = uploaded_file.respond_to?(:close) end - if image_content_type?(uploaded_file) && !valid_image_resolution?(uploaded_file) - errors[:base] = :too_large_resolution - @validated = true # Skip other validations to prevent unnecessary messages - return + if image_content_type?(uploaded_file) + # FastImage don`t rewind file if it was read before. + uploaded_file.rewind if uploaded_file.respond_to?(:rewind) + sizes = FastImage.size(uploaded_file) + + unless valid_image_resolution?(sizes) + errors[:base] = :too_large_resolution + @validated = true # Skip other validations to prevent unnecessary messages + return + end end + return unless valid_assignment?(uploaded_file) uploaded_file.binmode if uploaded_file.respond_to? :binmode @@ -136,6 +143,10 @@ module Paperclip instance_write(:content_type, uploaded_file.content_type.to_s.strip) instance_write(:file_size, uploaded_file.size.to_i) instance_write(:updated_at, Time.now) + if sizes + instance_write(:width, sizes[0]) + instance_write(:height, sizes[1]) + end @dirty = true @@ -154,10 +165,7 @@ module Paperclip file.respond_to?(:content_type) && file.content_type.try(:include?, 'image') end - def valid_image_resolution? file - # FastImage don`t rewind file if it readed before. - file.rewind if file.respond_to?(:rewind) - sizes = FastImage.size(file) + def valid_image_resolution?(sizes) !sizes || (sizes[0] <= MAX_IMAGE_RESOLUTION && sizes[1] <= MAX_IMAGE_RESOLUTION) end