27.4.1. Modified endpoints related to profile image - backend (#27)
This commit is contained in:
@ -6,7 +6,7 @@ public class FileConstant {
|
||||
public static final String JPG_EXTENSION = "jpg";
|
||||
public static final String USER_FOLDER = System.getProperty("user.home") + "/supportportal/user/";
|
||||
public static final String DIRECTORY_CREATED = "Created directory for: ";
|
||||
public static final String DEFAULT_USER_IMAGE_PATH = "/user/image/profile/";
|
||||
public static final String DEFAULT_USER_IMAGE_URI_PATTERN = "/user/%s/profile-image";
|
||||
public static final String USER_IMAGE_FILENAME = "avatar.jpg";
|
||||
public static final String FILE_SAVED_IN_FILE_SYSTEM = "Saved file in file system by name: ";
|
||||
public static final String DOT = ".";
|
||||
|
||||
@ -104,22 +104,17 @@ public class UserResource {
|
||||
.build();
|
||||
}
|
||||
|
||||
@PutMapping("{username}/profileImage")
|
||||
public User updateProfileImage(@PathVariable String username, MultipartFile profileImage) {
|
||||
return userService.updateProfileImage(username, profileImage);
|
||||
@PutMapping("{userId}/profile-image")
|
||||
public User updateProfileImage(@PathVariable UUID userId, MultipartFile profileImage) {
|
||||
return userService.updateProfileImage(userId, profileImage);
|
||||
}
|
||||
|
||||
@GetMapping(path = "{username}/image/profile", produces = MediaType.IMAGE_JPEG_VALUE)
|
||||
public byte[] getProfileImage(@PathVariable String username) throws IOException {
|
||||
return userService.getProfileImage(username);
|
||||
}
|
||||
|
||||
@GetMapping(path = "image/profile/{userId}/{filename}", produces = MediaType.IMAGE_JPEG_VALUE)
|
||||
@GetMapping(path = "{userId}/profile-image/{filename}", produces = MediaType.IMAGE_JPEG_VALUE)
|
||||
public byte[] getProfileImageByUserId(@PathVariable UUID userId, @PathVariable String filename) throws IOException {
|
||||
return userService.getImageByUserId(userId, filename);
|
||||
}
|
||||
|
||||
@GetMapping(path = "image/profile/{userId}", produces = MediaType.IMAGE_JPEG_VALUE)
|
||||
@GetMapping(path = "{userId}/profile-image", produces = MediaType.IMAGE_JPEG_VALUE)
|
||||
public byte[] getDefaultProfileImage(@PathVariable UUID userId) {
|
||||
return userService.getDefaultProfileImage(userId);
|
||||
}
|
||||
|
||||
@ -30,9 +30,7 @@ public interface UserService extends UserDetailsService {
|
||||
|
||||
void resetPassword(String email);
|
||||
|
||||
User updateProfileImage(String username, MultipartFile profileImage);
|
||||
|
||||
byte[] getProfileImage(String username) throws IOException;
|
||||
User updateProfileImage(UUID userId, MultipartFile profileImage);
|
||||
|
||||
byte[] getImageByUserId(UUID userId, String filename) throws IOException;
|
||||
|
||||
|
||||
@ -105,15 +105,13 @@ public class UserServiceImpl implements UserService {
|
||||
|
||||
private String generateDefaultProfileImageUrl(UUID userId) {
|
||||
return ServletUriComponentsBuilder.fromCurrentContextPath()
|
||||
.path(DEFAULT_USER_IMAGE_PATH)
|
||||
.pathSegment(userId.toString())
|
||||
.path(String.format(DEFAULT_USER_IMAGE_URI_PATTERN, userId))
|
||||
.toUriString();
|
||||
}
|
||||
|
||||
private String generateProfileImageUrl(UUID userId) {
|
||||
return ServletUriComponentsBuilder.fromCurrentContextPath()
|
||||
.path(DEFAULT_USER_IMAGE_PATH)
|
||||
.pathSegment(userId.toString())
|
||||
.path(String.format(DEFAULT_USER_IMAGE_URI_PATTERN, userId))
|
||||
.pathSegment(USER_IMAGE_FILENAME)
|
||||
.toUriString();
|
||||
}
|
||||
@ -265,20 +263,19 @@ public class UserServiceImpl implements UserService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public User updateProfileImage(String username, MultipartFile profileImage) {
|
||||
User user = findByUsername(username);
|
||||
public User updateProfileImage(UUID userId, MultipartFile profileImage) {
|
||||
User user = findByUserId(userId);
|
||||
saveProfileImage(user, profileImage);
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getProfileImage(String username) throws IOException {
|
||||
User user = findByUsername(username);
|
||||
return getImageByUserId(user.getUserId(), USER_IMAGE_FILENAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getImageByUserId(UUID userId, String filename) throws IOException {
|
||||
|
||||
if (!userRepository.existsByUserId(userId)) {
|
||||
throw new UserNotFoundException(USER_NOT_FOUND_MSG);
|
||||
}
|
||||
|
||||
Path userProfileImagePath = Paths
|
||||
.get(USER_FOLDER, userId.toString(), filename);
|
||||
return Files.readAllBytes(userProfileImagePath);
|
||||
|
||||
@ -40,7 +40,7 @@ spring:
|
||||
resources:
|
||||
add-mappings: false
|
||||
app:
|
||||
public-urls: /user/login,/user/register,/user/*/image/**,/user/image/**
|
||||
public-urls: /user/login,/user/register,/user/*/profile-image/**
|
||||
cors:
|
||||
allowed-origins: http://localhost:4200,https://localhost:4200,http://art-support-portal.s3-website.eu-north-1.amazonaws.com,http://portal.shyshkin.net
|
||||
jwt:
|
||||
|
||||
Reference in New Issue
Block a user