A platform for community discussion. Free, open, simple.
FIX: key optimized images on format (#32575)
Samc8d6dd2a9215
diff --git a/app/models/optimized_image.rb b/app/models/optimized_image.rbindex 36c273fddd2..84722c8c2b0 100644--- a/app/models/optimized_image.rb+++ b/app/models/optimized_image.rb@@ -39,17 +39,18 @@ class OptimizedImage < ActiveRecord::Base# no extension so try to guess itupload.fix_image_extension if (!upload.extension)- if !upload.extension.match?(IM_DECODERS) && upload.extension != "svg"- if !opts[:raise_on_error]+ if !upload.extension.match?(IM_DECODERS)+ if opts[:raise_on_error]+ raise InvalidAccess+ else# nothing to do ... bad extension, not an imagereturn- else- raise InvalidAccessendend# prefer to look up the thumbnail without grabbing any locks- thumbnail = find_by(upload_id: upload.id, width: width, height: height)+ extension = ".#{opts[:format] || upload.extension}"+ thumbnail = find_by(upload_id: upload.id, width: width, height: height, extension: extension)# correct bad thumbnail if neededif thumbnail && (thumbnail.url.blank? || thumbnail.version != VERSION)@@ -70,9 +71,18 @@ class OptimizedImage < ActiveRecord::Baseoriginal_path = external_copy&.pathend+ if extension == ".svg" && upload.extension != "svg"+ if opts[:raise_on_error]+ raise InvalidAccess+ else+ # we can not convert any images to svg, unsupported+ return+ end+ end+lock(upload.id, width, height) do# may have been generated since we got the lock- thumbnail = find_by(upload_id: upload.id, width: width, height: height)+ thumbnail = find_by(upload_id: upload.id, width: width, height: height, extension: extension)# return the previous thumbnail if anyreturn thumbnail if thumbnail@@ -81,7 +91,6 @@ class OptimizedImage < ActiveRecord::BaseRails.logger.error("Could not find file in the store located at url: #{upload.url}")else# create a temp file with the same extension as the original- extension = ".#{opts[:format] || upload.extension}"return nil if extension.length == 1@@ -93,7 +102,8 @@ class OptimizedImage < ActiveRecord::Baseopts = opts.merge(quality: target_quality) if target_qualityopts = opts.merge(upload_id: upload.id)- if upload.extension == "svg"+ # special case, when "resizing" vectors we simply copy+ if extension == ".svg"FileUtils.cp(original_path, temp_path)resized = trueelsif opts[:crop]@@ -103,6 +113,7 @@ class OptimizedImage < ActiveRecord::Baseendif resized+ # TODO: crop vs resize should be stored in the db, quality should be storedthumbnail =OptimizedImage.create!(upload_id: upload.id,@@ -185,7 +196,7 @@ class OptimizedImage < ActiveRecord::Basepaths.each { |path| raise Discourse::InvalidAccess unless safe_path?(path) }end- IM_DECODERS = /\A(jpe?g|png|ico|gif|webp|avif)\z/i+ IM_DECODERS = /\A(jpe?g|png|ico|gif|webp|avif|svg)\z/idef self.prepend_decoder!(path, ext_path = nil, opts = nil)opts ||= {}@@ -382,7 +393,7 @@ end## Indexes#-# index_optimized_images_on_etag (etag)-# index_optimized_images_on_upload_id (upload_id)-# index_optimized_images_on_upload_id_and_width_and_height (upload_id,width,height) UNIQUE+# index_optimized_images_on_etag (etag)+# index_optimized_images_on_upload_id (upload_id)+# index_optimized_images_unique (upload_id,width,height,extension) UNIQUE#diff --git a/db/migrate/20250505050610_optimized_image_format_index.rb b/db/migrate/20250505050610_optimized_image_format_index.rbnew file mode 100644index 00000000000..79b17298e56--- /dev/null+++ b/db/migrate/20250505050610_optimized_image_format_index.rb@@ -0,0 +1,17 @@+# frozen_string_literal: true+class OptimizedImageFormatIndex < ActiveRecord::Migration[7.2]+ def up+ remove_index :optimized_images, name: "index_optimized_images_on_upload_id_and_width_and_height"+ add_index :optimized_images,+ %i[upload_id width height extension],+ name: "index_optimized_images_unique",+ unique: true+ end++ def down+ remove_index :optimized_images, name: "index_optimized_images_unique"+ add_index :optimized_images,+ %i[upload_id width height],+ name: "index_optimized_images_on_upload_id_and_width_and_height"+ end+enddiff --git a/spec/models/optimized_image_spec.rb b/spec/models/optimized_image_spec.rbindex 4a6f206bcbb..aa5ec990ec2 100644--- a/spec/models/optimized_image_spec.rb+++ b/spec/models/optimized_image_spec.rb@@ -246,6 +246,20 @@ RSpec.describe OptimizedImage doexpect(optimized_new.id).not_to eq(old_id)+ old_id = optimized_new.id+ # it is also able to get an optimized image in a different format+ optimized_new = OptimizedImage.create_for(upload, 10, 10, format: "gif")+ expect(optimized_new.id).not_to eq(old_id)+ expect(optimized_new.extension).to eq(".gif")++ old_id = optimized_new.id+ # same format, same image+ optimized_new = OptimizedImage.create_for(upload, 10, 10, format: "gif")+ expect(optimized_new.id).to eq(old_id)++ path = Shellwords.escape(Discourse.store.path_for(optimized_new))+ expect(`identify -format %m #{path}`.strip).to eq("GIF")+# cleanup (which transaction rollback may miss)optimized_new.destroyupload.destroy@@ -266,6 +280,12 @@ RSpec.describe OptimizedImage doexpect(File.read(Discourse.store.path_for(resized))).to eq(File.read(Discourse.store.path_for(upload)),)++ resized = upload.get_optimized_image(50, 50, format: "gif", raise_on_error: true)+ expect(resized.extension).to eq(".gif")+ # lets ensure we have a gif with the identify tool+ path = Shellwords.escape(Discourse.store.path_for(resized))+ expect(`identify -format %m #{path}`.strip).to eq("GIF")endcontext "when using an internal store" do