diff --git a/SimplePdfGenAPI/src/main/java/org/shuhanmirza/simplepdfgenapi/component/pdfgenerator/HtmlTemplatePdfGenerator.java b/SimplePdfGenAPI/src/main/java/org/shuhanmirza/simplepdfgenapi/component/pdfgenerator/HtmlTemplatePdfGenerator.java index 85b6021..f4ce6ee 100644 --- a/SimplePdfGenAPI/src/main/java/org/shuhanmirza/simplepdfgenapi/component/pdfgenerator/HtmlTemplatePdfGenerator.java +++ b/SimplePdfGenAPI/src/main/java/org/shuhanmirza/simplepdfgenapi/component/pdfgenerator/HtmlTemplatePdfGenerator.java @@ -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; @@ -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(); @@ -50,6 +53,8 @@ public Mono 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))); }); diff --git a/SimplePdfGenAPI/src/main/java/org/shuhanmirza/simplepdfgenapi/component/pdfgenerator/LatexTemplatePdfGenerator.java b/SimplePdfGenAPI/src/main/java/org/shuhanmirza/simplepdfgenapi/component/pdfgenerator/LatexTemplatePdfGenerator.java index 9fbf7aa..5813e84 100644 --- a/SimplePdfGenAPI/src/main/java/org/shuhanmirza/simplepdfgenapi/component/pdfgenerator/LatexTemplatePdfGenerator.java +++ b/SimplePdfGenAPI/src/main/java/org/shuhanmirza/simplepdfgenapi/component/pdfgenerator/LatexTemplatePdfGenerator.java @@ -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; @@ -27,6 +28,7 @@ @RequiredArgsConstructor public class LatexTemplatePdfGenerator implements PdfGenerator { private final UtilityService utilityService; + private final PdfCleanUpService pdfCleanUpService; @Override public Mono generatePdfFromTemplate(PdfBuildingInstruction pdfBuildingInstruction) { @@ -34,6 +36,8 @@ public Mono generatePdfFromTemplate(PdfBuildingInstruction pdfBuild 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))) diff --git a/SimplePdfGenAPI/src/main/java/org/shuhanmirza/simplepdfgenapi/controller/PdfGeneratorController.java b/SimplePdfGenAPI/src/main/java/org/shuhanmirza/simplepdfgenapi/controller/PdfGeneratorController.java index 0288bb5..f07c846 100644 --- a/SimplePdfGenAPI/src/main/java/org/shuhanmirza/simplepdfgenapi/controller/PdfGeneratorController.java +++ b/SimplePdfGenAPI/src/main/java/org/shuhanmirza/simplepdfgenapi/controller/PdfGeneratorController.java @@ -29,9 +29,4 @@ public class PdfGeneratorController { public Mono> 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 } diff --git a/SimplePdfGenAPI/src/main/java/org/shuhanmirza/simplepdfgenapi/service/PdfCleanUpService.java b/SimplePdfGenAPI/src/main/java/org/shuhanmirza/simplepdfgenapi/service/PdfCleanUpService.java new file mode 100644 index 0000000..7276aaf --- /dev/null +++ b/SimplePdfGenAPI/src/main/java/org/shuhanmirza/simplepdfgenapi/service/PdfCleanUpService.java @@ -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 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); + } + +} diff --git a/SimplePdfGenAPI/src/main/java/org/shuhanmirza/simplepdfgenapi/util/Utility.java b/SimplePdfGenAPI/src/main/java/org/shuhanmirza/simplepdfgenapi/util/Utility.java index e3c22e5..4bd0c80 100644 --- a/SimplePdfGenAPI/src/main/java/org/shuhanmirza/simplepdfgenapi/util/Utility.java +++ b/SimplePdfGenAPI/src/main/java/org/shuhanmirza/simplepdfgenapi/util/Utility.java @@ -1,7 +1,6 @@ package org.shuhanmirza.simplepdfgenapi.util; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; /** * @author Shuhan Mirza @@ -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";