188. Delete user image on delete user (#28 Section 28: Enhancement)
This commit is contained in:
@ -110,23 +110,17 @@ public class UserResource {
|
|||||||
|
|
||||||
@GetMapping(path = "{username}/image/profile", produces = MediaType.IMAGE_JPEG_VALUE)
|
@GetMapping(path = "{username}/image/profile", produces = MediaType.IMAGE_JPEG_VALUE)
|
||||||
public byte[] getProfileImage(@PathVariable String username) throws IOException {
|
public byte[] getProfileImage(@PathVariable String username) throws IOException {
|
||||||
byte[] profileImage = userService.getProfileImage(username);
|
return userService.getProfileImage(username);
|
||||||
log.debug("File size: {}", profileImage.length);
|
|
||||||
return profileImage;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(path = "image/profile/{userId}/{filename}", produces = MediaType.IMAGE_JPEG_VALUE)
|
@GetMapping(path = "image/profile/{userId}/{filename}", produces = MediaType.IMAGE_JPEG_VALUE)
|
||||||
public byte[] getProfileImageByUserId(@PathVariable String userId, @PathVariable String filename) throws IOException {
|
public byte[] getProfileImageByUserId(@PathVariable String userId, @PathVariable String filename) throws IOException {
|
||||||
byte[] profileImage = userService.getImageByUserId(userId, filename);
|
return userService.getImageByUserId(userId, filename);
|
||||||
log.debug("File size: {}", profileImage.length);
|
|
||||||
return profileImage;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(path = "image/profile/{userId}", produces = MediaType.IMAGE_JPEG_VALUE)
|
@GetMapping(path = "image/profile/{userId}", produces = MediaType.IMAGE_JPEG_VALUE)
|
||||||
public byte[] getDefaultProfileImage(@PathVariable String userId) {
|
public byte[] getDefaultProfileImage(@PathVariable String userId) {
|
||||||
byte[] profileImage = userService.getDefaultProfileImage(userId);
|
return userService.getDefaultProfileImage(userId);
|
||||||
log.debug("File size: {}", profileImage.length);
|
|
||||||
return profileImage;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void authenticate(String username, String password) {
|
private void authenticate(String username, String password) {
|
||||||
|
|||||||
@ -23,6 +23,7 @@ import org.springframework.security.core.userdetails.UserDetails;
|
|||||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.FileSystemUtils;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
||||||
@ -194,6 +195,16 @@ public class UserServiceImpl implements UserService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void deleteProfileImageFolder(User user) {
|
||||||
|
|
||||||
|
Path userFolder = Paths.get(USER_FOLDER, user.getUserId());
|
||||||
|
try {
|
||||||
|
FileSystemUtils.deleteRecursively(userFolder);
|
||||||
|
} catch (IOException exception) {
|
||||||
|
log.error("Can't delete folder", exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public User updateUser(String username, UserDto userDto) {
|
public User updateUser(String username, UserDto userDto) {
|
||||||
|
|
||||||
@ -222,6 +233,8 @@ public class UserServiceImpl implements UserService {
|
|||||||
User userToBeDeleted = userRepository
|
User userToBeDeleted = userRepository
|
||||||
.findByUserId(userId)
|
.findByUserId(userId)
|
||||||
.orElseThrow(() -> new UserNotFoundException("User was not found"));
|
.orElseThrow(() -> new UserNotFoundException("User was not found"));
|
||||||
|
|
||||||
|
deleteProfileImageFolder(userToBeDeleted);
|
||||||
userRepository.delete(userToBeDeleted);
|
userRepository.delete(userToBeDeleted);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user