Skip to content

Commit 39d973a

Browse files
committed
fixed setting SiteLogoPath to a missing asset causing server errors
1 parent 5eb67ed commit 39d973a

4 files changed

Lines changed: 19 additions & 12 deletions

File tree

app/helpers/advertisement_helper.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,25 @@ def wrap_text(text, width, font_size)
7878
# images.
7979
# @param icon_path [String] A path or URI from which to load the image. If using a path this should be the asset path
8080
# as it would be accessible through the Rails server - see example.
81-
# @return [Magick::ImageList] An ImageList containing the icon.
81+
# @return [Magick::ImageList, nil] An ImageList containing the icon if found
8282
# @example Load an image from app/assets/images:
8383
# # This uses the path from which the image would be accessed via HTTP.
8484
# helpers.community_icon('/assets/codidact.png')
8585
def community_icon(icon_path)
86-
if icon_path.start_with? '/assets/'
87-
icon = Magick::ImageList.new("./app/assets/images/#{File.basename(icon_path)}")
86+
return nil unless icon_path.present?
87+
88+
expanded_path = File.expand_path(icon_path)
89+
90+
if expanded_path.start_with?('/assets/')
91+
icon = Magick::ImageList.new("./app/assets/images/#{File.basename(expanded_path)}")
8892
else
8993
icon = Magick::ImageList.new
9094
icon_path_content = URI.open(icon_path).read # rubocop:disable Security/Open
9195
icon.from_blob(icon_path_content)
9296
end
97+
9398
icon
99+
rescue
100+
nil
94101
end
95102
end

app/helpers/advertisements/article_helper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ def article_ad(article)
2626
s.fill = 'white'
2727
end
2828

29-
icon_path = SiteSetting.find_by(name: 'SiteLogoPath', community: article.community).typed
30-
if icon_path.present?
31-
icon = community_icon(icon_path)
29+
icon = community_icon(SiteSetting['SiteLogoPath', community: article.community])
30+
31+
if icon.present?
3232
icon.resize_to_fit!(120, 50)
3333
ad.composite!(icon, SouthWestGravity, 20, 15, SrcAtopCompositeOp)
3434
else

app/helpers/advertisements/community_helper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ def community_ad
2525
img.fill = 'white'
2626
end
2727

28-
icon_path = SiteSetting['SiteLogoPath']
29-
if icon_path.present?
30-
icon = community_icon(icon_path)
28+
icon = community_icon(SiteSetting['SiteLogoPath'])
29+
30+
if icon.present?
3131
icon.resize_to_fit!(400, 200)
3232
ad.composite!(icon, CenterGravity, 0, -175, SrcAtopCompositeOp)
3333
else

app/helpers/advertisements/question_helper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ def question_ad(question)
2929
img.fill = 'white'
3030
end
3131

32-
icon_path = SiteSetting.find_by(name: 'SiteLogoPath', community: question.community).typed
33-
if icon_path.present?
34-
icon = community_icon(icon_path)
32+
icon = community_icon(SiteSetting['SiteLogoPath', community: question.community])
33+
34+
if icon.present?
3535
icon.resize_to_fit!(175, 75)
3636
ad.composite!(icon, SouthWestGravity, 20, 15, SrcAtopCompositeOp)
3737
else

0 commit comments

Comments
 (0)