Skip to content

Commit dafbc45

Browse files
committed
/series/add: accept more JPEG files.
1 parent 951c65e commit dafbc45

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/main/java/ru/mystamps/web/validation/jsr303/ImageFileValidator.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ public class ImageFileValidator implements ConstraintValidator<ImageFile, Multip
3434
private static final Logger LOG = LoggerFactory.getLogger(ImageFileValidator.class);
3535

3636
// see https://en.wikipedia.org/wiki/JPEG#Syntax_and_structure
37-
private static final byte[] JPEG_SIGNATURE = new byte[] {
38-
(byte)0xFF, (byte)0xD8, (byte)0xFF, (byte)0xE0
37+
private static final byte[][] JPEG_SIGNATURES = {
38+
{ (byte)0xFF, (byte)0xD8, (byte)0xFF, (byte)0xE0 },
39+
{ (byte)0xFF, (byte)0xD8, (byte)0xFF, (byte)0xE1 }
3940
};
4041

4142
// see https://en.wikipedia.org/wiki/Portable_Network_Graphics#File_header
@@ -49,7 +50,14 @@ public class ImageFileValidator implements ConstraintValidator<ImageFile, Multip
4950

5051
private static boolean isJpeg(byte[] bytes) {
5152
// TODO: also check that last 2 bytes are FF D9 (use RandomAccessFile)
52-
return Arrays.equals(bytes, JPEG_SIGNATURE);
53+
54+
for (byte[] signature: JPEG_SIGNATURES) {
55+
if (Arrays.equals(bytes, signature)) {
56+
return true;
57+
}
58+
}
59+
60+
return false;
5361
}
5462

5563
private static boolean doesItLookLikePng(byte[] bytes) {

0 commit comments

Comments
 (0)