Skip to content

Commit

Permalink
[PAGOPA-2426] refactoring: Replace file-store with input-stream
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-ang committed Nov 28, 2024
1 parent 4cbdedb commit 48fa010
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -57,30 +57,23 @@ public String upload(String broker, String fiscalCode, File file) throws FileNot

BlockBlobClient blockBlobClient = blobClient.getBlockBlobClient();

CompletableFuture<String> uploadFuture = uploadFileAsync(blockBlobClient, file);
CompletableFuture<String> 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<String> uploadFileAsync(BlockBlobClient blockBlobClient, File file) {
private CompletableFuture<String> 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);
}
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
13 changes: 5 additions & 8 deletions src/main/java/it/gov/pagopa/gpd/upload/service/BlobService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 48fa010

Please sign in to comment.