Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a07d56b

Browse files
committedNov 29, 2015
ImageFileValidator: add checking for content type.
1 parent 599f59b commit a07d56b

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed
 

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public class ImageFileValidator implements ConstraintValidator<ImageFile, Multip
3333

3434
private static final Logger LOG = LoggerFactory.getLogger(ImageFileValidator.class);
3535

36+
private static final String JPEG_CONTENT_TYPE = "image/jpeg";
37+
private static final String PNG_CONTENT_TYPE = "image/png";
38+
3639
// see https://en.wikipedia.org/wiki/JPEG#Syntax_and_structure
3740
// CheckStyle: ignore NoWhitespaceAfterCheck for next 3 lines
3841
private static final byte[][] JPEG_SIGNATURES = {
@@ -97,6 +100,7 @@ public void initialize(ImageFile annotation) {
97100
}
98101

99102
@Override
103+
@SuppressWarnings("PMD.CyclomaticComplexity")
100104
public boolean isValid(MultipartFile file, ConstraintValidatorContext ctx) {
101105

102106
if (file == null) {
@@ -106,6 +110,13 @@ public boolean isValid(MultipartFile file, ConstraintValidatorContext ctx) {
106110
if (file.isEmpty()) {
107111
return false;
108112
}
113+
114+
String contentType = file.getContentType();
115+
if (!PNG_CONTENT_TYPE.equals(contentType)
116+
&& !JPEG_CONTENT_TYPE.equals(file.getContentType())) {
117+
LOG.debug("Reject file with content type '{}'", contentType);
118+
return false;
119+
}
109120

110121
try (InputStream stream = file.getInputStream()) {
111122

0 commit comments

Comments
 (0)
Please sign in to comment.