proper error handling for image clasification

This commit is contained in:
Timo 2021-04-18 11:30:53 +03:00
parent ed5686dc51
commit a34d2fb675
3 changed files with 67 additions and 63 deletions

View File

@ -35,7 +35,7 @@ public class ImageClassificationService {
var mockFile = new MockMultipartFile("file", "Image.png", "image/png", baos.toByteArray()); var mockFile = new MockMultipartFile("file", "Image.png", "image/png", baos.toByteArray());
ImageClassificationResponse response = imageClassificationClient.classify(mockFile); ImageClassificationResponse response = imageClassificationClient.classify(mockFile);
image.setImageType(ImageType.valueOf(response.getCategory())); image.setImageType(ImageType.valueOf(response.getCategory()));
} catch (IOException e) { } catch (Exception e) {
log.error("Could not classify image", e); log.error("Could not classify image", e);
} }

View File

@ -127,7 +127,8 @@ public class PdfSegmentationService {
public Document parseDocument(InputStream documentInputStream) throws IOException { public Document parseDocument(InputStream documentInputStream) throws IOException {
PDDocument pdDocument = null;
try {
//create tempFile //create tempFile
File tempFile = File.createTempFile("document", ".pdf"); File tempFile = File.createTempFile("document", ".pdf");
IOUtils.copy(documentInputStream, new FileOutputStream(tempFile)); IOUtils.copy(documentInputStream, new FileOutputStream(tempFile));
@ -137,7 +138,7 @@ public class PdfSegmentationService {
List<Page> pages = new ArrayList<>(); List<Page> pages = new ArrayList<>();
PDDocument pdDocument = reinitializePDDocument(tempFile, null); pdDocument = reinitializePDDocument(tempFile, null);
long pageCount = pdDocument.getNumberOfPages(); long pageCount = pdDocument.getNumberOfPages();
for (int pageNumber = 1; pageNumber <= pageCount; pageNumber++) { for (int pageNumber = 1; pageNumber <= pageCount; pageNumber++) {
@ -201,6 +202,11 @@ public class PdfSegmentationService {
tempFile.delete(); tempFile.delete();
return document; return document;
} finally {
if (pdDocument != null) {
pdDocument.close();
}
}
} }
private PDDocument reinitializePDDocument(File tempFile, PDDocument pdDocument) throws IOException { private PDDocument reinitializePDDocument(File tempFile, PDDocument pdDocument) throws IOException {

View File

@ -19,7 +19,6 @@ import lombok.SneakyThrows;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.kie.api.KieServices; import org.kie.api.KieServices;
@ -452,7 +451,6 @@ public class RedactionIntegrationTest {
@Test @Test
@Ignore
public void testLargeScannedFileOOM() { public void testLargeScannedFileOOM() {
AnalyzeRequest request = prepareStorage("scanned/VV-377031.pdf"); AnalyzeRequest request = prepareStorage("scanned/VV-377031.pdf");
MemoryStats.printMemoryStats(); MemoryStats.printMemoryStats();