From 303a535dbaa03e6026536209246cc0f90ed7ef88 Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Wed, 26 Dec 2018 17:17:08 +0200 Subject: [PATCH] PERF: automatic upload size calculation not persisted Previously if upload had missing width and height we would calculate on first use BUT we (me) forgot to save this to the database This was particularly bad on home page cause category images (when old) miss dimensions. --- app/models/upload.rb | 15 +++++++++++++-- spec/models/upload_spec.rb | 3 +++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/app/models/upload.rb b/app/models/upload.rb index a14b71b6fea..a90eb4c14ef 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -126,8 +126,19 @@ class Upload < ActiveRecord::Base end begin - self.width, self.height = size = FastImage.new(path, raise_on_failure: true).size - self.thumbnail_width, self.thumbnail_height = ImageSizer.resize(*size) + w, h = FastImage.new(path, raise_on_failure: true).size + + self.width = w || 0 + self.height = h || 0 + + self.thumbnail_width, self.thumbnail_height = ImageSizer.resize(w, h) + + self.update_columns( + width: width, + height: height, + thumbnail_width: thumbnail_width, + thumbnail_height: thumbnail_height + ) rescue => e Discourse.warn_exception(e, message: "Error getting image dimensions") end diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb index aba1b21c02d..1fe964da54b 100644 --- a/spec/models/upload_spec.rb +++ b/spec/models/upload_spec.rb @@ -55,6 +55,9 @@ describe Upload do expect(upload.width).to eq(64250) expect(upload.height).to eq(64250) + upload.reload + expect(upload.read_attribute(:width)).to eq(64250) + upload.update_columns(width: nil, height: nil, thumbnail_width: nil, thumbnail_height: nil) expect(upload.thumbnail_width).to eq(500)