mirror of
https://github.com/discourse/discourse.git
synced 2025-03-21 00:49:24 +08:00
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:
parent
bea7a8a4d1
commit
570877da3c
@ -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
|
||||
|
@ -0,0 +1,5 @@
|
||||
class AddVersionToOptimizedImages < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
add_column :optimized_images, :version, :integer
|
||||
end
|
||||
end
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user