Fix tests and profile picture
This commit is contained in:
@@ -3,49 +3,39 @@ package SuMoC.backend.gamification.badges.mapper;
|
||||
import SuMoC.backend.gamification.badges.models.Badge;
|
||||
import SuMoC.backend.gamification.badges.models.BadgeDTO;
|
||||
import SuMoC.backend.gamification.badges.models.BadgeImageEntity;
|
||||
import SuMoC.backend.images.ImageBase64Util;
|
||||
import org.mapstruct.Mapper;
|
||||
|
||||
import java.util.Base64;
|
||||
|
||||
@Mapper
|
||||
public class BadgeMapper {
|
||||
public interface BadgeMapper {
|
||||
|
||||
public static BadgeDTO toDTO(Badge badge) {
|
||||
static BadgeDTO toDTO(Badge badge) {
|
||||
BadgeDTO dto = new BadgeDTO();
|
||||
dto.setId(badge.getId());
|
||||
dto.setName(badge.getName());
|
||||
dto.setDescription(badge.getDescription());
|
||||
dto.setCategory(badge.getCategory());
|
||||
dto.setCriteria(badge.getCriteria());
|
||||
|
||||
// Base64-Encoding des Bildes
|
||||
if (badge.getImage() != null && badge.getImage().getImageData() != null) {
|
||||
String base64Image = Base64.getEncoder().encodeToString(badge.getImage().getImageData());
|
||||
dto.setImageBase64(base64Image);
|
||||
}
|
||||
dto.setImageBase64(ImageBase64Util.encode(badge.getImage().getImageData()));
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
public static Badge toEntity(BadgeDTO dto) {
|
||||
static Badge toEntity(BadgeDTO dto) {
|
||||
Badge badge = new Badge();
|
||||
badge.setId(dto.getId());
|
||||
badge.setName(dto.getName());
|
||||
badge.setDescription(dto.getDescription());
|
||||
badge.setCategory(dto.getCategory());
|
||||
badge.setCriteria(dto.getCriteria());
|
||||
|
||||
// Base64-Decoding des Bildes
|
||||
if (dto.getImageBase64() != null && !dto.getImageBase64().isEmpty()) {
|
||||
byte[] imageData = Base64.getDecoder().decode(dto.getImageBase64());
|
||||
BadgeImageEntity imageEntity = new BadgeImageEntity(badge, imageData);
|
||||
badge.setImage(imageEntity);
|
||||
}
|
||||
badge.setImage(new BadgeImageEntity(badge, ImageBase64Util.decode(dto.getImageBase64())));
|
||||
|
||||
return badge;
|
||||
}
|
||||
|
||||
public static void updateEntityFromDTO(Badge badge, BadgeDTO dto) {
|
||||
static void updateEntityFromDTO(Badge badge, BadgeDTO dto) {
|
||||
badge.setName(dto.getName());
|
||||
badge.setDescription(dto.getDescription());
|
||||
badge.setCategory(dto.getCategory());
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package SuMoC.backend.images;
|
||||
|
||||
import java.util.Base64;
|
||||
|
||||
public final class ImageBase64Util {
|
||||
|
||||
private ImageBase64Util() {
|
||||
}
|
||||
|
||||
public static String encode(byte[] imageData) {
|
||||
if (imageData == null || imageData.length == 0) {
|
||||
return null;
|
||||
}
|
||||
return Base64.getEncoder().encodeToString(imageData);
|
||||
}
|
||||
|
||||
public static byte[] decode(String base64Image) {
|
||||
if (base64Image == null || base64Image.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return Base64.getDecoder().decode(base64Image);
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,6 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -168,23 +167,23 @@ public class UserController {
|
||||
content = @Content(mediaType = "application/json"))
|
||||
})
|
||||
@PutMapping("/setProfilePicture/{id}")
|
||||
public ResponseEntity<Void> setProfilePicture(@RequestParam MultipartFile profilePicture,
|
||||
public ResponseEntity<Void> setProfilePicture(@RequestParam String base64ProfilePicture,
|
||||
@PathVariable @NotNull Long id) {
|
||||
this.userService.setProfilePicture(id, profilePicture);
|
||||
this.userService.setProfilePicture(id, base64ProfilePicture);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@Operation(summary = "Get the profile Picture of the user.", description = "A user can get their displayed profile picture.")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "200", description = "Profile picture successfully retrieved",
|
||||
content = @Content(mediaType = "image/jpeg")),
|
||||
content = @Content(mediaType = "application/json")),
|
||||
@ApiResponse(responseCode = "304", description = "Profile picture not modified since last request"),
|
||||
@ApiResponse(responseCode = "403", description = "Only authorized persons may see the profiles picture"),
|
||||
@ApiResponse(responseCode = "404", description = "The user does not exist")
|
||||
})
|
||||
@GetMapping("/getProfilePicture/{id}")
|
||||
public ResponseEntity<byte[]> getProfilePicture(@PathVariable @NotNull Long id) {
|
||||
byte[] imageData = userService.getProfilePicture(id);
|
||||
public ResponseEntity<String> getProfilePicture(@PathVariable @NotNull Long id) {
|
||||
String imageData = userService.getProfilePicture(id);
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.IMAGE_JPEG)
|
||||
|
||||
@@ -21,11 +21,12 @@ public class MaximalUserDTO {
|
||||
private Set<Trip> trips;
|
||||
|
||||
private Set<Friendship> friendships;
|
||||
private String base64ProfilePicture;
|
||||
|
||||
public MaximalUserDTO() {
|
||||
}
|
||||
|
||||
public MaximalUserDTO(Long id, String publicName, String privateName, String email, VisibilityPrivacy visibilityPrivacy, Set<Trip> trips, Set<Friendship> friendships) {
|
||||
public MaximalUserDTO(Long id, String publicName, String privateName, String email, VisibilityPrivacy visibilityPrivacy, Set<Trip> trips, Set<Friendship> friendships, String base64ProfilePicture) {
|
||||
this.id = id;
|
||||
this.publicName = publicName;
|
||||
this.privateName = privateName;
|
||||
@@ -33,6 +34,7 @@ public class MaximalUserDTO {
|
||||
this.visibilityPrivacy = visibilityPrivacy;
|
||||
this.trips = trips;
|
||||
this.friendships = friendships;
|
||||
this.base64ProfilePicture = base64ProfilePicture;
|
||||
}
|
||||
|
||||
|
||||
@@ -91,4 +93,12 @@ public class MaximalUserDTO {
|
||||
public void setFriendships(Set<Friendship> friendships) {
|
||||
this.friendships = friendships;
|
||||
}
|
||||
|
||||
public String getBase64ProfilePicture() {
|
||||
return base64ProfilePicture;
|
||||
}
|
||||
|
||||
public void setBase64ProfilePicture(String base64ProfilePicture) {
|
||||
this.base64ProfilePicture = base64ProfilePicture;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package SuMoC.backend.user.mapper;
|
||||
|
||||
|
||||
import SuMoC.backend.images.ImageBase64Util;
|
||||
import SuMoC.backend.user.dtos.MaximalUserDTO;
|
||||
import SuMoC.backend.user.dtos.MinimalUserDTO;
|
||||
import SuMoC.backend.user.dtos.UserDTO;
|
||||
@@ -13,7 +14,20 @@ public interface UserMapper {
|
||||
|
||||
User toUser(UserDTO userDTO);
|
||||
|
||||
MaximalUserDTO toMaximalUserDTO(User user);
|
||||
static MaximalUserDTO toMaximalUserDTO(User user) {
|
||||
MaximalUserDTO maximalUserDTO = new MaximalUserDTO(
|
||||
user.getId(),
|
||||
user.getPublicName(),
|
||||
user.getPrivateName(),
|
||||
user.getEmail(),
|
||||
user.getVisibilityPrivacy(),
|
||||
user.getTrips(),
|
||||
user.getFriendships(),
|
||||
ImageBase64Util.encode(user.getProfileImage().getImageData())
|
||||
);
|
||||
return maximalUserDTO;
|
||||
}
|
||||
|
||||
|
||||
MinimalUserDTO toMinimalUserDTO(User user);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package SuMoC.backend.user.service;
|
||||
|
||||
import SuMoC.backend.images.ImageBase64Util;
|
||||
import SuMoC.backend.security.dtos.LoginResponseDTO;
|
||||
import SuMoC.backend.security.services.TokenService;
|
||||
import SuMoC.backend.trip.TripMapper;
|
||||
@@ -201,10 +202,10 @@ public class UserService {
|
||||
|
||||
public MaximalUserDTO getSelf() {
|
||||
User sender = currentUser.get();
|
||||
return userMapper.toMaximalUserDTO(sender);
|
||||
return UserMapper.toMaximalUserDTO(sender);
|
||||
}
|
||||
|
||||
public void setProfilePicture(Long id, MultipartFile profilePicture) {
|
||||
public void setProfilePicture(Long id, String base64ProfilePicture) {
|
||||
User sender = currentUser.get();
|
||||
if (!sender.getId().equals(id) && !sender.getRoles().contains(RoleName.ADMIN)) {
|
||||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Ein Nutzer kann nur seinen eigenen privaten Namen ändern.");
|
||||
@@ -215,7 +216,7 @@ public class UserService {
|
||||
HttpStatus.NOT_FOUND,
|
||||
"Der Nutzer Existitert nicht."));
|
||||
try {
|
||||
userToChange.getProfileImage().setImageData(profilePicture.getBytes());
|
||||
userToChange.getProfileImage().setImageData(ImageBase64Util.decode(base64ProfilePicture));
|
||||
} catch (Exception e) {
|
||||
throw new ResponseStatusException(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
@@ -226,7 +227,7 @@ public class UserService {
|
||||
|
||||
}
|
||||
|
||||
public byte[] getProfilePicture(Long id) {
|
||||
public String getProfilePicture(Long id) {
|
||||
User sender = currentUser.get();
|
||||
User user = userRepository.findById(id)
|
||||
.orElseThrow(() -> new ResponseStatusException(
|
||||
@@ -242,6 +243,6 @@ public class UserService {
|
||||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Kein Profilbild vorhanden.");
|
||||
}
|
||||
|
||||
return user.getProfileImage().getImageData();
|
||||
return ImageBase64Util.encode(user.getProfileImage().getImageData());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user