A platform for community discussion. Free, open, simple.
FIX: Shrinking images where smaller image upload exists (#18965)
Jarek Radosz3e0196cbbfdb
diff --git a/app/models/post.rb b/app/models/post.rbindex 5f493c45850..362ce9bf8be 100644--- a/app/models/post.rb+++ b/app/models/post.rb@@ -989,10 +989,6 @@ class Post < ActiveRecord::Baseendend- def downloaded_images- self.custom_fields[Post::DOWNLOADED_IMAGES] || {}- end-def each_upload_url(fragments: nil, include_local_upload: true)current_db = RailsMultisite::ConnectionManagement.current_dbupload_patterns = [diff --git a/lib/shrink_uploaded_image.rb b/lib/shrink_uploaded_image.rbindex 3123468ef14..f6ef2354d47 100644--- a/lib/shrink_uploaded_image.rb+++ b/lib/shrink_uploaded_image.rb@@ -83,7 +83,7 @@ class ShrinkUploadedImageif post.raw_changed?log "Updating post"- elsif post.downloaded_images.has_value?(original_upload.id)+ elsif post.post_hotlinked_media.exists?(upload_id: original_upload.id)log "A hotlinked, unreferenced image"elsif post.raw.include?(upload.short_url)log "Already processed"@@ -161,13 +161,10 @@ class ShrinkUploadedImage)end- if existing_upload && post.downloaded_images.present?- downloaded_images = post.downloaded_images.transform_values do |upload_id|- upload_id == original_upload.id ? upload.id : upload_id- end-- post.custom_fields[Post::DOWNLOADED_IMAGES] = downloaded_images- post.save_custom_fields+ if existing_upload+ post.post_hotlinked_media+ .where(upload_id: original_upload.id)+ .update_all(upload_id: upload.id)endpost.rebake!diff --git a/spec/lib/shrink_uploaded_image_spec.rb b/spec/lib/shrink_uploaded_image_spec.rbindex 009d4a423a4..e3d6fb9ae54 100644--- a/spec/lib/shrink_uploaded_image_spec.rb+++ b/spec/lib/shrink_uploaded_image_spec.rb@@ -21,6 +21,29 @@ RSpec.describe ShrinkUploadedImage doexpect(upload.filesize).to be < filesize_beforeend+ it "updates HotlinkedMedia records when there is an upload for downsized image" do+ OptimizedImage.downsize(Discourse.store.path_for(upload), "/tmp/smaller.png", "10000@", filename: upload.original_filename)+ smaller_sha1 = Upload.generate_digest("/tmp/smaller.png")+ smaller_upload = Fabricate(:image_upload, sha1: smaller_sha1)++ post = Fabricate(:post, raw: "<img src='#{upload.url}'>")+ post.link_post_uploads+ post_hotlinked_media = PostHotlinkedMedia.create!(+ post: post,+ url: "http://example.com/images/2/2e/Longcat1.png",+ upload: upload,+ status: :downloaded,+ )++ ShrinkUploadedImage.new(+ upload: upload,+ path: Discourse.store.path_for(upload),+ max_pixels: 10_000,+ ).perform++ expect(post_hotlinked_media.reload.upload).to eq(smaller_upload)+ end+it "returns false if the image is not used by any models" doresult = ShrinkUploadedImage.new(upload: upload,