RED-6204: Corrected temp file deletion in PdfSegmentationService.
Previously files were not deleted correctly, because the code tried to delete the file while a file-stream was still open.
This commit is contained in:
parent
4b21418163
commit
6ae4d16bb7
@ -53,15 +53,14 @@ public class PdfSegmentationService {
|
|||||||
public Document parseDocument(String dossierId, String fileId, InputStream documentInputStream, Map<Integer, List<PdfImage>> pdfImages) throws IOException {
|
public Document parseDocument(String dossierId, String fileId, InputStream documentInputStream, Map<Integer, List<PdfImage>> pdfImages) throws IOException {
|
||||||
|
|
||||||
PDDocument pdDocument = null;
|
PDDocument pdDocument = null;
|
||||||
|
File tempFile = null;
|
||||||
try {
|
try {
|
||||||
//create tempFile
|
|
||||||
File tempFile = FileUtils.createTempFile("document", ".pdf");
|
|
||||||
|
|
||||||
Map<Integer, List<PdfTableCell>> pdfTableCells = new HashMap<>();
|
Map<Integer, List<PdfTableCell>> pdfTableCells = new HashMap<>();
|
||||||
if (redactionServiceSettings.isCvTableParsingEnabled()) {
|
if (redactionServiceSettings.isCvTableParsingEnabled()) {
|
||||||
pdfTableCells = tableService.convertTables(dossierId, fileId);
|
pdfTableCells = tableService.convertTables(dossierId, fileId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tempFile = FileUtils.createTempFile("document", ".pdf");
|
||||||
try (var fos = new FileOutputStream(tempFile)) {
|
try (var fos = new FileOutputStream(tempFile)) {
|
||||||
IOUtils.copy(documentInputStream, fos);
|
IOUtils.copy(documentInputStream, fos);
|
||||||
|
|
||||||
@ -74,6 +73,33 @@ public class PdfSegmentationService {
|
|||||||
long pageCount = pdDocument.getNumberOfPages();
|
long pageCount = pdDocument.getNumberOfPages();
|
||||||
|
|
||||||
for (int pageNumber = 1; pageNumber <= pageCount; pageNumber++) {
|
for (int pageNumber = 1; pageNumber <= pageCount; pageNumber++) {
|
||||||
|
processPage(pdfImages, pdDocument, pdfTableCells, document, pages, pageNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
document.setPages(pages);
|
||||||
|
|
||||||
|
classificationService.classifyDocument(document);
|
||||||
|
sectionsBuilderService.buildSections(document);
|
||||||
|
sectionsBuilderService.addImagesToSections(document);
|
||||||
|
|
||||||
|
return document;
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (pdDocument != null) {
|
||||||
|
pdDocument.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
FileUtils.deleteFile(tempFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void processPage(Map<Integer, List<PdfImage>> pdfImages,
|
||||||
|
PDDocument pdDocument,
|
||||||
|
Map<Integer, List<PdfTableCell>> pdfTableCells,
|
||||||
|
Document document,
|
||||||
|
List<Page> pages,
|
||||||
|
int pageNumber) throws IOException {
|
||||||
|
|
||||||
PDFLinesTextStripper stripper = new PDFLinesTextStripper();
|
PDFLinesTextStripper stripper = new PDFLinesTextStripper();
|
||||||
PDPage pdPage = pdDocument.getPage(pageNumber - 1);
|
PDPage pdPage = pdDocument.getPage(pageNumber - 1);
|
||||||
@ -114,27 +140,6 @@ public class PdfSegmentationService {
|
|||||||
pages.add(page);
|
pages.add(page);
|
||||||
}
|
}
|
||||||
|
|
||||||
document.setPages(pages);
|
|
||||||
|
|
||||||
classificationService.classifyDocument(document);
|
|
||||||
sectionsBuilderService.buildSections(document);
|
|
||||||
sectionsBuilderService.addImagesToSections(document);
|
|
||||||
|
|
||||||
IOUtils.close(pdDocument);
|
|
||||||
|
|
||||||
if (!tempFile.delete()) {
|
|
||||||
log.warn("Could not delete tmp file");
|
|
||||||
}
|
|
||||||
|
|
||||||
return document;
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if (pdDocument != null) {
|
|
||||||
pdDocument.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void increaseDocumentStatistics(Page page, Document document) {
|
private void increaseDocumentStatistics(Page page, Document document) {
|
||||||
|
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package com.iqser.red.service.redaction.v1.server.tableextraction.utils;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
import lombok.experimental.UtilityClass;
|
import lombok.experimental.UtilityClass;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@ -20,19 +21,31 @@ public class FileUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes a files; logs a message with the reason if the deletions fails.
|
||||||
|
* This method is null-safe.
|
||||||
|
* @param file The file to delete. Can be null.
|
||||||
|
*/
|
||||||
|
public void deleteFile(File file) {
|
||||||
|
if (file != null) {
|
||||||
|
try {
|
||||||
|
Files.deleteIfExists(Path.of(file.toString()));
|
||||||
|
} catch (IOException ex) {
|
||||||
|
log.warn("Could not delete file!", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// We don't need to check the results of the permission setters below,
|
// We don't need to check the results of the permission setters below,
|
||||||
// since we're manipulating a file we created ourselves.
|
// since we're manipulating a file we created ourselves.
|
||||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||||
private void setRWPermissionsOnlyForOwner(File tempFile) {
|
private void setRWPermissionsOnlyForOwner(File tempFile) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// deny for all
|
|
||||||
tempFile.setReadable(false);
|
|
||||||
tempFile.setWritable(false);
|
|
||||||
tempFile.setExecutable(false);
|
|
||||||
// allow for owner
|
|
||||||
tempFile.setReadable(true, true);
|
tempFile.setReadable(true, true);
|
||||||
tempFile.setWritable(true, true);
|
tempFile.setWritable(true, true);
|
||||||
|
tempFile.setExecutable(false);
|
||||||
} catch (SecurityException ex) {
|
} catch (SecurityException ex) {
|
||||||
// This should never happen since we're creating a temp file ourselves.
|
// This should never happen since we're creating a temp file ourselves.
|
||||||
log.warn("Caught an exception during temp file creation. This should not happend. Check the code.", ex);
|
log.warn("Caught an exception during temp file creation. This should not happend. Check the code.", ex);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user