RED-6864 - Switch to DELETE_ON_CLOSE

This commit is contained in:
Andrei Isvoran 2023-08-09 09:30:37 +03:00
parent f56ab8fa49
commit 5c1dca5933

View File

@ -1,11 +1,13 @@
package com.knecon.fforesight.service.layoutparser.processor; package com.knecon.fforesight.service.layoutparser.processor;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files; 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.commons.io.IOUtils;
import org.apache.pdfbox.Loader; 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.service.persistence.service.v1.api.shared.model.redactionlog.section.SectionGrid;
import com.iqser.red.storage.commons.service.StorageService; 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.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.data.taas.ResearchDocumentData;
import com.knecon.fforesight.service.layoutparser.internal.api.queue.LayoutParsingRequest; 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.image.ImageServiceResponse;
import com.knecon.fforesight.service.layoutparser.processor.python_api.model.table.TableServiceResponse; import com.knecon.fforesight.service.layoutparser.processor.python_api.model.table.TableServiceResponse;
import com.knecon.fforesight.tenantcommons.TenantContext; import com.knecon.fforesight.tenantcommons.TenantContext;
@ -43,6 +45,7 @@ public class LayoutParsingStorageService {
File tempFile = createTempFile("document", ".pdf"); File tempFile = createTempFile("document", ".pdf");
try (var tempFileOutputStream = new FileOutputStream(tempFile)) { try (var tempFileOutputStream = new FileOutputStream(tempFile)) {
IOUtils.copy(originDocumentInputStream, tempFileOutputStream); IOUtils.copy(originDocumentInputStream, tempFileOutputStream);
originDocumentInputStream.close();
} }
return Loader.loadPDF(tempFile, MemoryUsageSetting.setupMixed(67108864L)); return Loader.loadPDF(tempFile, MemoryUsageSetting.setupMixed(67108864L));
} }
@ -53,7 +56,9 @@ public class LayoutParsingStorageService {
try (InputStream inputStream = getObject(storageId)) { 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)) { 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) { private InputStream getObject(String storageId) {
File tempFile = File.createTempFile("temp", ".data"); File tempFile = File.createTempFile("temp", ".data");
tempFile.deleteOnExit();
storageService.downloadTo(TenantContext.getTenantId(), storageId, tempFile); storageService.downloadTo(TenantContext.getTenantId(), storageId, tempFile);
return new FileInputStream(tempFile); Path path = Paths.get(tempFile.getPath());
return Files.newInputStream(path, StandardOpenOption.DELETE_ON_CLOSE);
} }
} }