Skip to content

Commit 94fd684

Browse files
authored
✅ make sure HEIF is supported (#332)
1 parent d47ec69 commit 94fd684

3 files changed

Lines changed: 43 additions & 23 deletions

File tree

src/main/java/com/mindee/input/LocalInputSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public LocalInputSource(byte[] fileAsByteArray, String filename) {
5555
}
5656

5757
public LocalInputSource(String fileAsBase64, String filename) {
58-
this.file = Base64.getDecoder().decode(fileAsBase64.getBytes());
58+
this.file = Base64.getMimeDecoder().decode(fileAsBase64.getBytes());
5959
this.filename = filename;
6060
}
6161

src/test/java/com/mindee/input/LocalInputSourceTest.java

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
import java.io.IOException;
99
import java.nio.file.Files;
1010
import java.nio.file.Path;
11+
import java.util.stream.Stream;
1112
import org.apache.commons.codec.binary.Base64;
1213
import org.junit.jupiter.api.Assertions;
1314
import org.junit.jupiter.api.Test;
15+
import org.junit.jupiter.params.ParameterizedTest;
16+
import org.junit.jupiter.params.provider.MethodSource;
1417

1518
public class LocalInputSourceTest {
1619
void assertMultipagePDF(LocalInputSource inputSource, Path filePath) throws IOException {
@@ -66,42 +69,59 @@ void loadPDF__withoutText_mustNotDetectSourceText() throws MindeeException, IOEx
6669
Assertions.assertTrue(localInputSource.isPDF());
6770
}
6871

69-
void assertImage(LocalInputSource inputSource, Path filePath) throws IOException {
72+
void assertImage(
73+
LocalInputSource inputSource,
74+
Path filePath,
75+
String filename
76+
) throws IOException {
7077
Assertions.assertNotNull(inputSource);
7178

7279
Assertions.assertFalse(inputSource.isPDF());
7380
Assertions.assertEquals(1, inputSource.getPageCount());
74-
Assertions.assertEquals("receipt.jpg", inputSource.getFilename());
81+
Assertions.assertEquals(filename, inputSource.getFilename());
7582
Assertions.assertArrayEquals(inputSource.getFile(), Files.readAllBytes(filePath));
7683
}
7784

78-
@Test
79-
void loadImage_withFile_mustReturnAValidLocalInputSource() throws IOException {
80-
File file = getResourcePath("file_types/receipt.jpg").toFile();
81-
var localInputSource = new LocalInputSource(file);
82-
assertImage(localInputSource, file.toPath());
85+
private static Stream<String> imageExtensions() {
86+
return Stream.of("heic", "heif", "jpg", "jpga", "png", "tif", "tiff", "webp");
8387
}
8488

85-
@Test
86-
void loadImage_withInputStream_mustReturnAValidLocalInputSource() throws IOException {
87-
Path filePath = getResourcePath("file_types/receipt.jpg");
88-
var localInputSource = new LocalInputSource(Files.newInputStream(filePath), "receipt.jpg");
89-
assertImage(localInputSource, filePath);
89+
@ParameterizedTest
90+
@MethodSource("imageExtensions")
91+
void loadImage_withFile_mustReturnAValidLocalInputSource(String extension) throws IOException {
92+
File file = getResourcePath("file_types/receipt." + extension).toFile();
93+
var inputSource = new LocalInputSource(file);
94+
assertImage(inputSource, file.toPath(), "receipt." + extension);
9095
}
9196

92-
@Test
93-
void loadImage_withByteArray_mustReturnAValidLocalInputSource() throws IOException {
94-
Path filePath = getResourcePath("file_types/receipt.jpg");
95-
var localInputSource = new LocalInputSource(Files.readAllBytes(filePath), "receipt.jpg");
96-
assertImage(localInputSource, filePath);
97+
@ParameterizedTest
98+
@MethodSource("imageExtensions")
99+
void loadImage_withInputStream_mustReturnAValidLocalInputSource(
100+
String extension
101+
) throws IOException {
102+
Path filePath = getResourcePath("file_types/receipt." + extension);
103+
var inputSource = new LocalInputSource(Files.newInputStream(filePath), "receipt." + extension);
104+
assertImage(inputSource, filePath, "receipt." + extension);
105+
}
106+
107+
@ParameterizedTest
108+
@MethodSource("imageExtensions")
109+
void loadImage_withByteArray_mustReturnAValidLocalInputSource(
110+
String extension
111+
) throws IOException {
112+
Path filePath = getResourcePath("file_types/receipt." + extension);
113+
var inputSource = new LocalInputSource(Files.readAllBytes(filePath), "receipt." + extension);
114+
assertImage(inputSource, filePath, "receipt." + extension);
97115
}
98116

99117
@Test
100118
void loadImage_withBase64Encoded_mustReturnAValidLocalInputSource() throws IOException {
101-
Path filePath = getResourcePath("file_types/receipt.jpg");
102-
String encodedFile = Base64.encodeBase64String(Files.readAllBytes(filePath));
103-
var localInputSource = new LocalInputSource(encodedFile, "receipt.jpg");
104-
assertImage(localInputSource, filePath);
119+
Path filePath = getResourcePath("file_types/receipt.txt");
120+
String encodedFile = Files.readString(filePath, java.nio.charset.StandardCharsets.UTF_8);
121+
var inputSource = new LocalInputSource(encodedFile, "receipt.jpg");
122+
Assertions.assertFalse(inputSource.isPDF());
123+
Assertions.assertEquals(1, inputSource.getPageCount());
124+
Assertions.assertEquals("receipt.jpg", inputSource.getFilename());
105125
}
106126

107127
}

0 commit comments

Comments
 (0)