Skip to content

Commit

Permalink
Merge pull request #125 from bcgov/develop/alex-GRAD2-2410-2
Browse files Browse the repository at this point in the history
Save generated PDF in the /tmp folder
  • Loading branch information
kamal-mohammed committed Nov 27, 2023
2 parents 1764e48 + b89ce9a commit 497b9e8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
import java.util.concurrent.CompletableFuture;

Expand All @@ -38,6 +41,7 @@ public class GradBusinessService {
private static final String APPLICATION_JSON = "application/json";
private static final String APPLICATION_PDF = "application/pdf";
private static final String ACCEPT = "*/*";
private static final String TMP = "/tmp";
/**
* The Web client.
*/
Expand Down Expand Up @@ -188,7 +192,7 @@ public ResponseEntity<byte[]> getSchoolReportPDFByMincode(String mincode, String
if(result != null) {
res = result.getInputStream().readAllBytes();
}
return handleBinaryResponse(res, EducGradBusinessUtil.getFileNameSchoolReports(mincode,year,month,type), MediaType.APPLICATION_PDF);
return handleBinaryResponse(res, EducGradBusinessUtil.getFileNameSchoolReports(mincode,year,month,type,MediaType.APPLICATION_PDF), MediaType.APPLICATION_PDF);
} catch (Exception e) {
return getInternalServerErrorResponse(e);
}
Expand All @@ -206,7 +210,7 @@ public ResponseEntity<byte[]> getAmalgamatedSchoolReportPDFByMincode(String minc
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("PST"), Locale.CANADA);
int year = cal.get(Calendar.YEAR);
String month = "00";
String fileName = EducGradBusinessUtil.getFileNameSchoolReports(mincode, year, month, type);
String fileName = EducGradBusinessUtil.getFileNameSchoolReports(mincode, year, month, type, MediaType.APPLICATION_PDF);
try {
logger.debug("******** Merging Documents Started ******");
byte[] res = EducGradBusinessUtil.mergeDocumentsPDFs(locations);
Expand All @@ -215,6 +219,7 @@ public ResponseEntity<byte[]> getAmalgamatedSchoolReportPDFByMincode(String minc
headers.put(HttpHeaders.AUTHORIZATION, Collections.singletonList(BEARER + accessToken));
headers.put(HttpHeaders.ACCEPT, Collections.singletonList(APPLICATION_PDF));
headers.put(HttpHeaders.CONTENT_TYPE, Collections.singletonList(APPLICATION_PDF));
saveBinaryResponseToFile(res, fileName);
return handleBinaryResponse(res, fileName, MediaType.APPLICATION_PDF);
} catch (Exception e) {
return getInternalServerErrorResponse(e);
Expand Down Expand Up @@ -346,4 +351,12 @@ private ResponseEntity<byte[]> handleBinaryResponse(byte[] resultBinary, String
}
return response;
}

private void saveBinaryResponseToFile(byte[] resultBinary, String reportFile) throws IOException {
if(resultBinary.length > 0) {
try (OutputStream out = new FileOutputStream(TMP + "/" + reportFile)) {
out.write(resultBinary);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;

import java.io.*;
import java.util.ArrayList;
Expand Down Expand Up @@ -98,8 +99,8 @@ public static String getTempDirPath() {
return Optional.ofNullable(System.getProperty("java.io.tmpdir")).orElse("/tmp").concat(File.pathSeparator);
}

public static String getFileNameSchoolReports(String mincode, int year, String month, String type) {
return mincode + "_" + year + month + "_" + type + ".pdf";
public static String getFileNameSchoolReports(String mincode, int year, String month, String type, MediaType mediaType) {
return mincode + "_" + year + month + "_" + type + "." + mediaType.getSubtype();
}

public static String getFileNameStudentCredentials(String mincode, String pen, String type) {
Expand Down

0 comments on commit 497b9e8

Please sign in to comment.