@@ -25,7 +25,15 @@ public class ProjectImageValidationService {
2525 private static final Pattern SVG_WIDTH_PATTERN = Pattern .compile ("width\\ s*=\\ s*['\" ]([^'\" ]+)['\" ]" , Pattern .CASE_INSENSITIVE );
2626 private static final Pattern SVG_HEIGHT_PATTERN = Pattern .compile ("height\\ s*=\\ s*['\" ]([^'\" ]+)['\" ]" , Pattern .CASE_INSENSITIVE );
2727
28+ public void validateImage (MultipartFile file , String type ) {
29+ validateImage (file , null , type , null );
30+ }
31+
2832 public void validateImage (MultipartFile file , double targetRatio , String type , String ratioLabel ) {
33+ validateImage (file , Double .valueOf (targetRatio ), type , ratioLabel );
34+ }
35+
36+ private void validateImage (MultipartFile file , Double targetRatio , String type , String ratioLabel ) {
2937 if (file .getSize () > MAX_IMAGE_FILE_SIZE ) {
3038 throw new InvalidProjectRequestException (type + " image size must not exceed 10MB." );
3139 }
@@ -61,7 +69,7 @@ public void validateImage(MultipartFile file, double targetRatio, String type, S
6169 }
6270
6371 double actualRatio = (double ) image .getWidth () / image .getHeight ();
64- if (Math .abs (actualRatio - targetRatio ) > 0.05 ) {
72+ if (targetRatio != null && Math .abs (actualRatio - targetRatio ) > 0.05 ) {
6573 throw new InvalidProjectRequestException (
6674 String .format ("%s image must have an aspect ratio of %s (Uploaded: %.2f)." , type , ratioLabel , actualRatio )
6775 );
@@ -88,7 +96,7 @@ private boolean isLikelySvg(MultipartFile file, byte[] bytes) {
8896 return sample .startsWith ("<?xml" ) || sample .contains ("<svg" );
8997 }
9098
91- private void validateSvgAspectRatio (byte [] bytes , String type , String ratioLabel , double targetRatio ) {
99+ private void validateSvgAspectRatio (byte [] bytes , String type , String ratioLabel , Double targetRatio ) {
92100 String svgText = new String (bytes , StandardCharsets .UTF_8 );
93101 Matcher svgTagMatcher = SVG_TAG_PATTERN .matcher (svgText );
94102 if (!svgTagMatcher .find ()) {
@@ -122,7 +130,7 @@ private void validateSvgAspectRatio(byte[] bytes, String type, String ratioLabel
122130 }
123131
124132 double actualRatio = width / height ;
125- if (Math .abs (actualRatio - targetRatio ) > 0.05 ) {
133+ if (targetRatio != null && Math .abs (actualRatio - targetRatio ) > 0.05 ) {
126134 throw new InvalidProjectRequestException (
127135 String .format ("%s image must have an aspect ratio of %s (Uploaded: %.2f)." , type , ratioLabel , actualRatio )
128136 );
0 commit comments