mirror of
https://github.com/discourse/discourse.git
synced 2025-06-03 14:15:11 +08:00
PERF: Add GlobalSetting to redirect avatars instead of proxying (#19281)
When uploads are stored on S3, by default Discourse will fetch the avatars and proxy them through to the requesting client. This is simple, but it can lead to significant inbound/outbound network load in the hosting environment. This commit adds an optional redirect_avatar_requests GlobalSetting. When enabled, requests for user avatars will be redirected to the S3 asset instead of being proxied. This adds an extra round-trip for clients, but it should significantly reduce server load. To mitigate that extra round-trip for clients, a CDN with 'follow redirect' capability could be used.
This commit is contained in:
@ -125,6 +125,8 @@ class UserAvatarsController < ApplicationController
|
||||
if optimized.local?
|
||||
optimized_path = Discourse.store.path_for(optimized)
|
||||
image = optimized_path if File.exist?(optimized_path)
|
||||
elsif GlobalSetting.redirect_avatar_requests
|
||||
return redirect_s3_avatar(Discourse.store.cdn_url(optimized.url))
|
||||
else
|
||||
return proxy_avatar(Discourse.store.cdn_url(optimized.url), upload.created_at)
|
||||
end
|
||||
@ -179,6 +181,11 @@ class UserAvatarsController < ApplicationController
|
||||
send_file path, disposition: nil
|
||||
end
|
||||
|
||||
def redirect_s3_avatar(url)
|
||||
immutable_for 1.hour
|
||||
redirect_to url, allow_other_host: true
|
||||
end
|
||||
|
||||
# this protects us from a DoS
|
||||
def render_blank
|
||||
path = Rails.root + "public/images/avatar.png"
|
||||
|
Reference in New Issue
Block a user