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

@@ -10,7 +10,7 @@ module LdapManager
filter = Net::LDAP::Filter.eq("cn", @cn)
entry = client.search(base: treebase, filter: filter, attributes: attributes).first
entry&.jpegPhoto ? entry.jpegPhoto.first : nil
entry[:jpegPhoto].present? ? entry.jpegPhoto.first : nil
end
end
end

View File

@@ -2,26 +2,40 @@ require "image_processing/vips"
module LdapManager
class UpdateAvatar < LdapManagerService
def initialize(dn:, file:)
@dn = dn
@img_data = process(file)
def initialize(user:)
@user = user
@dn = user.dn
end
def call
result = replace_attribute @dn, :jpegPhoto, @img_data
result
unless @user.avatar.attached?
Rails.logger.error { "Cannot store empty jpegPhoto for user #{@user.cn}" }
return false
end
img_data = @user.avatar.blob.download
jpg_data = process(img_data)
result = replace_attribute(@dn, :jpegPhoto, jpg_data)
result == 0
end
private
def process(file)
processed = ImageProcessing::Vips
.resize_to_fill(256, 256)
.source(file)
.convert("jpeg")
.saver(strip: true)
.call
processed.read
def process(data)
@user.avatar.blob.open do |file|
processed = ImageProcessing::Vips
.source(file)
.resize_to_fill(256, 256)
.convert("jpeg")
.saver(strip: true)
.call
processed.read
end
rescue Vips::Error => e
Sentry.capture_exception(e) if Setting.sentry_enabled?
Rails.logger.error { "Image processing failed for LDAP avatar: #{e.message}" }
nil
end
end
end