PERF: stop downloading images from post processor and lean on uploads

Previously we would unconditionally fetch all images via HTTP to grab
original sizing from cooked post processor in 2 different spots.

This was wasteful as we already calculate and cache this info in upload records.

This also simplifies some specs and reduces use of mocks.
This commit is contained in:
Sam
2022-11-25 12:40:31 +11:00
committed by GitHub
parent 45f3e9f19e
commit 755ca0fcbb
6 changed files with 39 additions and 27 deletions

View File

@ -153,14 +153,21 @@ class CookedPostProcessor
src = 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")
end