From 5c1dca593387572a9a2f4792bbc430dc3b310ce8 Mon Sep 17 00:00:00 2001 From: Andrei Isvoran Date: Wed, 9 Aug 2023 09:30:37 +0300 Subject: [PATCH] RED-6864 - Switch to DELETE_ON_CLOSE --- .../LayoutParsingStorageService.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/layoutparser-service/layoutparser-service-processor/src/main/java/com/knecon/fforesight/service/layoutparser/processor/LayoutParsingStorageService.java b/layoutparser-service/layoutparser-service-processor/src/main/java/com/knecon/fforesight/service/layoutparser/processor/LayoutParsingStorageService.java index 7053c01..836a582 100644 --- a/layoutparser-service/layoutparser-service-processor/src/main/java/com/knecon/fforesight/service/layoutparser/processor/LayoutParsingStorageService.java +++ b/layoutparser-service/layoutparser-service-processor/src/main/java/com/knecon/fforesight/service/layoutparser/processor/LayoutParsingStorageService.java @@ -1,11 +1,13 @@ package com.knecon.fforesight.service.layoutparser.processor; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; import org.apache.commons.io.IOUtils; import org.apache.pdfbox.Loader; @@ -17,9 +19,9 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.iqser.red.service.persistence.service.v1.api.shared.model.redactionlog.section.SectionGrid; import com.iqser.red.storage.commons.service.StorageService; import com.knecon.fforesight.service.layoutparser.internal.api.data.redaction.DocumentData; +import com.knecon.fforesight.service.layoutparser.internal.api.data.redaction.SimplifiedText; import com.knecon.fforesight.service.layoutparser.internal.api.data.taas.ResearchDocumentData; import com.knecon.fforesight.service.layoutparser.internal.api.queue.LayoutParsingRequest; -import com.knecon.fforesight.service.layoutparser.internal.api.data.redaction.SimplifiedText; import com.knecon.fforesight.service.layoutparser.processor.python_api.model.image.ImageServiceResponse; import com.knecon.fforesight.service.layoutparser.processor.python_api.model.table.TableServiceResponse; import com.knecon.fforesight.tenantcommons.TenantContext; @@ -43,6 +45,7 @@ public class LayoutParsingStorageService { File tempFile = createTempFile("document", ".pdf"); try (var tempFileOutputStream = new FileOutputStream(tempFile)) { IOUtils.copy(originDocumentInputStream, tempFileOutputStream); + originDocumentInputStream.close(); } return Loader.loadPDF(tempFile, MemoryUsageSetting.setupMixed(67108864L)); } @@ -53,7 +56,9 @@ public class LayoutParsingStorageService { try (InputStream inputStream = getObject(storageId)) { - return objectMapper.readValue(inputStream, ImageServiceResponse.class); + ImageServiceResponse imageServiceResponse = objectMapper.readValue(inputStream, ImageServiceResponse.class); + inputStream.close(); + return imageServiceResponse; } } @@ -62,8 +67,9 @@ public class LayoutParsingStorageService { try (var tableClassificationStream = getObject(storageId)) { - return objectMapper.readValue(tableClassificationStream, TableServiceResponse.class); - + TableServiceResponse tableServiceResponse = objectMapper.readValue(tableClassificationStream, TableServiceResponse.class); + tableClassificationStream.close(); + return tableServiceResponse; } } @@ -123,9 +129,9 @@ public class LayoutParsingStorageService { private InputStream getObject(String storageId) { File tempFile = File.createTempFile("temp", ".data"); - tempFile.deleteOnExit(); storageService.downloadTo(TenantContext.getTenantId(), storageId, tempFile); - return new FileInputStream(tempFile); + Path path = Paths.get(tempFile.getPath()); + return Files.newInputStream(path, StandardOpenOption.DELETE_ON_CLOSE); } }