WIP Store avatars as ActiveStorage attachments
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

Also push to LDAP as jpegPhoto
This commit is contained in:
2025-05-11 18:43:21 +04:00
parent 9e2210c45b
commit 17ffbde03a
8 changed files with 80 additions and 42 deletions

View File

@@ -4,8 +4,8 @@ class User < ApplicationRecord
include EmailValidatable
attr_accessor :current_password
attr_accessor :avatar_new
attr_accessor :display_name
attr_accessor :avatar_new
attr_accessor :pgp_pubkey
serialize :preferences, coder: UserPreferences
@@ -27,6 +27,12 @@ class User < ApplicationRecord
has_many :accounts, through: :lndhub_user
#
# Attachments
#
has_one_attached :avatar
#
# Validations
#
@@ -159,13 +165,30 @@ class User < ApplicationRecord
@display_name ||= ldap_entry[:display_name]
end
def avatar
@avatar ||= LdapManager::FetchAvatar.call(cn: cn)
def avatar_base64(size: :medium)
return nil unless avatar.attached?
variant = avatar_variant(size: size)
data = ActiveStorage::Blob.service.download(variant.key)
Base64.strict_encode64(data)
end
def avatar_base64
return nil if avatar.nil?
@avatar_base64 ||= Base64.strict_encode64(avatar)
def avatar_filename
return nil unless avatar.attached?
data = ActiveStorage::Blob.service.download(avatar.key)
hash = Digest::SHA256.hexdigest(data)
ext = avatar.content_type == "image/png" ? "png" : "jpg"
"#{hash}.#{ext}"
end
def avatar_variant(size: :medium)
dimensions = case size
when :large then [400, 400]
when :medium then [256, 256]
when :small then [64, 64]
else [256, 256]
end
format = avatar.content_type == "image/png" ? :png : :jpeg
avatar.variant(resize_to_fill: dimensions, format: format)
end
def nostr_pubkey
@@ -232,7 +255,7 @@ class User < ApplicationRecord
return unless avatar_new.present?
if avatar_new.size > 1.megabyte
errors.add(:avatar, "file size is too large")
errors.add(:avatar, "must be less than 1MB file size")
end
acceptable_types = ["image/jpeg", "image/png"]