81.1 Get user temporary profile image (#9)
This commit is contained in:
@ -124,6 +124,13 @@ public class UserResource {
|
||||
return profileImage;
|
||||
}
|
||||
|
||||
@GetMapping(path = "image/profile/{userId}", produces = MediaType.IMAGE_JPEG_VALUE)
|
||||
public byte[] getDefaultProfileImage(@PathVariable String userId) {
|
||||
byte[] profileImage = userService.getDefaultProfileImage(userId);
|
||||
log.debug("File size: {}", profileImage.length);
|
||||
return profileImage;
|
||||
}
|
||||
|
||||
private void authenticate(String username, String password) {
|
||||
Authentication auth = new UsernamePasswordAuthenticationToken(username, password);
|
||||
authenticationManager.authenticate(auth);
|
||||
|
||||
@ -32,4 +32,5 @@ public interface UserService extends UserDetailsService {
|
||||
|
||||
byte[] getImageByUserId(String userId, String filename) throws IOException;
|
||||
|
||||
byte[] getDefaultProfileImage(String userId);
|
||||
}
|
||||
|
||||
@ -13,13 +13,19 @@ import net.shyshkin.study.fullstack.supportportal.backend.exception.domain.Usern
|
||||
import net.shyshkin.study.fullstack.supportportal.backend.mapper.UserMapper;
|
||||
import net.shyshkin.study.fullstack.supportportal.backend.repository.UserRepository;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.transaction.Transactional;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
@ -47,6 +53,16 @@ public class UserServiceImpl implements UserService {
|
||||
private final LoginAttemptService loginAttemptService;
|
||||
private final EmailService emailService;
|
||||
private final UserMapper userMapper;
|
||||
private final RestTemplateBuilder restTemplateBuilder;
|
||||
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@PostConstruct
|
||||
void init() {
|
||||
restTemplate = restTemplateBuilder
|
||||
.rootUri(TEMP_PROFILE_IMAGE_BASE_URL)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
@ -230,6 +246,18 @@ public class UserServiceImpl implements UserService {
|
||||
return Files.readAllBytes(userProfileImagePath);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getDefaultProfileImage(String userId) {
|
||||
// "https://robohash.org/11951691-d373-4126-bef2-84d157a6546b"
|
||||
RequestEntity<Void> requestEntity = RequestEntity
|
||||
.get("/{userId}", userId)
|
||||
.accept(MediaType.IMAGE_JPEG)
|
||||
.build();
|
||||
var responseEntity = restTemplate.exchange(requestEntity, new ParameterizedTypeReference<byte[]>() {
|
||||
});
|
||||
return responseEntity.getBody();
|
||||
}
|
||||
|
||||
private void validateNewUsernameAndEmail(String username, String email) {
|
||||
|
||||
if (userRepository.existsByUsername(username))
|
||||
|
||||
@ -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/**,/user/image/**
|
||||
jwt:
|
||||
secret: VeRy_5ecretP@55W0rd!
|
||||
# secret: ${random.value} #Does not work - every time generates new value
|
||||
|
||||
@ -752,6 +752,26 @@ class UserResourceUnSecureTest extends BaseUserTest {
|
||||
assertThat(responseEntity.getBody()).hasSize(52);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void getDefaultProfileImage_correct() throws IOException {
|
||||
|
||||
//given
|
||||
String userId = user.getUserId();
|
||||
|
||||
//when
|
||||
RequestEntity<Void> requestEntity = RequestEntity.get("/user/image/profile/{userId}",userId)
|
||||
.accept(MediaType.IMAGE_JPEG)
|
||||
.build();
|
||||
var responseEntity = restTemplate.exchange(requestEntity, new ParameterizedTypeReference<byte[]>() {
|
||||
});
|
||||
|
||||
//then
|
||||
log.debug("Response Entity: {}", responseEntity);
|
||||
assertThat(responseEntity.getStatusCode()).isEqualTo(OK);
|
||||
assertThat(responseEntity.getBody()).hasSizeGreaterThan(52);
|
||||
}
|
||||
|
||||
private void uploadProfileImage(String username) throws IOException {
|
||||
|
||||
MultipartFile profileImage = new MockMultipartFile("profileImage", "test.txt",
|
||||
|
||||
Reference in New Issue
Block a user