A platform for community discussion. Free, open, simple.
PERF: stop downloading images from post processor and lean on uploads
Sam755ca0fcbbf9
diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rbindex f3b6e712d76..4d8df118e6a 100644--- a/lib/cooked_post_processor.rb+++ b/lib/cooked_post_processor.rb@@ -153,14 +153,21 @@ class CookedPostProcessorsrc = img["src"]return if src.blank? || is_a_hyperlink?(img) || is_svg?(img)- original_width, original_height = (get_size(src) || [0, 0]).map(&:to_i)- if original_width == 0 || original_height == 0- Rails.logger.info "Can't reach '#{src}' to get its dimension."- return- end-upload = Upload.get_from_url(src)+ original_width, original_height = nil++ if (upload.present?)+ original_width = upload.width || 0+ original_height = upload.height || 0+ else+ original_width, original_height = (get_size(src) || [0, 0]).map(&:to_i)+ if original_width == 0 || original_height == 0+ Rails.logger.info "Can't reach '#{src}' to get its dimension."+ return+ end+ end+if (upload.present? && upload.animated?) || src.match?(GIF_SOURCES_REGEXP)img.add_class("animated")enddiff --git a/lib/cooked_processor_mixin.rb b/lib/cooked_processor_mixin.rbindex e22777ba73b..cf4885788d5 100644--- a/lib/cooked_processor_mixin.rb+++ b/lib/cooked_processor_mixin.rb@@ -45,10 +45,7 @@ module CookedProcessorMixinoneboxed_images.each do |img|next if img["src"].blank?- src = img["src"].sub(/^https?:/i, "")parent = img.parent- img_classes = (img["class"] || "").split(" ")- link_classes = ((parent&.name == "a" && parent["class"]) || "").split(" ")if respond_to?(:process_hotlinked_image, true)still_an_image = process_hotlinked_image(img)@@ -183,7 +180,13 @@ module CookedProcessorMixinreturn unless is_valid_image_url?(absolute_url)- @size_cache[url] = FastImage.size(absolute_url)+ upload = Upload.get_from_url(absolute_url)+ if upload && upload.width && upload.width > 0+ @size_cache[url] = [upload.width, upload.height]+ else+ @size_cache[url] = FastImage.size(absolute_url)+ end+rescue Zlib::BufError, URI::Error, OpenSSL::SSL::SSLError# FastImage.size raises BufError for some gifs, leave it.enddiff --git a/spec/fabricators/upload_fabricator.rb b/spec/fabricators/upload_fabricator.rbindex 5ccd0b96118..cb47f293b09 100644--- a/spec/fabricators/upload_fabricator.rb+++ b/spec/fabricators/upload_fabricator.rb@@ -21,6 +21,16 @@ Fabricator(:upload) doextension "png"end+Fabricator(:large_image_upload, from: :upload) do+ width 2000+ height 2000+ after_create do |upload, _transients|+ file = file_from_fixtures("2000x2000.png")+ upload.url = Discourse.store.store_upload(file, upload)+ upload.sha1 = Upload.generate_digest(file)+ end+end+Fabricator(:image_upload, from: :upload) dotransient color: "white"diff --git a/spec/fixtures/images/2000x2000.png b/spec/fixtures/images/2000x2000.pngnew file mode 100644index 00000000000..b0085b18c70Binary files /dev/null and b/spec/fixtures/images/2000x2000.png differdiff --git a/spec/jobs/pull_hotlinked_images_spec.rb b/spec/jobs/pull_hotlinked_images_spec.rbindex 8e1709ef86e..b16915ae428 100644--- a/spec/jobs/pull_hotlinked_images_spec.rb+++ b/spec/jobs/pull_hotlinked_images_spec.rb@@ -271,7 +271,6 @@ RSpec.describe Jobs::PullHotlinkedImages dopost = Fabricate(:post, raw: "<img src='#{url}'>")upload.update(access_control_post: Fabricate(:post))FileStore::S3Store.any_instance.stubs(:store_upload).returns(upload.url)- FastImage.expects(:size).returns([100, 100]).at_least_onceexpect { Jobs::PullHotlinkedImages.new.execute(post_id: post.id) }.to change { Upload.count }.by(1)@@ -374,7 +373,7 @@ RSpec.describe Jobs::PullHotlinkedImages dopost.rebake!post.reload- expect(post.cooked).to match(/<img src=.*\/uploads/)+ expect(post.cooked).to match(/<img src=.*\/uploads.*\ class="thumbnail/)expect(post.upload_references.count).to eq(1)end@@ -418,7 +417,7 @@ RSpec.describe Jobs::PullHotlinkedImages doMDexpect(post.cooked).to match(/<p><img src=.*\/uploads/)- expect(post.cooked).to match(/<img src=.*\/uploads.*\ class="thumbnail"/)+ expect(post.cooked).to match(/<img src=.*\/uploads.*\ class="thumbnail/)expect(post.cooked).to match(/<span class="broken-image/)expect(post.cooked).to match(/<div class="large-image-placeholder">/)end@@ -542,12 +541,10 @@ RSpec.describe Jobs::PullHotlinkedImages doenddescribe "with a lightboxed image" do- fab!(:upload) { Fabricate(:upload) }+ fab!(:upload) { Fabricate(:large_image_upload) }fab!(:user) { Fabricate(:user) }before do- FastImage.expects(:size).returns([1750, 2000]).at_least_once- OptimizedImage.stubs(:resize).returns(true)Jobs.run_immediately!enddiff --git a/spec/lib/cooked_post_processor_spec.rb b/spec/lib/cooked_post_processor_spec.rbindex 0d8c7402749..26f1eaededc 100644--- a/spec/lib/cooked_post_processor_spec.rb+++ b/spec/lib/cooked_post_processor_spec.rb@@ -5,6 +5,7 @@ require "file_store/s3_store"RSpec.describe CookedPostProcessor dofab!(:upload) { Fabricate(:upload) }+ fab!(:large_image_upload) { Fabricate(:large_image_upload) }let(:upload_path) { Discourse.store.upload_path }describe "#post_process" do@@ -764,7 +765,6 @@ RSpec.describe CookedPostProcessor doendit "won't remove the original image if another post doesn't have an image" do- FastImage.stubs(:size)topic = post.topiccpp.post_process@@ -781,8 +781,7 @@ RSpec.describe CookedPostProcessor doendit "generates thumbnails correctly" do- FastImage.expects(:size).returns([1750, 2000])-+ # image size in cooked is 1500*2000topic = post.topiccpp.post_processtopic.reload@@ -941,17 +940,13 @@ RSpec.describe CookedPostProcessor dodescribe "#convert_to_link" dofab!(:thumbnail) { Fabricate(:optimized_image, upload: upload, width: 512, height: 384) }- before do- CookedPostProcessor.any_instance.stubs(:get_size).with(upload.url).returns([1024, 768])- end-it "adds lightbox and optimizes images" do- post = Fabricate(:post, raw: "")-+ post = Fabricate(:post, raw: "")cpp = CookedPostProcessor.new(post, disable_dominant_color: true)cpp.post_processdoc = Nokogiri::HTML5::fragment(cpp.html)+expect(doc.css('.lightbox-wrapper').size).to eq(1)expect(doc.css('img').first['srcset']).to_not eq(nil)end@@ -1001,7 +996,7 @@ RSpec.describe CookedPostProcessor doit "optimizes images in quotes" dopost = Fabricate(:post, raw: <<~MD)[quote]- + [/quote]MD@@ -1016,7 +1011,7 @@ RSpec.describe CookedPostProcessor doit "optimizes images in Onebox" doOneboxer.expects(:onebox).with("https://discourse.org", anything)- .returns("<aside class='onebox'><img src='#{upload.url}' width='512' height='384'></aside>")+ .returns("<aside class='onebox'><img src='#{large_image_upload.url}' width='512' height='384'></aside>")post = Fabricate(:post, raw: "https://discourse.org")