Files
once-campfire/app/controllers/users/avatars_controller.rb
T
Mike Dalessio b065b40a34 Disable libvips unfuzzed operations (#226)
and add test coverage for (un)supported file types.

The avatar and logo variants move into the models and return nil for content
types that are no longer variable, so the controllers fall back to the initials
avatar and stock logo icon instead of raising `ActiveStorage::InvariableError`.
2026-07-28 11:57:57 -04:00

40 lines
1023 B
Ruby

class Users::AvatarsController < ApplicationController
include ActiveStorage::Streaming
rescue_from(ActiveSupport::MessageVerifier::InvalidSignature) { head :not_found }
def show
@user = User.from_avatar_token(params[:user_id])
if stale?(etag: @user)
expires_in 30.minutes, public: true, stale_while_revalidate: 1.week
if (avatar_variant = @user.avatar_variant)
send_webp_blob_file avatar_variant.key
elsif @user.bot?
render_default_bot
else
render_initials
end
end
end
def destroy
Current.user.avatar.destroy
redirect_to user_profile_url
end
private
def send_webp_blob_file(key)
send_file ActiveStorage::Blob.service.path_for(key), content_type: "image/webp", disposition: :inline
end
def render_default_bot
send_file Rails.root.join("app/assets/images/default-bot-avatar.svg"), content_type: "image/svg+xml", disposition: :inline
end
def render_initials
render formats: :svg
end
end