From 92d1c6789eda4ee3759f81ab46fa9a3bc7bdc7c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ianar=C3=A9=20S=C3=A9vi?= Date: Tue, 16 Jun 2026 14:08:42 +0200 Subject: [PATCH] :white_check_mark: make sure HEIF is supported --- .../com/mindee/input/LocalInputSource.java | 2 +- .../mindee/input/LocalInputSourceTest.java | 62 ++++++++++++------- src/test/resources | 2 +- 3 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/mindee/input/LocalInputSource.java b/src/main/java/com/mindee/input/LocalInputSource.java index 1dd89595e..5dc0ee582 100644 --- a/src/main/java/com/mindee/input/LocalInputSource.java +++ b/src/main/java/com/mindee/input/LocalInputSource.java @@ -55,7 +55,7 @@ public LocalInputSource(byte[] fileAsByteArray, String filename) { } public LocalInputSource(String fileAsBase64, String filename) { - this.file = Base64.getDecoder().decode(fileAsBase64.getBytes()); + this.file = Base64.getMimeDecoder().decode(fileAsBase64.getBytes()); this.filename = filename; } diff --git a/src/test/java/com/mindee/input/LocalInputSourceTest.java b/src/test/java/com/mindee/input/LocalInputSourceTest.java index 81788e354..a568612d0 100644 --- a/src/test/java/com/mindee/input/LocalInputSourceTest.java +++ b/src/test/java/com/mindee/input/LocalInputSourceTest.java @@ -8,9 +8,12 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.stream.Stream; import org.apache.commons.codec.binary.Base64; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; public class LocalInputSourceTest { void assertMultipagePDF(LocalInputSource inputSource, Path filePath) throws IOException { @@ -66,42 +69,59 @@ void loadPDF__withoutText_mustNotDetectSourceText() throws MindeeException, IOEx Assertions.assertTrue(localInputSource.isPDF()); } - void assertImage(LocalInputSource inputSource, Path filePath) throws IOException { + void assertImage( + LocalInputSource inputSource, + Path filePath, + String filename + ) throws IOException { Assertions.assertNotNull(inputSource); Assertions.assertFalse(inputSource.isPDF()); Assertions.assertEquals(1, inputSource.getPageCount()); - Assertions.assertEquals("receipt.jpg", inputSource.getFilename()); + Assertions.assertEquals(filename, inputSource.getFilename()); Assertions.assertArrayEquals(inputSource.getFile(), Files.readAllBytes(filePath)); } - @Test - void loadImage_withFile_mustReturnAValidLocalInputSource() throws IOException { - File file = getResourcePath("file_types/receipt.jpg").toFile(); - var localInputSource = new LocalInputSource(file); - assertImage(localInputSource, file.toPath()); + private static Stream imageExtensions() { + return Stream.of("heic", "heif", "jpg", "jpga", "png", "tif", "tiff", "webp"); } - @Test - void loadImage_withInputStream_mustReturnAValidLocalInputSource() throws IOException { - Path filePath = getResourcePath("file_types/receipt.jpg"); - var localInputSource = new LocalInputSource(Files.newInputStream(filePath), "receipt.jpg"); - assertImage(localInputSource, filePath); + @ParameterizedTest + @MethodSource("imageExtensions") + void loadImage_withFile_mustReturnAValidLocalInputSource(String extension) throws IOException { + File file = getResourcePath("file_types/receipt." + extension).toFile(); + var inputSource = new LocalInputSource(file); + assertImage(inputSource, file.toPath(), "receipt." + extension); } - @Test - void loadImage_withByteArray_mustReturnAValidLocalInputSource() throws IOException { - Path filePath = getResourcePath("file_types/receipt.jpg"); - var localInputSource = new LocalInputSource(Files.readAllBytes(filePath), "receipt.jpg"); - assertImage(localInputSource, filePath); + @ParameterizedTest + @MethodSource("imageExtensions") + void loadImage_withInputStream_mustReturnAValidLocalInputSource( + String extension + ) throws IOException { + Path filePath = getResourcePath("file_types/receipt." + extension); + var inputSource = new LocalInputSource(Files.newInputStream(filePath), "receipt." + extension); + assertImage(inputSource, filePath, "receipt." + extension); + } + + @ParameterizedTest + @MethodSource("imageExtensions") + void loadImage_withByteArray_mustReturnAValidLocalInputSource( + String extension + ) throws IOException { + Path filePath = getResourcePath("file_types/receipt." + extension); + var inputSource = new LocalInputSource(Files.readAllBytes(filePath), "receipt." + extension); + assertImage(inputSource, filePath, "receipt." + extension); } @Test void loadImage_withBase64Encoded_mustReturnAValidLocalInputSource() throws IOException { - Path filePath = getResourcePath("file_types/receipt.jpg"); - String encodedFile = Base64.encodeBase64String(Files.readAllBytes(filePath)); - var localInputSource = new LocalInputSource(encodedFile, "receipt.jpg"); - assertImage(localInputSource, filePath); + Path filePath = getResourcePath("file_types/receipt.txt"); + String encodedFile = Files.readString(filePath, java.nio.charset.StandardCharsets.UTF_8); + var inputSource = new LocalInputSource(encodedFile, "receipt.jpg"); + Assertions.assertFalse(inputSource.isPDF()); + Assertions.assertEquals(1, inputSource.getPageCount()); + Assertions.assertEquals("receipt.jpg", inputSource.getFilename()); } } diff --git a/src/test/resources b/src/test/resources index 13093f3a4..2d7fcf8f5 160000 --- a/src/test/resources +++ b/src/test/resources @@ -1 +1 @@ -Subproject commit 13093f3a48de212ef26889df71199c1a2a9d1478 +Subproject commit 2d7fcf8f591f6d7f40e39862965325e6a8a21874