Files
fizzy/app/controllers/users/avatars_controller.rb
T
Stanko K.R. fe909d6dc5 Disable stale while revalidate for own avatar
Locally, having stale_while_revalidate works great, but in production when we are behind CloudFlare, this results in an old image being shown after you upload a new one

See: https://app.fizzy.do/5986089/cards/2978
2025-11-21 19:26:38 +01:00

49 lines
997 B
Ruby

class Users::AvatarsController < ApplicationController
include ActiveStorage::Streaming
allow_unauthenticated_access only: :show
before_action :set_user
before_action :ensure_permission_to_administer_user, only: :destroy
def show
if stale? @user, cache_control: cache_control
render_avatar_or_initials
end
end
def destroy
@user.avatar.destroy
redirect_to @user
end
private
def set_user
@user = Current.account.users.find(params[:user_id])
end
def ensure_permission_to_administer_user
head :forbidden unless Current.user.can_change?(@user)
end
def cache_control
if @user == Current.user
{}
else
{ max_age: 30.minutes, stale_while_revalidate: 1.week }
end
end
def render_avatar_or_initials
if @user.avatar.attached?
send_blob_stream @user.avatar
else
render_initials
end
end
def render_initials
render formats: :svg
end
end