A platform for community discussion. Free, open, simple.
DEV: Map already-downloaded hotlinked images in post_process_cooked
David Taylor0baabafa9d48
diff --git a/app/jobs/regular/pull_hotlinked_images.rb b/app/jobs/regular/pull_hotlinked_images.rbindex bf0aa6014c7..ba3389843ea 100644--- a/app/jobs/regular/pull_hotlinked_images.rb+++ b/app/jobs/regular/pull_hotlinked_images.rb@@ -237,12 +237,7 @@ module Jobsprotecteddef normalize_src(src)- uri = Addressable::URI.heuristic_parse(src)- uri.normalize!- uri.scheme = nil- uri.to_s- rescue URI::Error, Addressable::URI::InvalidURIError- src+ PostHotlinkedMedia.normalize_src(src)endenddiff --git a/app/models/post_hotlinked_media.rb b/app/models/post_hotlinked_media.rbindex 1bf5d14e9e2..fc8e5814dfb 100644--- a/app/models/post_hotlinked_media.rb+++ b/app/models/post_hotlinked_media.rb@@ -9,6 +9,15 @@ class PostHotlinkedMedia < ActiveRecord::Basedownload_failed: "download_failed",upload_create_failed: "upload_create_failed"}++ def self.normalize_src(src)+ uri = Addressable::URI.heuristic_parse(src)+ uri.normalize!+ uri.scheme = nil+ uri.to_s+ rescue URI::Error, Addressable::URI::InvalidURIError+ src+ endend# == Schema Informationdiff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rbindex e8f48d73138..8444a4768bb 100644--- a/lib/cooked_post_processor.rb+++ b/lib/cooked_post_processor.rb@@ -144,18 +144,6 @@ class CookedPostProcessor@doc.css(".onebox.githubfolder img")end- def large_images- @large_images ||= @post&.post_hotlinked_media.select { |r| r.too_large? }.map(&:url)- end-- def broken_images- @broken_images ||= @post&.post_hotlinked_media.select { |r| r.download_failed? }.map(&:url)- end-- def downloaded_images- @downloaded_images ||= @post&.post_hotlinked_media.select { |r| r.downloaded? }.map { |r| [r.url, r.upload_id] }.to_h- end-def convert_to_link!(img)w, h = img["width"].to_i, img["height"].to_iuser_width, user_height = (w > 0 && h > 0 && [w, h]) ||@@ -412,10 +400,31 @@ class CookedPostProcessordef post_process_imagesextract_images.each do |img|- convert_to_link!(img) unless add_image_placeholder!(img)+ still_an_image = process_hotlinked_image(img)+ convert_to_link!(img) if still_an_imageendend+ def process_hotlinked_image(img)+ @hotlinked_map ||= @post.post_hotlinked_media.preload(:upload).map { |r| [r.url, r] }.to_h+ normalized_src = PostHotlinkedMedia.normalize_src(img["src"])+ info = @hotlinked_map[normalized_src]++ still_an_image = true++ if info&.too_large?+ add_large_image_placeholder!(img)+ still_an_image = false+ elsif info&.download_failed?+ add_broken_image_placeholder!(img)+ still_an_image = false+ elsif info&.downloaded? && upload = info&.upload+ img["src"] = UrlHelper.cook_url(upload.url, secure: @with_secure_media)+ end++ still_an_image+ end+def is_svg?(img)path =begindiff --git a/lib/cooked_processor_mixin.rb b/lib/cooked_processor_mixin.rbindex 9985c1684b1..8d49865c3c0 100644--- a/lib/cooked_processor_mixin.rb+++ b/lib/cooked_processor_mixin.rb@@ -48,18 +48,9 @@ module CookedProcessorMixinimg_classes = (img["class"] || "").split(" ")link_classes = ((parent&.name == "a" && parent["class"]) || "").split(" ")- if img_classes.include?("onebox") || link_classes.include?("onebox")- next if add_image_placeholder!(img)- elsif large_images.include?(src) || broken_images.include?(src)- img.remove- next- end-- upload_id = downloaded_images[src]- upload = Upload.find_by_id(upload_id) if upload_id-- if upload.present?- img["src"] = UrlHelper.cook_url(upload.url, secure: @with_secure_media)+ if respond_to?(:process_hotlinked_image, true)+ still_an_image = process_hotlinked_image(img)+ next if !still_an_imageend# make sure we grab dimensions for oneboxed images@@ -201,18 +192,6 @@ module CookedProcessorMixinrescue URI::Errorend- def add_image_placeholder!(img)- src = img["src"].sub(/^https?:/i, "")-- if large_images.include?(src)- return add_large_image_placeholder!(img)- elsif broken_images.include?(src)- return add_broken_image_placeholder!(img)- end-- false- end-def add_large_image_placeholder!(img)url = img["src"]