From 172c9334c4495630650654ea97fbfcb215281513 Mon Sep 17 00:00:00 2001 From: AndreaC <94940911+candreac@users.noreply.github.com> Date: Sun, 3 Dec 2023 10:28:15 +0100 Subject: [PATCH] Fix/tika quarkus (#80) --- pom.xml | 13 ++++--- .../service/model/docker/Dockerfile.native | 34 +++++++++++++++- .../service/impl/BpmnVersionServiceImpl.java | 3 +- .../impl/ResourceEntityServiceImpl.java | 18 +++++---- .../service/model/utils/FileUtilities.java | 39 ++++++++++--------- 5 files changed, 73 insertions(+), 34 deletions(-) diff --git a/pom.xml b/pom.xml index 0817a2c2..43b1a6fd 100644 --- a/pom.xml +++ b/pom.xml @@ -27,7 +27,7 @@ 5.4.0 5.0.0 2.15.0 - 2.9.1 + 2.0.3 @@ -49,11 +49,14 @@ - org.apache.tika - tika-core - ${tika-core.version} + io.quarkiverse.tika + quarkus-tika + ${quarkus-tika.version} + + + io.quarkus + quarkus-awt - io.quarkus quarkus-resteasy-reactive-jackson diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/docker/Dockerfile.native b/src/main/java/it/gov/pagopa/atmlayer/service/model/docker/Dockerfile.native index 58acbfdb..0fe98e89 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/docker/Dockerfile.native +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/docker/Dockerfile.native @@ -1,3 +1,7 @@ +# Stage 1: Set up environment for POI +FROM registry.access.redhat.com/ubi8/ubi-minimal:8.9 as POI +RUN microdnf update && microdnf install -y freetype fontconfig && microdnf clean all +# Stage 2: BUILD FROM quay.io/quarkus/ubi-quarkus-graalvmce-builder-image:22.3-java17 AS build COPY --chown=quarkus:quarkus mvnw /code/mvnw COPY --chown=quarkus:quarkus .mvn /code/.mvn @@ -10,8 +14,36 @@ ARG QUARKUS_PROFILE ARG APP_NAME RUN ./mvnw package -Pnative -Dquarkus.application.name=atm-layer-model -Dquarkus.profile=prod -DskipTests -## Stage 2 : create the docker final image +# Stage 3 : create the docker final image FROM quay.io/quarkus/quarkus-micro-image:2.0 + +COPY --from=POI \ + /lib64/libfreetype.so.6 \ + /lib64/libgcc_s.so.1 \ + /lib64/libbz2.so.1 \ + /lib64/libpng16.so.16 \ + /lib64/libm.so.6 \ + /lib64/libbz2.so.1 \ + /lib64/libexpat.so.1 \ + /lib64/libuuid.so.1 \ + /lib64/ + +COPY --from=POI \ + /usr/lib64/libfontconfig.so.1 \ + /usr/lib64/ + +COPY --from=POI \ + /usr/share/fonts /usr/share/fonts + +COPY --from=POI \ + /usr/share/fontconfig /usr/share/fontconfig + +COPY --from=POI \ + /usr/lib/fontconfig /usr/lib/fontconfig + +COPY --from=POI \ + /etc/fonts /etc/fonts + WORKDIR /work/ COPY --from=build /code/target/*-runner /work/application diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/BpmnVersionServiceImpl.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/BpmnVersionServiceImpl.java index 69d1e56b..3d0193a8 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/BpmnVersionServiceImpl.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/BpmnVersionServiceImpl.java @@ -203,7 +203,8 @@ public Uni saveAndUpload(BpmnVersion bpmnVersion, File file, String log.error(failure.getMessage()); return Uni.createFrom().failure(new AtmLayerException("Failed to save BPMN in Object Store. BPMN creation aborted", Response.Status.INTERNAL_SERVER_ERROR, OBJECT_STORE_SAVE_FILE_ERROR)); }) - .onItem().transformToUni(putObjectResponse -> { + .onItem().transformToUni(resourceFile -> { + element.setResourceFile(resourceFile); log.info("Completed BPMN Creation"); return Uni.createFrom().item(element); })); diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/ResourceEntityServiceImpl.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/ResourceEntityServiceImpl.java index 4cfd110d..e2098bfa 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/ResourceEntityServiceImpl.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/ResourceEntityServiceImpl.java @@ -34,6 +34,8 @@ import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.RESOURCE_DOES_NOT_EXIST; import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.RESOURCE_WITH_SAME_SHA256_ALREADY_EXISTS; import static it.gov.pagopa.atmlayer.service.model.utils.FileUtilities.calculateSha256; +import static it.gov.pagopa.atmlayer.service.model.utils.FileUtilities.getExtension; +import static it.gov.pagopa.atmlayer.service.model.utils.FileUtilities.isExtensionValid; @ApplicationScoped @Slf4j @@ -117,10 +119,10 @@ public Uni createResource(ResourceEntity resourceEntity, File fi throw new AtmLayerException(String.format("Cannot upload %s: resource with same file name and path already exists", resourceEntity.getStorageKey()), Response.Status.BAD_REQUEST, AppErrorCodeEnum.RESOURCE_WITH_SAME_NAME_AND_PATH_ALREADY_SAVED); } -// if (!isExtensionValid(file, filename)) { -// throw new AtmLayerException(String.format("Cannot upload file: the extension %s doesn't match with the provided filename %s", -// getExtension(file), filename), Response.Status.BAD_REQUEST, AppErrorCodeEnum.RESOURCE_WITH_SAME_NAME_AND_PATH_ALREADY_SAVED); -// } + if (!isExtensionValid(file, filename)) { + throw new AtmLayerException(String.format("Cannot upload file: the extension %s doesn't match with the provided filename %s", + getExtension(file), filename), Response.Status.BAD_REQUEST, AppErrorCodeEnum.RESOURCE_WITH_SAME_NAME_AND_PATH_ALREADY_SAVED); + } return saveAndUpload(resourceEntity, file, filename, path) .onItem().transformToUni(bpmn -> this.findByUUID(resourceEntity.getResourceId()) .onItem().transformToUni(optionalResource -> { @@ -152,10 +154,10 @@ public Uni updateResource(UUID uuid, File file) { } String fileNameDb = resourceEntity.getFileName(); String extensionDb = FilenameUtils.getExtension(fileNameDb); -// if (!Objects.equals(extensionDb, getExtension(file))) { -// throw new AtmLayerException(String.format("Cannot upload file: the extension %s doesn't match with the file you are trying to update: %s", -// getExtension(file), fileNameDb), Response.Status.BAD_REQUEST, AppErrorCodeEnum.RESOURCE_WITH_SAME_NAME_AND_PATH_ALREADY_SAVED); -// } + if (!Objects.equals(extensionDb, getExtension(file))) { + throw new AtmLayerException(String.format("Cannot upload file: the extension %s doesn't match with the file you are trying to update: %s", + getExtension(file), fileNameDb), Response.Status.BAD_REQUEST, AppErrorCodeEnum.RESOURCE_WITH_SAME_NAME_AND_PATH_ALREADY_SAVED); + } resourceEntity.setSha256(newFileSha256); Date date = new Date(); resourceEntity.setLastUpdatedAt(new Timestamp(date.getTime())); diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/utils/FileUtilities.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/utils/FileUtilities.java index c0158dc9..f6cf3bd7 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/utils/FileUtilities.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/utils/FileUtilities.java @@ -97,24 +97,25 @@ public static String toHexString(byte[] hash) { return hexString.toString(); } -// public static boolean isExtensionValid(File file, String fileName) throws IOException, MimeTypeException { -// String detectedExtension = getExtension(file); -// String extension = FilenameUtils.getExtension(fileName); -// if (Objects.equals(extension, "bpmn") || Objects.equals(extension, "dmn")) { -// extension = UtilityValues.XML_EXTENSION.getValue(); -// } -// if (Objects.equals(extension, "form")) { -// extension = UtilityValues.TXT_EXTENSION.getValue(); -// } -// return Objects.equals(extension, detectedExtension); -// } -// -// public static String getExtension(File file) throws IOException, MimeTypeException { -// Tika tika = new Tika(); -// String detectedType = tika.detect(file); -// MimeTypes allTypes = MimeTypes.getDefaultMimeTypes(); -// MimeType type = allTypes.forName(detectedType); -// return type.getExtension().replace(".", ""); -// } + public static boolean isExtensionValid(File file, String fileName) throws IOException, MimeTypeException { + String detectedExtension = getExtension(file); + String extension = FilenameUtils.getExtension(fileName); + if (Objects.equals(extension, "bpmn") || Objects.equals(extension, "dmn")) { + extension = UtilityValues.XML_EXTENSION.getValue(); + } + if (Objects.equals(extension, "form")) { + extension = UtilityValues.TXT_EXTENSION.getValue(); + } + return Objects.equals(extension, detectedExtension); + } + + public static String getExtension(File file) throws IOException, MimeTypeException { + Tika tika = new Tika(); + String mimeType = tika.detect(file); + log.info("Detected mimeType: {}", mimeType); + MimeTypes allTypes = MimeTypes.getDefaultMimeTypes(); + MimeType type = allTypes.forName(mimeType); + return type.getExtension().replace(".", ""); + } }