Skip to content

Commit

Permalink
Fix/tika quarkus (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
candreac authored Dec 3, 2023
1 parent b87578d commit 172c933
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 34 deletions.
13 changes: 8 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<mockito.core.version>5.4.0</mockito.core.version>
<easy.random.version>5.0.0</easy.random.version>
<commons-io.version>2.15.0</commons-io.version>
<tika-core.version>2.9.1</tika-core.version>
<quarkus-tika.version>2.0.3</quarkus-tika.version>
</properties>
<dependencyManagement>
<dependencies>
Expand All @@ -49,11 +49,14 @@
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>${tika-core.version}</version>
<groupId>io.quarkiverse.tika</groupId>
<artifactId>quarkus-tika</artifactId>
<version>${quarkus-tika.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-awt</artifactId>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-jackson</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ public Uni<BpmnVersion> 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);
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -117,10 +119,10 @@ public Uni<ResourceEntity> 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 -> {
Expand Down Expand Up @@ -152,10 +154,10 @@ public Uni<ResourceEntity> 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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(".", "");
}

}

0 comments on commit 172c933

Please sign in to comment.