Skip to content

Commit

Permalink
Clean temp folder after completing PDF generation
Browse files Browse the repository at this point in the history
  • Loading branch information
shuhanmirza committed Jan 17, 2024
1 parent f4b6867 commit 25c7473
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.shuhanmirza.simplepdfgenapi.component.PdfGenerator;
import org.shuhanmirza.simplepdfgenapi.dto.FontFile;
import org.shuhanmirza.simplepdfgenapi.dto.PdfBuildingInstruction;
import org.shuhanmirza.simplepdfgenapi.service.PdfCleanUpService;
import org.shuhanmirza.simplepdfgenapi.service.UtilityService;
import org.shuhanmirza.simplepdfgenapi.util.Utility;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -32,9 +33,11 @@
public class HtmlTemplatePdfGenerator implements PdfGenerator {
private final SpringTemplateEngine springTemplateEngine;
private final UtilityService utilityService;
private final PdfCleanUpService pdfCleanUpService;

public HtmlTemplatePdfGenerator(UtilityService utilityService) {
public HtmlTemplatePdfGenerator(UtilityService utilityService, PdfCleanUpService pdfCleanUpService) {
this.utilityService = utilityService;
this.pdfCleanUpService = pdfCleanUpService;
springTemplateEngine = new SpringTemplateEngine();

var stringTemplateResolver = new StringTemplateResolver();
Expand All @@ -50,6 +53,8 @@ public Mono<InputStream> generatePdfFromTemplate(PdfBuildingInstruction pdfBuild
log.info("HtmlGenerator: Temp Folder Generated {}", tempFolderPath);
var context = generateContext(pdfBuildingInstruction);

pdfCleanUpService.scheduleFolderForDeletion(tempFolderPath);

return generateHtml(pdfBuildingInstruction, context)
.flatMap(generatedHtml -> generatedPdfFromHtml(pdfBuildingInstruction, tempFolderPath, generatedHtml, tempFolderPath.concat("/").concat(Utility.HTML_FILE_OUTPUT)));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.apache.commons.lang3.StringUtils;
import org.shuhanmirza.simplepdfgenapi.component.PdfGenerator;
import org.shuhanmirza.simplepdfgenapi.dto.PdfBuildingInstruction;
import org.shuhanmirza.simplepdfgenapi.service.PdfCleanUpService;
import org.shuhanmirza.simplepdfgenapi.service.UtilityService;
import org.shuhanmirza.simplepdfgenapi.util.Utility;
import org.springframework.stereotype.Component;
Expand All @@ -27,13 +28,16 @@
@RequiredArgsConstructor
public class LatexTemplatePdfGenerator implements PdfGenerator {
private final UtilityService utilityService;
private final PdfCleanUpService pdfCleanUpService;

@Override
public Mono<InputStream> generatePdfFromTemplate(PdfBuildingInstruction pdfBuildingInstruction) {

return Mono.fromFuture(utilityService.createTemporaryDirectory(Utility.APPLICATION_NAME))
.flatMap(tempFolderPath -> {
log.info("LatexGenerator: Temp Folder Generated {}", tempFolderPath);
pdfCleanUpService.scheduleFolderForDeletion(tempFolderPath);

return downloadAllFiles(pdfBuildingInstruction, tempFolderPath)
.flatMap(fileDownloaded -> prepareLatexFile(pdfBuildingInstruction, tempFolderPath))
.flatMap(latexFilePath -> Mono.fromFuture(compilePdf(tempFolderPath)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,4 @@ public class PdfGeneratorController {
public Mono<ResponseEntity<?>> generatePdf(@RequestBody @Valid PdfGenerationRequest pdfGenerationRequest) {
return pdfGenerationService.generatePdfFromTemplate(pdfGenerationRequest);
}

//TODO: return file in a callback url
//TODO: span and trace id
//TODO: add password protection for pdf
//TODO: clean up temp folder after completion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.shuhanmirza.simplepdfgenapi.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
* @author Shuhan Mirza
* @since 17/1/24
*/
@Service
@Slf4j
@RequiredArgsConstructor
public class PdfCleanUpService {

private static long DELETE_FOLDER_DELAY_MINUTES = 15L;

private Mono<Boolean> deleteFolder(String folderPath) {

var folder = new File(folderPath);
try {
FileUtils.deleteDirectory(folder);
} catch (IOException ioException) {
log.error("Failed to delete {}", folderPath, ioException);
return Mono.just(Boolean.FALSE);
}

log.info("Deleted {}", folder.getAbsolutePath());

return Mono.just(Boolean.TRUE);
}

public void scheduleFolderForDeletion(String folderPath) {
Schedulers.boundedElastic().schedule(() -> deleteFolder(folderPath).subscribe(), DELETE_FOLDER_DELAY_MINUTES, TimeUnit.MINUTES);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.shuhanmirza.simplepdfgenapi.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;

/**
* @author Shuhan Mirza
Expand All @@ -14,8 +13,7 @@ private Utility() {
}

public static int XX_HASH_SEED = 0x6969abcd;
@Value("${spring.application.name}")
public static String APPLICATION_NAME;
public static String APPLICATION_NAME = "SimplePdfGenAPI";
public static String LATEX_FILE_INPUT = "main.tex";
public static String LATEX_FILE_OUTPUT = "main.pdf";
public static String HTML_FILE_OUTPUT = "main.pdf";
Expand Down

0 comments on commit 25c7473

Please sign in to comment.