From 48fa01047331570de5e6fe1899981a58721aa0a7 Mon Sep 17 00:00:00 2001 From: Angelo Caporaso <56113767+cap-ang@users.noreply.github.com> Date: Thu, 28 Nov 2024 18:20:40 +0100 Subject: [PATCH] [PAGOPA-2426] refactoring: Replace file-store with input-stream --- .../repository/BlobStorageRepository.java | 22 ++++++------------- .../gpd/upload/repository/FileRepository.java | 4 ++-- .../gpd/upload/service/BlobService.java | 13 +++++------ 3 files changed, 14 insertions(+), 25 deletions(-) diff --git a/src/main/java/it/gov/pagopa/gpd/upload/repository/BlobStorageRepository.java b/src/main/java/it/gov/pagopa/gpd/upload/repository/BlobStorageRepository.java index 861021a..474237d 100644 --- a/src/main/java/it/gov/pagopa/gpd/upload/repository/BlobStorageRepository.java +++ b/src/main/java/it/gov/pagopa/gpd/upload/repository/BlobStorageRepository.java @@ -43,7 +43,7 @@ public void init() { } @Override - public String upload(String broker, String fiscalCode, File file) throws FileNotFoundException { + public String upload(String broker, String fiscalCode, InputStream inputStream) throws FileNotFoundException { blobServiceClient.createBlobContainerIfNotExists(broker); BlobContainerClient container = blobServiceClient.getBlobContainerClient(broker + "/" + fiscalCode + "/" + INPUT_DIRECTORY); String key = this.createRandomName(broker + "_" + fiscalCode); @@ -57,30 +57,23 @@ public String upload(String broker, String fiscalCode, File file) throws FileNot BlockBlobClient blockBlobClient = blobClient.getBlockBlobClient(); - CompletableFuture uploadFuture = uploadFileAsync(blockBlobClient, file); + CompletableFuture uploadFuture = uploadFileAsync(blockBlobClient, inputStream); uploadFuture.thenAccept(blobName -> { // Handle the result asynchronously - log.debug(String.format("Asynchronous upload completed for blob %s", blobName)); + log.debug("Asynchronous upload completed for blob {}", blobName); }).exceptionally(ex -> { - log.error(String.format("[Error][BlobStorageRepository@upload] Exception while uploading file %s asynchronously: %s", - file.getName(), ex.getMessage())); + log.error("[Error][BlobStorageRepository@upload] Exception while uploading file asynchronously: {}", ex.getMessage()); throw new AppException(HttpStatus.INTERNAL_SERVER_ERROR, "INTERNAL_SERVER_ERROR", "Error uploading file asynchronously", ex); }); return key; } - private CompletableFuture uploadFileAsync(BlockBlobClient blockBlobClient, File file) { + private CompletableFuture uploadFileAsync(BlockBlobClient blockBlobClient, InputStream inputStream) { return CompletableFuture.supplyAsync(() -> { try { - String blobName = this.uploadFileBlocksAsBlockBlob(blockBlobClient, file); - - if(!file.delete()) { - log.error(String.format("[Error][BlobStorageRepository@uploadFileAsync] The file %s was not deleted", file.getName())); - } - - return blobName; + return this.uploadFileBlocksAsBlockBlob(blockBlobClient, inputStream); } catch (IOException e) { throw new AppException(HttpStatus.INTERNAL_SERVER_ERROR, "INTERNAL_SERVER_ERROR", "Error uploading file asynchronously", e); } @@ -91,8 +84,7 @@ private String createRandomName(String namePrefix) { return namePrefix + "_" + UUID.randomUUID().toString().replace("-", ""); } - private String uploadFileBlocksAsBlockBlob(BlockBlobClient blockBlob, File file) throws IOException { - InputStream inputStream = new FileInputStream(file); + private String uploadFileBlocksAsBlockBlob(BlockBlobClient blockBlob, InputStream inputStream) throws IOException { ByteArrayInputStream byteInputStream = null; int blockSize = 1024 * 1024; diff --git a/src/main/java/it/gov/pagopa/gpd/upload/repository/FileRepository.java b/src/main/java/it/gov/pagopa/gpd/upload/repository/FileRepository.java index 45383af..0713db4 100644 --- a/src/main/java/it/gov/pagopa/gpd/upload/repository/FileRepository.java +++ b/src/main/java/it/gov/pagopa/gpd/upload/repository/FileRepository.java @@ -1,8 +1,8 @@ package it.gov.pagopa.gpd.upload.repository; -import java.io.File; import java.io.IOException; +import java.io.InputStream; public interface FileRepository { - String upload(String container, String directory, File file) throws IOException; + String upload(String container, String directory, InputStream inputStream) throws IOException; } diff --git a/src/main/java/it/gov/pagopa/gpd/upload/service/BlobService.java b/src/main/java/it/gov/pagopa/gpd/upload/service/BlobService.java index 9dbec50..14163ae 100644 --- a/src/main/java/it/gov/pagopa/gpd/upload/service/BlobService.java +++ b/src/main/java/it/gov/pagopa/gpd/upload/service/BlobService.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import io.micronaut.context.annotation.Context; import io.micronaut.context.annotation.Value; @@ -22,8 +23,6 @@ import jakarta.inject.Singleton; import java.io.*; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.List; import java.util.UUID; import java.util.zip.ZipEntry; @@ -162,14 +161,12 @@ public String upload(UploadInput uploadInput, String broker, String organization log.debug(String.format("Upload operation %s was launched for broker %s and organization fiscal code %s", uploadInput.getUploadOperation(), broker, organizationFiscalCode)); - // replace file content - File uploadInputFile = Files.createTempFile(Path.of(DESTINATION_DIRECTORY), "gpd_upload_temp", ".json").toFile(); - FileWriter fileWriter = new FileWriter(uploadInputFile); - fileWriter.write(objectMapper.writeValueAsString(uploadInput)); - fileWriter.close(); + // from UploadInput Object to ByteArrayInputStream + objectMapper.enable(SerializationFeature.INDENT_OUTPUT); + ByteArrayInputStream inputStream = new ByteArrayInputStream(objectMapper.writeValueAsBytes(uploadInput)); // upload blob - String fileId = blobStorageRepository.upload(broker, organizationFiscalCode, uploadInputFile); + String fileId = blobStorageRepository.upload(broker, organizationFiscalCode, inputStream); statusService.createUploadStatus(organizationFiscalCode, broker, fileId, totalItem); return fileId;