fixed controllers
This commit is contained in:
@@ -20,7 +20,7 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
@RequiredArgsConstructor
|
||||
public class UserService {
|
||||
private final UserRepository repository;
|
||||
private final AvatarService avatarService;
|
||||
private final FileStorageService fileStorageService;
|
||||
|
||||
public User getCurrentUser() {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
@@ -55,8 +55,19 @@ public class UserService {
|
||||
|
||||
MultipartFile avatar = dto.getAvatar();
|
||||
if (avatar != null && !avatar.isEmpty()) {
|
||||
String url = avatarService.save(avatar); // сохранение на диск/в S3/Yandex Object Storage
|
||||
user.setAvatarUrl(url);
|
||||
String oldAvatarKey = user.getAvatarUrl();
|
||||
|
||||
String newAvatarKey = fileStorageService.uploadFile(avatar, "avatars");
|
||||
user.setAvatarUrl(newAvatarKey);
|
||||
|
||||
// подчищаем старый файл, если он был и это реально ключ MinIO
|
||||
if (oldAvatarKey != null && !oldAvatarKey.isBlank()) {
|
||||
try {
|
||||
fileStorageService.deleteFile(oldAvatarKey);
|
||||
} catch (Exception e) {
|
||||
log.warn("Не удалось удалить старый аватар из хранилища: {}", oldAvatarKey, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
repository.save(user);
|
||||
|
||||
Reference in New Issue
Block a user