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.
		
			
				
	
	
		
			43 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			43 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			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_avatar
 | 
						|
 | 
						|
      Rails.logger.debug { "Storing new jpegPhoto for user #{@user.cn} in LDAP" }
 | 
						|
      result = replace_attribute(@dn, :jpegPhoto, jpg_data)
 | 
						|
      result == 0
 | 
						|
    end
 | 
						|
 | 
						|
    private
 | 
						|
 | 
						|
    def process_avatar
 | 
						|
      @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
 |