-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageValidator.java
82 lines (64 loc) · 2.08 KB
/
ImageValidator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package qiwi.util.validation;
import com.google.common.collect.ImmutableSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import qiwi.core.util.converter.ValueConverter;
import javax.imageio.ImageReader;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.io.IOException;
import java.util.Set;
public class ImageValidator implements ConstraintValidator<ImageConstraint, byte[]> {
private static final Logger log = LoggerFactory.getLogger(ImageValidator.class);
private Set<String> formats;
private ImageConstraint constraint;
@Override
public void initialize(ImageConstraint constraint) {
this.constraint = constraint;
final ImmutableSet.Builder<String> builder = ImmutableSet.builder();
for (String format : constraint.formats()) {
builder.add(prepareFormat(format));
}
this.formats = builder.build();
}
@Override
public boolean isValid(@Nullable byte[] image, ConstraintValidatorContext context) {
return image != null && checkSize(image) && checkFormat(image);
}
private boolean checkSize(@NotNull byte[] image) {
return image.length <= constraint.size();
}
private boolean checkFormat(@NotNull byte[] image) {
@Nullable
final ImageReader reader = getImageReader(image);
if (reader == null) {
return false;
}
@Nullable
final String formatName = getFormatName(reader);
return formats.contains(prepareFormat(formatName));
}
private String prepareFormat(@Nullable String format) {
return format != null ? format.toLowerCase() : null;
}
@Nullable
private ImageReader getImageReader(@NotNull byte[] image) {
try {
return ValueConverter.convertTo(ImageReader.class, image);
} catch (Exception e) {
log.debug("ImageValidator getImageReader exception", e);
return null;
}
}
@Nullable
private String getFormatName(@NotNull ImageReader reader) {
try {
return reader.getFormatName();
} catch (IOException e) {
log.debug("ImageValidator getFormatName exception", e);
return null;
}
}
}