Fix html variant image prefix logic (#11917)

* Fix html variant image prefix logic

* Update app/models/html_variant.rb

Co-authored-by: Mac Siri <mac@dev.to>

* Update spec/models/html_variant_spec.rb

Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com>

* Use helper to simplify url code and update spec

* Provide Imgproxy default endpoint

Co-authored-by: Mac Siri <mac@dev.to>
Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com>
Co-authored-by: Mac Siri <krairit.siri@gmail.com>
This commit is contained in:
Ben Halpern 2020-12-15 14:44:11 -05:00 committed by GitHub
parent bb532d39b3
commit ad1a59a14c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 2 deletions

View file

@ -75,6 +75,6 @@ class HtmlVariant < ApplicationRecord
end
def allowed_image_host?(src)
src.start_with?("https://res.cloudinary.com/")
src.start_with?("https://res.cloudinary.com/") || src.start_with?(Images::Optimizer.get_imgproxy_endpoint)
end
end

View file

@ -66,7 +66,7 @@ module Images
# On other environments, rely on ApplicationConfig for a
# more flexible configuration
# ie. default imgproxy endpoint is localhost:8080
ApplicationConfig["IMGPROXY_ENDPOINT"]
ApplicationConfig["IMGPROXY_ENDPOINT"] || "http://localhost:8080"
end
end
end

View file

@ -55,4 +55,19 @@ RSpec.describe HtmlVariant, type: :model do
cloudinary_string = "/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_420/https://devimages.com/image.jpg"
expect(html_variant.html).to include(cloudinary_string)
end
it "does not add prefix if it already starts with cloudinary" do
allow(Images::Optimizer).to receive(:call)
html = %(<img src="https://res.cloudinary.com/image.jpg">)
html_variant.update(approved: false, html: html)
expect(Images::Optimizer).not_to have_received(:call)
end
it "does not add prefix if already on site root" do
allow(Images::Optimizer).to receive(:call)
html = "<img src=\"#{Images::Optimizer.get_imgproxy_endpoint}/image.jpg\">"
html_variant.update(approved: false, html: html)
html_variant.save
expect(Images::Optimizer).not_to have_received(:call)
end
end