mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 09:42:07 +08:00
DEV: Map already-downloaded hotlinked images in post_process_cooked
Previously this mapping of **cooked** images was only being run for oneboxes. Now it runs for all images, so we can transform hotlinked images without needing to immediately update `raw`
This commit is contained in:
parent
115fe90bff
commit
0baabafa9d
|
@ -237,12 +237,7 @@ module Jobs
|
|||
protected
|
||||
|
||||
def 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)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -9,6 +9,15 @@ class PostHotlinkedMedia < ActiveRecord::Base
|
|||
download_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
|
||||
end
|
||||
end
|
||||
|
||||
# == Schema Information
|
||||
|
|
|
@ -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_i
|
||||
user_width, user_height = (w > 0 && h > 0 && [w, h]) ||
|
||||
|
@ -412,10 +400,31 @@ class CookedPostProcessor
|
|||
|
||||
def post_process_images
|
||||
extract_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_image
|
||||
end
|
||||
end
|
||||
|
||||
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 =
|
||||
begin
|
||||
|
|
|
@ -48,18 +48,9 @@ module CookedProcessorMixin
|
|||
img_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_image
|
||||
end
|
||||
|
||||
# make sure we grab dimensions for oneboxed images
|
||||
|
@ -201,18 +192,6 @@ module CookedProcessorMixin
|
|||
rescue URI::Error
|
||||
end
|
||||
|
||||
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"]
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user