FIX: Shrinking images where smaller image upload exists (#18965)

This commit is contained in:
Jarek Radosz 2022-11-10 12:43:56 +01:00 committed by GitHub
parent c0a4823203
commit 3e0196cbbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 12 deletions

View File

@ -989,10 +989,6 @@ class Post < ActiveRecord::Base
end
end
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_db
upload_patterns = [

View File

@ -83,7 +83,7 @@ class ShrinkUploadedImage
if 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)
end
post.rebake!

View File

@ -21,6 +21,29 @@ RSpec.describe ShrinkUploadedImage do
expect(upload.filesize).to be < filesize_before
end
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" do
result = ShrinkUploadedImage.new(
upload: upload,