Add http_header to FastText for Fastly compatibility (#19999)

* Add http_header to FastText for Fastly compatibility

* Update app/services/articles/enrich_image_attributes.rb

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update app/services/html/parser.rb

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update spec/models/display_ad_spec.rb

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Fix rubocop

* Also accept canges to main image for this re-calc

* Rename display_ad_spec for compatibility with main

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Ben Halpern 2023-08-28 09:34:59 -04:00 committed by GitHub
parent 2d6b740940
commit 3324bf47f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 11 additions and 8 deletions

View file

@ -35,7 +35,7 @@
aspect-ratio: auto 650 / 273;
object-fit: contain;
border-radius: var(--radius-auto) var(--radius) 0 0;
max-height: calc(100vh - var(--header-height) - var(--su-4));
max-height: calc(90vh - var(--header-height));
}
}
}

View file

@ -1011,7 +1011,7 @@ class Article < ApplicationRecord
end
def enrich_image_attributes
return unless saved_change_to_attribute?(:processed_html)
return unless saved_change_to_attribute?(:processed_html) || saved_change_to_attribute?(:main_image)
::Articles::EnrichImageAttributesWorker.perform_async(id)
end

View file

@ -16,6 +16,7 @@ module Articles
].join(", ").freeze
def self.call(article)
fast_image_headers = { "User-Agent" => "#{Settings::Community.community_name} (#{URL.url})" }
parsed_html = Nokogiri::HTML.fragment(article.processed_html)
main_image_height = default_image_height
@ -35,13 +36,13 @@ module Articles
next if image.blank?
img_properties = FastImage.new(image, timeout: 10)
img_properties = FastImage.new(image, timeout: 10, http_header: fast_image_headers)
img["width"], img["height"] = img_properties.size
img["data-animated"] = true if img_properties.type == :gif
end
if article.main_image && Settings::UserExperience.cover_image_fit == "limit"
main_image_size = FastImage.size(article.main_image, timeout: 15)
main_image_size = FastImage.size(article.main_image, timeout: 15, http_header: fast_image_headers)
main_image_height = (main_image_size[1].to_f / main_image_size[0]) * 1000 if main_image_size
end

View file

@ -43,7 +43,8 @@ module Html
next if allowed_image_host?(src)
if synchronous_detail_detection
img["width"], img["height"] = FastImage.size(src, timeout: 10)
header = { "User-Agent" => "#{Settings::Community.community_name} (#{URL.url})" }
img["width"], img["height"] = FastImage.size(src, timeout: 10, http_header: header)
end
img["loading"] = "lazy"

View file

@ -2,7 +2,7 @@ module Articles
class EnrichImageAttributesWorker
include Sidekiq::Job
sidekiq_options queue: :medium_priority, retry: 5, lock: :until_executing
sidekiq_options queue: :high_priority, retry: 5, lock: :until_executing
def perform(article_id)
article = Article.find_by(id: article_id)

View file

@ -173,7 +173,8 @@ RSpec.describe Billboard do
allow(Images::Optimizer).to receive(:call).and_return(image_url)
image_md = "![Image description](#{image_url})<p style='margin-top:100px'>Hello <em>hey</em> Hey hey</p>"
create(:billboard, body_markdown: image_md, placement_area: "post_comments")
expect(FastImage).to have_received(:size).with(image_url, { timeout: 10 })
options = { http_header: { "User-Agent" => "DEV(local) (http://forem.test)" }, timeout: 10 }
expect(FastImage).to have_received(:size).with(image_url, options)
# width is billboard.prefix_width
expect(Images::Optimizer).to have_received(:call).with(image_url, width: Billboard::POST_WIDTH)
# Images::Optimizer.call(source, width: width)

View file

@ -1,7 +1,7 @@
require "rails_helper"
RSpec.describe Articles::EnrichImageAttributesWorker, type: :worker do
include_examples "#enqueues_on_correct_queue", "medium_priority", 1
include_examples "#enqueues_on_correct_queue", "high_priority", 1
describe "#perform" do
let(:worker) { subject }