Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions app/helpers/advertisement_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,25 @@ def wrap_text(text, width, font_size)
# images.
# @param icon_path [String] A path or URI from which to load the image. If using a path this should be the asset path
# as it would be accessible through the Rails server - see example.
# @return [Magick::ImageList] An ImageList containing the icon.
# @return [Magick::ImageList, nil] An ImageList containing the icon if found
# @example Load an image from app/assets/images:
# # This uses the path from which the image would be accessed via HTTP.
# helpers.community_icon('/assets/codidact.png')
def community_icon(icon_path)
if icon_path.start_with? '/assets/'
icon = Magick::ImageList.new("./app/assets/images/#{File.basename(icon_path)}")
return nil unless icon_path.present?

expanded_path = File.expand_path(icon_path)

if expanded_path.start_with?('/assets/')
icon = Magick::ImageList.new("./app/assets/images/#{File.basename(expanded_path)}")
else
icon = Magick::ImageList.new
icon_path_content = URI.open(icon_path).read # rubocop:disable Security/Open
icon.from_blob(icon_path_content)
end

icon
rescue
nil
end
end
6 changes: 3 additions & 3 deletions app/helpers/advertisements/article_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def article_ad(article)
s.fill = 'white'
end

icon_path = SiteSetting.find_by(name: 'SiteLogoPath', community: article.community).typed
if icon_path.present?
icon = community_icon(icon_path)
icon = community_icon(SiteSetting['SiteLogoPath', community: article.community])

if icon.present?
icon.resize_to_fit!(120, 50)
ad.composite!(icon, SouthWestGravity, 20, 15, SrcAtopCompositeOp)
else
Expand Down
6 changes: 3 additions & 3 deletions app/helpers/advertisements/community_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def community_ad
img.fill = 'white'
end

icon_path = SiteSetting['SiteLogoPath']
if icon_path.present?
icon = community_icon(icon_path)
icon = community_icon(SiteSetting['SiteLogoPath'])

if icon.present?
icon.resize_to_fit!(400, 200)
ad.composite!(icon, CenterGravity, 0, -175, SrcAtopCompositeOp)
else
Expand Down
6 changes: 3 additions & 3 deletions app/helpers/advertisements/question_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def question_ad(question)
img.fill = 'white'
end

icon_path = SiteSetting.find_by(name: 'SiteLogoPath', community: question.community).typed
if icon_path.present?
icon = community_icon(icon_path)
icon = community_icon(SiteSetting['SiteLogoPath', community: question.community])

if icon.present?
icon.resize_to_fit!(175, 75)
ad.composite!(icon, SouthWestGravity, 20, 15, SrcAtopCompositeOp)
else
Expand Down
27 changes: 27 additions & 0 deletions test/helpers/advertisement_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'test_helper'

class AdvertisementHelperTest < ActionView::TestCase
include ApplicationHelper
include AdvertisementHelper

setup do
@external_png = File.open(Rails.root.join('app/assets/images/logo.png'))
stub_request(:get, 'https://example.com/external.png').to_return(body: @external_png)
end

teardown do
@external_png.close
end

test ':community_icon should gracefully handle non-existent paths' do
assert_nothing_raised do
icon = community_icon('/assets/non-existent.jpeg')
assert_nil icon
end
end

test ':community_icon should correctly handle external URLs' do
icon = community_icon('https://example.com/external.png')
assert icon.is_a?(Magick::ImageList)
end
end
Loading