Skip to content

Commit

Permalink
feat: add image avatar api (#14)
Browse files Browse the repository at this point in the history
Co-authored-by: milan.horvath <[email protected]>
  • Loading branch information
milanhorvath and milan.horvath authored Mar 20, 2024
1 parent c9d7482 commit bb56393
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 135 deletions.
44 changes: 31 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,6 @@
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<executions>
<execution>
<id>internal</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>src/main/openapi/openapi-bff.yaml</inputSpec>
<apiPackage>gen.org.tkit.onecx.user.profile.bff.rs.internal</apiPackage>
<modelPackage>gen.org.tkit.onecx.user.profile.bff.rs.internal.model</modelPackage>
</configuration>
</execution>
</executions>
<configuration>
<additionalProperties>onecx-permissions=true</additionalProperties>
<generatorName>jaxrs-spec</generatorName>
Expand Down Expand Up @@ -147,6 +134,22 @@
<java17>true</java17>
</configOptions>
</configuration>
<executions>
<execution>
<id>internal</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>src/main/openapi/openapi-bff.yaml</inputSpec>
<apiPackage>gen.org.tkit.onecx.user.profile.bff.rs.internal</apiPackage>
<modelPackage>gen.org.tkit.onecx.user.profile.bff.rs.internal.model</modelPackage>
<generateModelTests>true</generateModelTests>
<generateModelDocumentation>true</generateModelDocumentation>
<typeMappings>File=byte[]</typeMappings>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.googlecode.maven-download-plugin</groupId>
Expand All @@ -167,6 +170,21 @@
<skipCache>true</skipCache>
</configuration>
</execution>
<execution>
<id>image-user-profile-svc-internal</id>
<phase>generate-resources</phase>
<goals>
<goal>wget</goal>
</goals>
<configuration>
<uri>
https://raw.githubusercontent.com/onecx/onecx-user-profile-svc/main/src/main/openapi/onecx-image-internal-openapi.yaml
</uri>
<outputDirectory>target/tmp/openapi</outputDirectory>
<outputFileName>onecx-image-internal-openapi.yaml</outputFileName>
<skipCache>true</skipCache>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package org.tkit.onecx.user.profile.bff.rs.controllers;

import java.io.File;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.Response;

import org.eclipse.microprofile.rest.client.inject.RestClient;
Expand All @@ -14,9 +13,10 @@
import org.tkit.onecx.user.profile.bff.rs.mappers.UserProfileMapper;
import org.tkit.quarkus.log.cdi.LogService;

import gen.org.tkit.onecx.user.profile.bff.clients.api.AvatarApi;
import gen.org.tkit.onecx.user.profile.bff.clients.model.ImageInfo;
import gen.org.tkit.onecx.image.bff.clients.api.ImagesInternalApi;
import gen.org.tkit.onecx.image.bff.clients.model.ImageInfo;
import gen.org.tkit.onecx.user.profile.bff.rs.internal.UserAvatarApiService;
import gen.org.tkit.onecx.user.profile.bff.rs.internal.model.RefTypeDTO;

@ApplicationScoped
@Transactional(value = Transactional.TxType.NOT_SUPPORTED)
Expand All @@ -25,50 +25,64 @@ public class UserAvatarRestController implements UserAvatarApiService {

@Inject
@RestClient
AvatarApi client;
ImagesInternalApi client;

@Inject
UserProfileMapper mapper;

@Inject
ExceptionMapper exceptionMapper;

@ServerExceptionMapper
public Response restException(WebApplicationException ex) {
return exceptionMapper.restException(ex);
}

@Override
public Response deleteUserAvatar() {
try (Response response = client.deleteUserProfileAvatar()) {
public Response deleteUserAvatar(RefTypeDTO refType) {
try (Response response = client.deleteMyImage(mapper.map(refType))) {
return Response.status(response.getStatus()).build();
}
}

@Override
public Response getUserAvatar(String id) {
try (Response response = client.getUserProfileAvatar(id)) {
var byteResponse = response.readEntity(byte[].class);
return Response.status(response.getStatus())
.entity(byteResponse).build();
public Response getUserAvatar(RefTypeDTO refType) {
Response.ResponseBuilder responseBuilder = null;
try (Response response = client.getMyImage(mapper.map(refType))) {
var contentType = response.getHeaderString(HttpHeaders.CONTENT_TYPE);
var contentLength = response.getHeaderString(HttpHeaders.CONTENT_LENGTH);
var body = response.readEntity(byte[].class);

if (contentType != null && body.length != 0) {
responseBuilder = Response.status(response.getStatus())
.header(HttpHeaders.CONTENT_TYPE, contentType)
.header(HttpHeaders.CONTENT_LENGTH, contentLength)
.entity(body);
} else {
responseBuilder = Response.status(Response.Status.BAD_REQUEST);
}

return responseBuilder.build();
}

}

@Override
public Response getUserAvatarInfo() {
try (Response response = client.getUserProfileAvatarInfo()) {
public Response updateAvatar(byte[] body, Integer contentLength, RefTypeDTO refType) {
try (Response response = client.updateMyImage(body, contentLength, mapper.map(refType))) {
var imageInfo = response.readEntity(ImageInfo.class);

return Response.status(response.getStatus())
.entity(mapper.map(imageInfo)).build();
}
}

@Override
public Response uploadAvatar(File body) {
try (Response response = client.uploadUserProfileAvatar(body)) {
public Response uploadAvatar(byte[] body, Integer contentLength, RefTypeDTO refType) {
try (Response response = client.uploadMyImage(contentLength, body, mapper.map(refType))) {
var imageInfo = response.readEntity(ImageInfo.class);
return Response.status(response.getStatus())
.entity(mapper.map(imageInfo)).build();
}
}

@ServerExceptionMapper
public Response restException(WebApplicationException ex) {
return exceptionMapper.restException(ex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.mapstruct.Mapping;
import org.tkit.quarkus.rs.mappers.OffsetDateTimeMapper;

import gen.org.tkit.onecx.image.bff.clients.model.ImageInfo;
import gen.org.tkit.onecx.image.bff.clients.model.RefType;
import gen.org.tkit.onecx.user.profile.bff.clients.model.*;
import gen.org.tkit.onecx.user.profile.bff.rs.internal.model.*;

Expand Down Expand Up @@ -34,5 +36,7 @@ public interface UserProfileMapper {

UpdateUserSettings map(UpdateUserSettingsDTO updateUserSettingsDTO);

RefType map(RefTypeDTO imageInfo);

ImageInfoDTO map(ImageInfo imageInfo);
}
118 changes: 70 additions & 48 deletions src/main/openapi/openapi-bff.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,23 @@ paths:
- read
tags:
- userAvatar
description: Get user avatar info
operationId: getUserAvatarInfo
description: Get user avatar image
operationId: getUserAvatar
parameters:
- name: refType
in: query
schema:
$ref: "#/components/schemas/RefType"
responses:
"200":
description: OK
content:
application/json:
image/*:
schema:
$ref: '#/components/schemas/ImageInfo'
minimum: 1
maximum: 110000
type: string
format: binary
"404":
description: User avatar not found
put:
Expand All @@ -290,16 +298,28 @@ paths:
- write
tags:
- userAvatar
description: Upload user avatar
operationId: uploadAvatar
description: Update user avatar
operationId: updateAvatar
parameters:
- in: header
name: Content-Length
schema:
type: integer
minimum: 1
maximum: 110000
- name: refType
in: query
schema:
$ref: "#/components/schemas/RefType"
requestBody:
required: true
content:
'image/*':
schema:
minimum: 1
maximum: 110000
type: string
format: binary
maxLength: 10
responses:
'200':
description: OK
Expand All @@ -313,52 +333,71 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ProblemDetailResponse'
delete:
post:
x-onecx:
permissions:
user-profile:
- delete
- write
tags:
- userAvatar
description: Delete user's avatar
operationId: deleteUserAvatar
description: Upload user avatar
operationId: uploadAvatar
parameters:
- in: header
name: Content-Length
schema:
type: integer
minimum: 1
maximum: 110000
- name: refType
in: query
schema:
$ref: "#/components/schemas/RefType"
requestBody:
required: true
content:
'image/*':
schema:
minimum: 1
maximum: 110000
type: string
format: binary
responses:
'204':
description: No Content
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/ImageInfo'
"400":
description: Bad request
content:
application/json:
schema:
$ref: '#/components/schemas/ProblemDetailResponse'
/userProfile/me/avatar/{id}:
get:
delete:
x-onecx:
permissions:
user-profile:
- read
- delete
tags:
- userAvatar
description: Get the image of user's avatar
operationId: getUserAvatar
description: Delete user's avatar
operationId: deleteUserAvatar
parameters:
- $ref: '#/components/parameters/id'
- name: refType
in: query
schema:
$ref: "#/components/schemas/RefType"
responses:
"200":
description: OK
content:
application/octet-stream:
schema:
type: string
format: binary
'204':
description: No Content
"400":
description: Bad request
content:
application/json:
schema:
$ref: '#/components/schemas/ProblemDetailResponse'
"404":
description: Not found
/userProfiles/search:
post:
x-onecx:
Expand Down Expand Up @@ -683,31 +722,14 @@ components:
- LIGHT
- DARK
type: string
RefType:
type: string
enum: [ small, normal ]
ImageInfo:
type: object
properties:
modificationCount:
format: int32
type: integer
creationDate:
$ref: '#/components/schemas/OffsetDateTime'
creationUser:
type: string
modificationDate:
$ref: '#/components/schemas/OffsetDateTime'
modificationUser:
type: string
id:
type: string
userUploaded:
description: flag whether Image was uploaded by user
type: boolean
imageUrl:
type: string
example: http://tkit-portal/data/afcc5d0d-6509-497a-8125-614f82b106ae
smallImageUrl:
type: string
example: http://tkit-portal/data/afcc5d0d-6509-497a-8125-614f82asb106ae
ProblemDetailResponse:
type: object
properties:
Expand Down
Loading

0 comments on commit bb56393

Please sign in to comment.