Files
fizzy/app/controllers/users/avatars_controller.rb
T
Mike Dalessio bcb43305b6 Improve avatar image handling
- redirect avatar image requests to the rails_blob_url, instead of
  streaming them through the web app
- use a thumbnail variant for avatar images
- only put avatar initials behind the stale? check (not the image
  redirect, which would result in browsers rendering broken images when
  an avatar is changed, until max-age expires)
2025-11-21 15:16:46 -05:00

43 lines
948 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 @user.avatar.attached?
redirect_to rails_blob_url(@user.avatar.variant(:thumb), disposition: "inline")
elsif stale? @user, cache_control: cache_control
render_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_initials
render formats: :svg
end
end