Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save generated PDF in the /tmp folder #125

Merged
merged 2 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading