Skip to content

Commit

Permalink
Merge pull request #14 from pagopa/hotfix-zip-function
Browse files Browse the repository at this point in the history
Hotfix zip function
  • Loading branch information
jacopocarlini authored Aug 8, 2024
2 parents c81037b + b296f72 commit 5c9f48e
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 23 deletions.
4 changes: 2 additions & 2 deletions helm/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ apiVersion: v2
name: pagopa-print-payment-notice-functions
description: Microservice that handles services for notice print generation
type: application
version: 0.38.0
appVersion: 1.0.0
version: 0.39.0
appVersion: 1.0.0-1-hotfix-zip-function
dependencies:
- name: microservice-chart
version: 2.8.0
Expand Down
2 changes: 1 addition & 1 deletion helm/values-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ microservice-chart:
fullnameOverride: "print-payment-notice-functions"
image:
repository: ghcr.io/pagopa/pagopa-print-payment-notice-functions
tag: "1.0.0"
tag: "1.0.0-1-hotfix-zip-function"
pullPolicy: Always
livenessProbe:
httpGet:
Expand Down
2 changes: 1 addition & 1 deletion helm/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ microservice-chart:
fullnameOverride: "print-payment-notice-functions"
image:
repository: ghcr.io/pagopa/pagopa-print-payment-notice-functions
tag: "1.0.0"
tag: "1.0.0-1-hotfix-zip-function"
pullPolicy: Always
livenessProbe:
httpGet:
Expand Down
2 changes: 1 addition & 1 deletion helm/values-uat.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ microservice-chart:
fullnameOverride: "print-payment-notice-functions"
image:
repository: ghcr.io/pagopa/pagopa-print-payment-notice-functions
tag: "1.0.0"
tag: "1.0.0-1-hotfix-zip-function"
pullPolicy: Always
livenessProbe:
httpGet:
Expand Down
71 changes: 70 additions & 1 deletion openapi/openapi.json
Original file line number Diff line number Diff line change
@@ -1 +1,70 @@

