38 lines
866 B
Ruby
38 lines
866 B
Ruby
require "image_processing/vips"
|
|
|
|
module LdapManager
|
|
class UpdateAvatar < LdapManagerService
|
|
def initialize(dn:, file:)
|
|
@dn = dn
|
|
@img_data = process(file)
|
|
end
|
|
|
|
def call
|
|
Rails.logger.debug { "Storing jpegPhoto for dn=#{@dn}, size=#{@img_data.bytesize}" }
|
|
result = replace_attribute @dn, :jpegPhoto, @img_data
|
|
Rails.logger.debug { "LDAP replace_attribute result: #{result}" }
|
|
result
|
|
end
|
|
|
|
private
|
|
|
|
def process(file)
|
|
processed = ImageProcessing::Vips
|
|
.resize_to_fill(256, 256)
|
|
.source(file)
|
|
.convert("jpeg")
|
|
.saver(strip: true)
|
|
.call
|
|
|
|
data = processed.read
|
|
|
|
debug_file_path = "#{Rails.root}/tmp/avatar-processed.jpg"
|
|
debug_file = File.open(debug_file_path, 'wb')
|
|
debug_file.write(data)
|
|
debug_file.close
|
|
|
|
data
|
|
end
|
|
end
|
|
end
|