A platform for community discussion. Free, open, simple.
FIX: Fallback to upload URLs when SHA1 doesn’t match (#36299)
Loïc Guitaut4791accfa148
diff --git a/app/models/concerns/has_post_upload_references.rb b/app/models/concerns/has_post_upload_references.rbindex 86fa4d93aaa..e17f2c3e599 100644--- a/app/models/concerns/has_post_upload_references.rb+++ b/app/models/concerns/has_post_upload_references.rb@@ -6,10 +6,8 @@ module HasPostUploadReferencesdef link_post_uploads(fragments: nil)upload_ids = []- each_upload_url(fragments: fragments) do |src, _, sha1|- upload = nil- upload = Upload.find_by(sha1: sha1) if sha1.present?- upload ||= Upload.get_from_url(src)+ each_upload_url(fragments: fragments) do |url, _, sha1|+ upload = Upload.fetch_from(sha1:, url:)# Link any video thumbnailsif SiteSetting.video_thumbnails_enabled && upload.present? &&diff --git a/app/models/upload.rb b/app/models/upload.rbindex 5f3db6ef5d8..6ceca401a2a 100644--- a/app/models/upload.rb+++ b/app/models/upload.rb@@ -80,6 +80,10 @@ class Upload < ActiveRecord::Base)end+ def self.fetch_from(sha1:, url:)+ sha1.presence.try { |_| find_by(sha1:) } || get_from_url(url)+ end+def self.mark_invalid_s3_uploads_as_missingUpload.with_invalid_etag_verification_status.update_all(verification_status: Upload.verification_statuses[:s3_file_missing_confirmed],diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rbindex ad6b4117189..6f778ba6b60 100644--- a/lib/cooked_post_processor.rb+++ b/lib/cooked_post_processor.rb@@ -144,14 +144,14 @@ class CookedPostProcessorupload = nilimages = extract_images_for_post- @post.each_upload_url(fragments: images.css("[data-thumbnail]")) do |src, path, sha1|- upload = Upload.find_by(sha1: sha1)+ @post.each_upload_url(fragments: images.css("[data-thumbnail]")) do |url, path, sha1|+ upload = Upload.fetch_from(sha1:, url:)break if uploadendif upload.nil? # No specified thumbnail. Use any image:- @post.each_upload_url(fragments: images.css(":not([data-thumbnail])")) do |src, path, sha1|- upload = Upload.find_by(sha1: sha1)+ @post.each_upload_url(fragments: images.css(":not([data-thumbnail])")) do |url, path, sha1|+ upload = Upload.fetch_from(sha1:, url:)break if uploadendenddiff --git a/spec/lib/cooked_post_processor_spec.rb b/spec/lib/cooked_post_processor_spec.rbindex 1d384aad81a..6777b7a45c5 100644--- a/spec/lib/cooked_post_processor_spec.rb+++ b/spec/lib/cooked_post_processor_spec.rb@@ -886,6 +886,30 @@ RSpec.describe CookedPostProcessor doreply.reloadexpect(reply.image_upload_id).to be_presentend++ context "when upload filename doesn't match SHA1" do+ let(:upload_with_secure_url) do+ Fabricate(+ :upload,+ sha1: "a" * 40,+ url: "/uploads/default/original/3X/b/c/#{"d" * 40}.png",+ )+ end+ let(:post_with_secure_upload) do+ Fabricate(+ :post,+ user: user_with_auto_groups,+ raw: "<img src='#{upload_with_secure_url.url}'>",+ )+ end+ let(:cpp) { CookedPostProcessor.new(post_with_secure_upload) }++ it "sets image_upload via URL fallback" do+ expect { cpp.post_process }.to change {+ post_with_secure_upload.reload.image_upload+ }.to eq(upload_with_secure_url)+ end+ endendendenddiff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rbindex edcad1d9b3d..c14d87ace2f 100644--- a/spec/models/upload_spec.rb+++ b/spec/models/upload_spec.rb@@ -18,6 +18,40 @@ RSpec.describe Upload doit { is_expected.to have_many(:badges).dependent(:nullify) }+ describe ".fetch_from" do+ subject(:record) { described_class.fetch_from(sha1:, url:) }++ fab!(:upload)++ let(:url) { upload.url }++ context "when sha1 is present" do+ context "when there is a matching upload for this SHA1" do+ let(:sha1) { upload.sha1 }++ it "returns the record" do+ expect(record).to eq(upload)+ end+ end++ context "when there is no matching upload for this SHA1" do+ let(:sha1) { "non-existent" }++ it "fetches the record using the provided URL" do+ expect(record).to eq(upload)+ end+ end+ end++ context "when sha1 is blank" do+ let(:sha1) { "" }++ it "fetches the record using the provided URL" do+ expect(record).to eq(upload)+ end+ end+ end+describe ".with_no_non_post_relations" doit "does not find non-post related uploads" dopost_upload = Fabricate(:upload)