FEATURE: store thumbnail algorithm version in optimized image table

Previously we had no idea what algorithm generated thumbnails, this starts tracking the version.

We also bumped up the version to force all optimized images to be generated. This is important cause we recently introduced pngquant which results in much smaller images.
This commit is contained in:
Sam 2019-01-03 17:07:30 +11:00
parent bea7a8a4d1
commit 570877da3c
3 changed files with 35 additions and 3 deletions

View File

@ -7,7 +7,7 @@ class OptimizedImage < ActiveRecord::Base
belongs_to :upload
# BUMP UP if optimized image algorithm changes
VERSION = 1
VERSION = 2
def self.lock(upload_id, width, height)
@hostname ||= `hostname`.strip rescue "unknown"
@ -43,7 +43,7 @@ class OptimizedImage < ActiveRecord::Base
thumbnail = find_by(upload_id: upload.id, width: width, height: height)
# correct bad thumbnail if needed
if thumbnail && thumbnail.url.blank?
if thumbnail && (thumbnail.url.blank? || thumbnail.version != VERSION)
thumbnail.destroy!
thumbnail = nil
end
@ -94,7 +94,8 @@ class OptimizedImage < ActiveRecord::Base
width: width,
height: height,
url: "",
filesize: File.size(temp_path)
filesize: File.size(temp_path),
version: VERSION
)
# store the optimized image and update its url

View File

@ -0,0 +1,5 @@
class AddVersionToOptimizedImages < ActiveRecord::Migration[5.2]
def change
add_column :optimized_images, :version, :integer
end
end

View File

@ -201,6 +201,32 @@ describe OptimizedImage do
describe ".create_for" do
context "versioning" do
let(:filename) { 'logo.png' }
let(:file) { file_from_fixtures(filename) }
it "is able to update optimized images on version change" do
upload = UploadCreator.new(file, filename).create_for(Discourse.system_user.id)
optimized = OptimizedImage.create_for(upload, 10, 10)
expect(optimized.version).to eq(OptimizedImage::VERSION)
optimized_again = OptimizedImage.create_for(upload, 10, 10)
expect(optimized_again.id).to eq(optimized.id)
optimized.update_columns(version: nil)
old_id = optimized.id
optimized_new = OptimizedImage.create_for(upload, 10, 10)
expect(optimized_new.id).not_to eq(old_id)
# cleanup (which transaction rollback may miss)
optimized_new.destroy
upload.destroy
end
end
it "is able to 'optimize' an svg" do
# we don't really optimize anything, we simply copy
# but at least this confirms this actually works