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.
This commit is contained in:
Sam Saffron 2018-12-26 17:17:08 +02:00
parent f637286db5
commit 303a535dba
2 changed files with 16 additions and 2 deletions

View File

@ -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

View File

@ -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)