FIX: StaticController#favicon reads from disk when using local store. (#7160)

Since uploads site settings are now backed by an actual upload, we don't
have to reach over the network just to fetch the favicon. Instead, we
can just read the upload directly from disk.
This commit is contained in:
Guo Xiang Tan
2019-03-14 04:17:36 +08:00
committed by GitHub
parent 4f74210949
commit 1c6a2262b3
3 changed files with 69 additions and 39 deletions

View File

@@ -124,22 +124,28 @@ class StaticController < ApplicationController
is_asset_path
hijack do
data = DistributedMemoizer.memoize(FAVICON + SiteSetting.site_favicon_url, 60 * 30) do
begin
file = FileHelper.download(
UrlHelper.absolute(SiteSetting.site_favicon_url),
max_file_size: 50.kilobytes,
tmp_file_name: FAVICON,
follow_redirect: true
)
file ||= Tempfile.new([FAVICON, ".png"])
data = file.read
file.unlink
data
rescue => e
AdminDashboardData.add_problem_message('dashboard.bad_favicon_url', 1800)
Rails.logger.debug("Invalid favicon_url #{SiteSetting.site_favicon_url}: #{e}\n#{e.backtrace}")
""
data = DistributedMemoizer.memoize("FAVICON#{SiteSetting.favicon&.id}", 60 * 30) do
favicon = SiteSetting.favicon
next "" unless favicon
if Discourse.store.external?
begin
file = FileHelper.download(
UrlHelper.absolute(favicon.url),
max_file_size: favicon.filesize,
tmp_file_name: FAVICON,
follow_redirect: true
)
file&.read || ""
rescue => e
AdminDashboardData.add_problem_message('dashboard.bad_favicon_url', 1800)
Rails.logger.warn("Failed to fetch faivcon #{favicon.url}: #{e}\n#{e.backtrace}")
ensure
file&.unlink
end
else
File.read(Rails.root.join("public", favicon.url[1..-1]))
end
end