From a5bace37eee22ef1fffe9033b593065349b11b4a Mon Sep 17 00:00:00 2001 From: Takagi <1103069291@qq.com> Date: Thu, 27 Jul 2023 11:48:13 +0800 Subject: [PATCH] fix: resolve the issue of missing old avatar (#4293) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind bug /area core #### What this PR does / why we need it: 解决 #4253 所导致的用户旧头像被删除的问题。 #### Special notes for your reviewer: 1. 使用 2.7.x 之前的 Halo 版本,新建用户并设置其头像。 2. 升级 Halo 至 2.8.0-rc.1 3. 头像还存在即可。 4. 测试头像上传、移除功能是否正常可用。 #### Does this PR introduce a user-facing change? ```release-note None ``` --- .../app/core/extension/endpoint/UserEndpoint.java | 1 + .../core/extension/reconciler/UserReconciler.java | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/application/src/main/java/run/halo/app/core/extension/endpoint/UserEndpoint.java b/application/src/main/java/run/halo/app/core/extension/endpoint/UserEndpoint.java index 23a131ee6..736bdb594 100644 --- a/application/src/main/java/run/halo/app/core/extension/endpoint/UserEndpoint.java +++ b/application/src/main/java/run/halo/app/core/extension/endpoint/UserEndpoint.java @@ -209,6 +209,7 @@ public class UserEndpoint implements CustomEndpoint { .flatMap(user -> { MetadataUtil.nullSafeAnnotations(user) .remove(User.AVATAR_ATTACHMENT_NAME_ANNO); + user.getSpec().setAvatar(null); return client.update(user); }) .flatMap(user -> ServerResponse.ok().bodyValue(user)); diff --git a/application/src/main/java/run/halo/app/core/extension/reconciler/UserReconciler.java b/application/src/main/java/run/halo/app/core/extension/reconciler/UserReconciler.java index ab89f6e74..927c50d75 100644 --- a/application/src/main/java/run/halo/app/core/extension/reconciler/UserReconciler.java +++ b/application/src/main/java/run/halo/app/core/extension/reconciler/UserReconciler.java @@ -78,12 +78,6 @@ public class UserReconciler implements Reconciler { oldAvatarAttachmentName = null; } - if (StringUtils.isBlank(oldAvatarAttachmentName) - && StringUtils.isNotBlank(avatarAttachmentName)) { - oldAvatarAttachmentName = avatarAttachmentName; - annotations.put(User.LAST_AVATAR_ATTACHMENT_NAME_ANNO, oldAvatarAttachmentName); - } - if (StringUtils.isNotBlank(avatarAttachmentName)) { client.fetch(Attachment.class, avatarAttachmentName) .ifPresent(attachment -> { @@ -93,9 +87,15 @@ public class UserReconciler implements Reconciler { } user.getSpec().setAvatar(avatarUri.toString()); }); - } else { + } else if (StringUtils.isNotBlank(oldAvatarAttachmentName)) { user.getSpec().setAvatar(null); } + + if (StringUtils.isBlank(oldAvatarAttachmentName) + && StringUtils.isNotBlank(avatarAttachmentName)) { + annotations.put(User.LAST_AVATAR_ATTACHMENT_NAME_ANNO, avatarAttachmentName); + } + client.update(user); }); }