Do not use ActiveStorage variants, process original avatar
Variants are currently broken. So we process the original file with the most common avatar dimensions and stripping metadata, then hash and upload only that version.
This commit is contained in:
@@ -56,6 +56,7 @@ class User < ApplicationRecord
|
||||
validates_length_of :display_name, minimum: 3, maximum: 35, allow_blank: true,
|
||||
if: -> { defined?(@display_name) }
|
||||
|
||||
|
||||
validate :acceptable_avatar
|
||||
|
||||
validate :acceptable_pgp_key_format, if: -> { defined?(@pgp_pubkey) && @pgp_pubkey.present? }
|
||||
@@ -83,6 +84,10 @@ class User < ApplicationRecord
|
||||
:timeoutable,
|
||||
:rememberable
|
||||
|
||||
#
|
||||
# Methods
|
||||
#
|
||||
|
||||
def ldap_before_save
|
||||
self.email = Devise::LDAP::Adapter.get_ldap_param(self.cn, "mail").first
|
||||
self.ou = dn.split(',')
|
||||
@@ -165,23 +170,19 @@ class User < ApplicationRecord
|
||||
@display_name ||= ldap_entry[:display_name]
|
||||
end
|
||||
|
||||
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_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
|
||||
# TODO Variant keys are currently broken for some reason
|
||||
# (They use the same key as the main blob, when it should be
|
||||
# "/variants/#{key)"
|
||||
# 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
|
||||
@nostr_pubkey ||= ldap_entry[:nostr_key]
|
||||
|
||||
Reference in New Issue
Block a user