Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/get create profile #4

Merged
merged 4 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.github.onecx.user.profile.domain.config;

import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;
import io.smallrye.config.WithName;

@ConfigMapping(prefix = "onecx.identity.token")
public interface TokenConfig {

@WithName("header.name")
@WithDefault("apm-principal-token")
String headerName();

@WithName("claim.display.name")
@WithDefault("name")
String displayName();

@WithName("claim.email")
@WithDefault("email")
String email();

@WithName("claim.first.name")
@WithDefault("given_name")
String firstName();

@WithName("claim.last.name")
@WithDefault("family_name")
String lastName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @author bkalas
*/
@Entity
@Table(name = "USM_PREFERENCE", indexes = { @Index(columnList = "USER_ID", name = "preferences_user_id_idx") })
@Table(name = "USM_PREFERENCE", indexes = { @Index(columnList = "USER_ID, TENANT_ID", name = "preferences_user_id_idx") })
@Getter
@Setter
@SuppressWarnings("java:S2160")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
@Entity
@Table(name = "USM_USER_PROFILE", uniqueConstraints = {
@UniqueConstraint(columnNames = { "USER_ID" })
@UniqueConstraint(columnNames = { "USER_ID", "TENANT_ID" })
}, indexes = {
@Index(columnList = "FIRST_NAME,LAST_NAME,EMAIL, TENANT_ID", name = "user_person_criteria_idx") })
@NamedEntityGraph(name = "UserProfile.loadById", attributeNodes = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package io.github.onecx.user.profile.rs.external.v1.controllers;

import java.io.*;
import java.net.URLConnection;
import java.nio.file.Files;

import javax.imageio.ImageIO;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
Expand All @@ -13,18 +9,14 @@
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.tkit.quarkus.context.ApplicationContext;
import org.tkit.quarkus.log.cdi.LogService;

import gen.io.github.onecx.user.profile.rs.external.v1.AvatarV1Api;
import io.github.onecx.user.profile.domain.daos.ImageDAO;
import io.github.onecx.user.profile.domain.daos.UserProfileDAO;
import io.github.onecx.user.profile.domain.models.Image;
import io.github.onecx.user.profile.domain.models.UserProfile;
import io.github.onecx.user.profile.rs.external.v1.mappers.AvatarV1Mapper;
import io.github.onecx.user.profile.rs.external.v1.mappers.V1ExceptionMapper;
import io.github.onecx.user.profile.rs.external.v1.service.ImageUtilService;
import lombok.extern.slf4j.Slf4j;

@LogService
Expand All @@ -39,36 +31,12 @@ public class AvatarV1RestController implements AvatarV1Api {
@Inject
ImageDAO imageDAO;

@Inject
V1ExceptionMapper exceptionMapper;

@Inject
AvatarV1Mapper avatarV1Mapper;

@Context
UriInfo uriInfo;

@ConfigProperty(name = "avatar.small.height", defaultValue = "150")
Integer smallImgHeight;

@ConfigProperty(name = "avatar.small.width", defaultValue = "150")
Integer smallImgWidth;

@Override
@Transactional
public Response deleteUserAvatar() {
var userProfile = userProfileDAO.getUserProfileByUserId(ApplicationContext.get().getPrincipal(),
UserProfile.ENTITY_GRAPH_LOAD_ALL);

if (userProfile != null) {
userProfile.setAvatar(null);
userProfile.setSmallAvatar(null);
userProfileDAO.update(userProfile);
}

return Response.noContent().build();
}

@Override
@Transactional
public Response getUserAvatar(String id) {
Expand Down Expand Up @@ -110,70 +78,4 @@ public Response getUserAvatarInfo() {
return Response.ok(imageInfo).build();
}

@Override
public Response uploadAvatar(File body) {
var userProfile = userProfileDAO.getUserProfileByUserId(ApplicationContext.get().getPrincipal(),
UserProfile.ENTITY_GRAPH_LOAD_ALL);

if (userProfile == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}

try {
byte[] avatarBytes = Files.readAllBytes(body.toPath());
InputStream avatarIs = new ByteArrayInputStream(avatarBytes);
var avatarContentType = URLConnection.guessContentTypeFromStream(avatarIs);

var smallAvatarBytes = this.convertToSmallImage(avatarBytes);

var avatar = updateUserAvatar(userProfile, avatarBytes, smallAvatarBytes, avatarContentType);

userProfile = userProfileDAO.getUserProfileByUserId(ApplicationContext.get().getPrincipal(),
UserProfile.ENTITY_GRAPH_LOAD_ALL);

var imageInfo = avatarV1Mapper.map(userProfile.getAvatar());
imageInfo.setUserUploaded(true);
imageInfo.setImageUrl(uriInfo.getPath()
.concat("/")
.concat(avatar.getId()));

imageInfo.setSmallImageUrl(uriInfo.getPath()
.concat("/")
.concat(userProfile.getSmallAvatar().getId()));

return Response.ok(imageInfo).build();
} catch (Exception ioe) {
var e = exceptionMapper.exception("UPLOAD_ERROR", ioe.getMessage());
return Response.status(Response.Status.BAD_REQUEST).entity(e).build();
}
}

private Image updateUserAvatar(UserProfile userProfile, byte[] avatarBytes, byte[] smallAvatarBytes, String mimeType)
throws IOException {
var avatar = new Image();
userProfile.setAvatar(avatar);
avatar.setImageByte(avatarBytes);
avatar.setMimeType(mimeType);
this.setUpAvatarDimensions(avatar);

var smallAvatar = new Image();
userProfile.setSmallAvatar(smallAvatar);
smallAvatar.setImageByte(smallAvatarBytes);
smallAvatar.setMimeType("image/png");
this.setUpAvatarDimensions(smallAvatar);
userProfile.setSmallAvatar(smallAvatar);

userProfileDAO.update(userProfile);
return avatar;
}

private byte[] convertToSmallImage(byte[] imgBytesArray) throws IOException {
return ImageUtilService.resizeImage(imgBytesArray, smallImgWidth, smallImgHeight);
}

private void setUpAvatarDimensions(Image avatar) throws IOException {
var image = ImageIO.read(new ByteArrayInputStream(avatar.getImageByte()));
avatar.setHeight(image.getHeight());
avatar.setWidth(image.getWidth());
}
}
Loading
Loading