{
"openapi": "3.0.1",
"info": {
"description": "PagoPA Print Payment Notices Functions",
"termsOfService": "https://www.pagopa.gov.it/",
"title": "pagopa-print-payment-notice-functions",
"version": "1.0.0-1-hotfix-zip-function"
},
"servers": [
{
"url": "http://localhost",
"description": "Generated server url"
}
],
"paths": {
"/info": {
"get": {
"description": "Return OK if application is started",
"operationId": "healthCheck",
"responses": {
"200": {
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/AppInfo"
}
}
},
"description": "OK"
}
},
"security": [
{
"ApiKey": []
}
],
"summary": "health check",
"tags": [
"Home"
]
}
}
},
"components": {
"schemas": {
"AppInfo": {
"type": "object",
"properties": {
"environment": {
"type": "string"
},
"name": {
"type": "string"
},
"version": {
"type": "string"
}
}
}
},
"securitySchemes": {
"ApiKey": {
"description": "The API key to access this function app.",
"in": "header",
"name": "Ocp-Apim-Subscription-Key",
"type": "apiKey"
}
}
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>it.gov.pagopa</groupId>
<artifactId>print-payment-notice-functions</artifactId>
<version>1.0.0</version>
<version>1.0.0-1-hotfix-zip-function</version>
<name>pagopa-print-payment-notice-functions</name>
<description>PagoPA Print Payment Notices Functions</description>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@
import com.azure.storage.blob.BlobServiceClientBuilder;
import com.azure.storage.blob.models.ListBlobsOptions;
import it.gov.pagopa.print.payment.notice.functions.model.response.BlobStorageResponse;
import it.gov.pagopa.print.payment.notice.functions.utils.WorkingDirectoryUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import static it.gov.pagopa.print.payment.notice.functions.utils.WorkingDirectoryUtils.createWorkingDirectory;

@Component
@Slf4j
public class NoticeStorageClient {
Expand Down Expand Up @@ -48,11 +51,16 @@ public NoticeStorageClient(
public BlobStorageResponse compressFolder(String folderId) throws IOException {
log.info("Create Zip file. Request {}", folderId);

try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {

File workingDirectory = createWorkingDirectory();
Path tempDirectory = Files.createTempDirectory(workingDirectory.toPath(), "notice-generation-function")
.normalize()
.toAbsolutePath();
File pathFile = File.createTempFile("tempFile", ".zip", tempDirectory.toFile());

try (ZipOutputStream zipStream = new ZipOutputStream(outputStream)) {
// List<CompletableFuture<Void>> futures = new ArrayList<>();
try {
try (FileOutputStream fos = new FileOutputStream(pathFile);
BufferedOutputStream outputStream = new BufferedOutputStream(fos);
ZipOutputStream zipStream = new ZipOutputStream(outputStream)) {

String delimiter = "/";
ListBlobsOptions options = new ListBlobsOptions()
Expand All @@ -73,15 +81,14 @@ public BlobStorageResponse compressFolder(String folderId) throws IOException {
log.info("Get info file {} from blob. Request {}", blobItem.getName(), folderId);
final BlobClient blobClient = blobContainerClient.getBlobClient(blobItem.getName());


// CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
try (ByteArrayOutputStream fileOutputStream = new ByteArrayOutputStream()) {
if (blobClient.exists()) {
log.info("put file {} into zipStream. Request {}", blobItem.getName(), folderId);
blobClient.downloadStream(fileOutputStream);
zipStream.putNextEntry(new ZipEntry(finalSingleFileName));
zipStream.write(fileOutputStream.toByteArray());
zipStream.closeEntry();
zipStream.flush();
} else {
log.error("file not found: {}", finalSingleFileName);
throw new RuntimeException("File not found: " + finalSingleFilepath);
Expand All @@ -91,22 +98,24 @@ public BlobStorageResponse compressFolder(String folderId) throws IOException {
throw new RuntimeException("Error processing file: " + finalSingleFileName, e);
}
log.info("Download file completed. Request {}", folderId);
// });

// futures.add(future);

}
});

// CompletableFuture<Void> allOf = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
// allOf.join();
// TODO : zip library is not thread-safe. Make multithread in the future with another library
zipStream.finish();


} catch (IOException e) {
throw new RuntimeException(e);
}

try (FileInputStream fis = new FileInputStream(pathFile);
BufferedInputStream bif = new BufferedInputStream(fis)) {

BlobClient zipFileClient = blobContainerClient.getBlobClient(
folderId + "/" + folderId.concat(".zip"));
zipFileClient.upload(new ByteArrayInputStream(
outputStream.toByteArray()), outputStream.size(), true);
zipFileClient.upload(bif, true);
log.info("Zip file uploaded. Request {}", folderId);
} catch (IOException e) {
throw new RuntimeException(e);
Expand All @@ -115,6 +124,8 @@ public BlobStorageResponse compressFolder(String folderId) throws IOException {
BlobStorageResponse blobStorageResponse = new BlobStorageResponse();
blobStorageResponse.setStatusCode(HttpStatus.OK.value());
return blobStorageResponse;
} finally {
WorkingDirectoryUtils.clearTempDirectory(tempDirectory);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package it.gov.pagopa.print.payment.notice.functions.utils;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

/**
* Utils methods for working directory
*/
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class WorkingDirectoryUtils {

public static File createWorkingDirectory() throws IOException {
File workingDirectory = new File("temp");
if(!workingDirectory.exists()) {
Files.createDirectory(workingDirectory.toPath());
}
return workingDirectory;
}

public static void clearTempDirectory(java.nio.file.Path workingDirPath) {
try {
FileUtils.deleteDirectory(workingDirPath.toFile());
} catch (IOException e) {
log.warn("Unable to clear working directory", e);
}
}

}

0 comments on commit 5c9f48e

Please sign in to comment.