mirror of
https://github.com/discourse/discourse.git
synced 2025-04-30 23:54:37 +08:00
FIX: Shrinking images where smaller image upload exists (#18965)
This commit is contained in:
parent
c0a4823203
commit
3e0196cbbf
@ -989,10 +989,6 @@ class Post < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def downloaded_images
|
|
||||||
self.custom_fields[Post::DOWNLOADED_IMAGES] || {}
|
|
||||||
end
|
|
||||||
|
|
||||||
def each_upload_url(fragments: nil, include_local_upload: true)
|
def each_upload_url(fragments: nil, include_local_upload: true)
|
||||||
current_db = RailsMultisite::ConnectionManagement.current_db
|
current_db = RailsMultisite::ConnectionManagement.current_db
|
||||||
upload_patterns = [
|
upload_patterns = [
|
||||||
|
@ -83,7 +83,7 @@ class ShrinkUploadedImage
|
|||||||
|
|
||||||
if post.raw_changed?
|
if post.raw_changed?
|
||||||
log "Updating post"
|
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"
|
log "A hotlinked, unreferenced image"
|
||||||
elsif post.raw.include?(upload.short_url)
|
elsif post.raw.include?(upload.short_url)
|
||||||
log "Already processed"
|
log "Already processed"
|
||||||
@ -161,13 +161,10 @@ class ShrinkUploadedImage
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
if existing_upload && post.downloaded_images.present?
|
if existing_upload
|
||||||
downloaded_images = post.downloaded_images.transform_values do |upload_id|
|
post.post_hotlinked_media
|
||||||
upload_id == original_upload.id ? upload.id : upload_id
|
.where(upload_id: original_upload.id)
|
||||||
end
|
.update_all(upload_id: upload.id)
|
||||||
|
|
||||||
post.custom_fields[Post::DOWNLOADED_IMAGES] = downloaded_images
|
|
||||||
post.save_custom_fields
|
|
||||||
end
|
end
|
||||||
|
|
||||||
post.rebake!
|
post.rebake!
|
||||||
|
@ -21,6 +21,29 @@ RSpec.describe ShrinkUploadedImage do
|
|||||||
expect(upload.filesize).to be < filesize_before
|
expect(upload.filesize).to be < filesize_before
|
||||||
end
|
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
|
it "returns false if the image is not used by any models" do
|
||||||
result = ShrinkUploadedImage.new(
|
result = ShrinkUploadedImage.new(
|
||||||
upload: upload,
|
upload: upload,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user