akkounts/app/services/ldap_manager/update_avatar.rb
Râu Cao 17ffbde03a
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
WIP Store avatars as ActiveStorage attachments
Also push to LDAP as jpegPhoto
2025-05-11 18:43:21 +04:00

42 lines
983 B
Ruby

require "image_processing/vips"
module LdapManager
class UpdateAvatar < LdapManagerService
def initialize(user:)
@user = user
@dn = user.dn
end
def call
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(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