From 9e2210c45b3b6077d29daee6fe3e2cfec3621667 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Wed, 24 Jan 2024 15:56:34 +0300 Subject: [PATCH] Store avatars as binary instead of base64 --- app/models/user.rb | 13 +++++++---- app/services/ldap_manager/fetch_avatar.rb | 6 +++--- app/services/ldap_manager/update_avatar.rb | 8 +++---- app/views/settings/_profile.html.erb | 7 +++--- spec/features/settings/profile_spec.rb | 25 +++++++++++++--------- 5 files changed, 35 insertions(+), 24 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 1886b1f..d73d780 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -159,6 +159,15 @@ class User < ApplicationRecord @display_name ||= ldap_entry[:display_name] end + def avatar + @avatar ||= LdapManager::FetchAvatar.call(cn: cn) + end + + def avatar_base64 + return nil if avatar.nil? + @avatar_base64 ||= Base64.strict_encode64(avatar) + end + def nostr_pubkey @nostr_pubkey ||= ldap_entry[:nostr_key] end @@ -186,10 +195,6 @@ class User < ApplicationRecord ZBase32.encode(Digest::SHA1.digest(cn)) end - def avatar - @avatar_base64 ||= LdapManager::FetchAvatar.call(cn: cn) - end - def services_enabled ldap_entry[:services_enabled] || [] end diff --git a/app/services/ldap_manager/fetch_avatar.rb b/app/services/ldap_manager/fetch_avatar.rb index 11035ae..a838bb8 100644 --- a/app/services/ldap_manager/fetch_avatar.rb +++ b/app/services/ldap_manager/fetch_avatar.rb @@ -5,12 +5,12 @@ module LdapManager end def call - treebase = ldap_config["base"] + treebase = ldap_config["base"] attributes = %w{ jpegPhoto } - filter = Net::LDAP::Filter.eq("cn", @cn) + filter = Net::LDAP::Filter.eq("cn", @cn) entry = client.search(base: treebase, filter: filter, attributes: attributes).first - entry.try(:jpegPhoto) ? entry.jpegPhoto.first : nil + entry&.jpegPhoto ? entry.jpegPhoto.first : nil end end end diff --git a/app/services/ldap_manager/update_avatar.rb b/app/services/ldap_manager/update_avatar.rb index f3c8975..d238303 100644 --- a/app/services/ldap_manager/update_avatar.rb +++ b/app/services/ldap_manager/update_avatar.rb @@ -8,20 +8,20 @@ module LdapManager end def call - replace_attribute @dn, :jpegPhoto, @img_data + result = replace_attribute @dn, :jpegPhoto, @img_data + result end private def process(file) processed = ImageProcessing::Vips - .resize_to_fill(512, 512) + .resize_to_fill(256, 256) .source(file) .convert("jpeg") .saver(strip: true) .call - - Base64.strict_encode64 processed.read + processed.read end end end diff --git a/app/views/settings/_profile.html.erb b/app/views/settings/_profile.html.erb index 4443e25..45ffc37 100644 --- a/app/views/settings/_profile.html.erb +++ b/app/views/settings/_profile.html.erb @@ -20,7 +20,7 @@

- Your user address for Chat and Lightning Network. + Your account's address on the Internet

<%= form_for(@user, url: setting_path(:profile), html: { :method => :put }) do |f| %> @@ -40,9 +40,10 @@ Default profile picture

- <% if current_user.avatar.present? %> + <% unless current_user.avatar.nil? %>

- <%= image_tag "data:image/jpeg;base64,#{current_user.avatar}", class: "h-24 w-24 rounded-lg" %> + <%= image_tag "data:image/jpeg;base64,#{current_user.avatar_base64}", + class: "h-24 w-24 rounded-lg" %>

<% end %>
diff --git a/spec/features/settings/profile_spec.rb b/spec/features/settings/profile_spec.rb index 55b861e..8797890 100644 --- a/spec/features/settings/profile_spec.rb +++ b/spec/features/settings/profile_spec.rb @@ -2,18 +2,22 @@ require 'rails_helper' RSpec.describe 'Profile settings', type: :feature do let(:user) { create :user, cn: "mwahlberg" } - let(:avatar_base64) { File.read("#{Rails.root}/spec/fixtures/files/avatar-base64.txt") } + let(:avatar_jpeg) { File.binread("#{Rails.root}/spec/fixtures/files/taipei.jpg") } before do + Flipper.enable "avatar_upload" + login_as user, :scope => :user allow(user).to receive(:display_name).and_return("Mark") - allow_any_instance_of(User).to receive(:dn).and_return("cn=mwahlberg,ou=kosmos.org,cn=users,dc=kosmos,dc=org") - allow_any_instance_of(User).to receive(:ldap_entry).and_return({ - uid: user.cn, ou: user.ou, display_name: "Mark", pgp_key: nil - }) - allow_any_instance_of(User).to receive(:avatar).and_return(avatar_base64) - Flipper.enable "avatar_upload" + allow_any_instance_of(User).to receive(:dn) + .and_return("cn=mwahlberg,ou=kosmos.org,cn=users,dc=kosmos,dc=org") + allow_any_instance_of(User).to receive(:ldap_entry) + .and_return({ + uid: user.cn, ou: user.ou, display_name: "Mark", pgp_key: nil + }) + allow_any_instance_of(User).to receive(:avatar) + .and_return(avatar_jpeg) end feature "Update display name" do @@ -70,8 +74,8 @@ RSpec.describe 'Profile settings', type: :feature do scenario 'works with valid JPG file' do file_path = "#{Rails.root}/spec/fixtures/files/taipei.jpg" - expect_any_instance_of(LdapManager::UpdateAvatar).to receive(:replace_attribute) - .with(user.dn, :jpegPhoto, avatar_base64).and_return(true) + expect_any_instance_of(LdapManager::UpdateAvatar) + .to receive(:replace_attribute).and_return(true) visit setting_path(:profile) attach_file "Avatar", file_path @@ -86,7 +90,8 @@ RSpec.describe 'Profile settings', type: :feature do scenario 'works with valid PNG file' do file_path = "#{Rails.root}/spec/fixtures/files/bender.png" - expect(LdapManager::UpdateAvatar).to receive(:call).and_return(true) + expect_any_instance_of(LdapManager::UpdateAvatar) + .to receive(:replace_attribute).and_return(true) visit setting_path(:profile) attach_file "Avatar", file_path