80.1 Get user profile image - through username (#9)

This commit is contained in:
Art
2021-09-10 15:48:15 +03:00
parent f64d9f7246
commit 1f14f925cf
5 changed files with 139 additions and 3 deletions

View File

@ -8,6 +8,7 @@ import net.shyshkin.study.fullstack.supportportal.backend.domain.User;
import net.shyshkin.study.fullstack.supportportal.backend.domain.dto.UserDto;
import net.shyshkin.study.fullstack.supportportal.backend.service.UserService;
import net.shyshkin.study.fullstack.supportportal.backend.utility.JwtTokenProvider;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@ -17,6 +18,7 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import static org.springframework.http.HttpStatus.OK;
@ -108,6 +110,13 @@ public class UserResource {
return userService.updateProfileImage(username, profileImage);
}
@GetMapping(path = "{username}/image/profile", produces = MediaType.IMAGE_JPEG_VALUE)
public byte[] getProfileImage(@PathVariable String username) throws IOException {
byte[] profileImage = userService.getProfileImage(username);
log.debug("File size: {}", profileImage.length);
return profileImage;
}
private void authenticate(String username, String password) {
Authentication auth = new UsernamePasswordAuthenticationToken(username, password);
authenticationManager.authenticate(auth);

View File

@ -5,6 +5,7 @@ import net.shyshkin.study.fullstack.supportportal.backend.domain.dto.UserDto;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
public interface UserService extends UserDetailsService {
@ -27,4 +28,6 @@ public interface UserService extends UserDetailsService {
User updateProfileImage(String username, MultipartFile profileImage);
byte[] getProfileImage(String username) throws IOException;
}

View File

@ -217,6 +217,14 @@ public class UserServiceImpl implements UserService {
return user;
}
@Override
public byte[] getProfileImage(String username) throws IOException {
User user = findByUsername(username);
Path userFolder = Paths
.get(USER_FOLDER, user.getUserId(), USER_IMAGE_FILENAME);
return Files.readAllBytes(userFolder);
}
private void validateNewUsernameAndEmail(String username, String email) {
if (userRepository.existsByUsername(username))

View File

@ -35,7 +35,7 @@ spring:
# resources:
# add-mappings: false
app:
public-urls: /user/login,/user/register,/user/image/**
public-urls: /user/login,/user/register,/user/*/image/**
jwt:
secret: VeRy_5ecretP@55W0rd!
# secret: ${random.value} #Does not work - every time generates new value