Fix S3 keys/paths for user avatars
Also fixes the avatars controller to work with all back-ends
This commit is contained in:
@@ -34,10 +34,12 @@ class SettingsController < ApplicationController
|
||||
end
|
||||
|
||||
if @user.avatar_new.present?
|
||||
@user.avatar.attach(@user.avatar_new)
|
||||
@user.avatar.blob.update(filename: @user.avatar_filename)
|
||||
@user.save!
|
||||
LdapManager::UpdateAvatar.call(user: @user)
|
||||
if store_user_avatar
|
||||
LdapManager::UpdateAvatar.call(user: @user)
|
||||
else
|
||||
@validation_errors = @user.errors
|
||||
render :show, status: :unprocessable_entity and return
|
||||
end
|
||||
end
|
||||
|
||||
if @user.pgp_pubkey && (@user.pgp_pubkey != @user.ldap_entry[:pgp_key])
|
||||
@@ -187,4 +189,21 @@ class SettingsController < ApplicationController
|
||||
salt = BCrypt::Engine.generate_salt
|
||||
BCrypt::Engine.hash_secret(password, salt)
|
||||
end
|
||||
|
||||
def store_user_avatar
|
||||
data = @user.avatar_new.tempfile.read
|
||||
@user.avatar_new.tempfile.rewind
|
||||
hash = Digest::SHA256.hexdigest(data)
|
||||
ext = @user.avatar_new.content_type == "image/png" ? "png" : "jpg"
|
||||
filename = "#{hash}.#{ext}"
|
||||
|
||||
if filename == @user.avatar.filename.to_s
|
||||
@user.errors.add(:avatar, "must be a new file/picture")
|
||||
false
|
||||
else
|
||||
key = "users/#{@user.cn}/avatars/#{filename}"
|
||||
@user.avatar.attach io: @user.avatar_new.tempfile, key: key, filename: filename
|
||||
@user.save
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user