Skip to content

Commit c58dc51

Browse files
cssruphp-coder
authored andcommitted
Valdiate that uploaded files not greater than 500 Kb.
Fix #129
1 parent 3e0ae7f commit c58dc51

File tree

7 files changed

+118
-0
lines changed

7 files changed

+118
-0
lines changed

src/main/java/ru/mystamps/web/model/AddImageForm.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,21 @@
2727

2828
import ru.mystamps.web.service.dto.AddImageDto;
2929
import ru.mystamps.web.validation.jsr303.ImageFile;
30+
import ru.mystamps.web.validation.jsr303.MaxFileSize;
31+
import ru.mystamps.web.validation.jsr303.MaxFileSize.Unit;
3032
import ru.mystamps.web.validation.jsr303.NotEmptyFile;
3133
import ru.mystamps.web.validation.jsr303.NotEmptyFilename;
3234

35+
import static ru.mystamps.web.validation.ValidationRules.MAX_IMAGE_SIZE;
36+
3337
@Getter
3438
@Setter
3539
public class AddImageForm implements AddImageDto {
3640

3741
@NotNull
3842
@NotEmptyFilename(groups = Image1Checks.class)
3943
@NotEmptyFile(groups = Image2Checks.class)
44+
@MaxFileSize(value = MAX_IMAGE_SIZE, unit = Unit.Kbytes, groups = Image3Checks.class)
4045
@ImageFile(groups = Image3Checks.class)
4146
private MultipartFile image;
4247

src/main/java/ru/mystamps/web/model/AddSeriesForm.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@
3838
import ru.mystamps.web.service.dto.LinkEntityDto;
3939
import ru.mystamps.web.validation.jsr303.CatalogNumbers;
4040
import ru.mystamps.web.validation.jsr303.ImageFile;
41+
import ru.mystamps.web.validation.jsr303.MaxFileSize;
42+
import ru.mystamps.web.validation.jsr303.MaxFileSize.Unit;
4143
import ru.mystamps.web.validation.jsr303.NotEmptyFile;
4244
import ru.mystamps.web.validation.jsr303.NotEmptyFilename;
4345
import ru.mystamps.web.validation.jsr303.NotNullIfFirstField;
4446
import ru.mystamps.web.validation.jsr303.Price;
4547

4648
import static ru.mystamps.web.validation.ValidationRules.MAX_DAYS_IN_MONTH;
49+
import static ru.mystamps.web.validation.ValidationRules.MAX_IMAGE_SIZE;
4750
import static ru.mystamps.web.validation.ValidationRules.MAX_MONTHS_IN_YEAR;
4851
import static ru.mystamps.web.validation.ValidationRules.MAX_SERIES_COMMENT_LENGTH;
4952
import static ru.mystamps.web.validation.ValidationRules.MAX_STAMPS_IN_SERIES;
@@ -118,6 +121,7 @@ public class AddSeriesForm implements AddSeriesDto {
118121
@NotNull
119122
@NotEmptyFilename(groups = Image1Checks.class)
120123
@NotEmptyFile(groups = Image2Checks.class)
124+
@MaxFileSize(value = MAX_IMAGE_SIZE, unit = Unit.Kbytes, groups = Image3Checks.class)
121125
@ImageFile(groups = Image3Checks.class)
122126
private MultipartFile image;
123127

src/main/java/ru/mystamps/web/validation/ValidationRules.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public final class ValidationRules {
5555
public static final int MAX_SERIES_COMMENT_LENGTH = Series.COMMENT_LENGTH;
5656
public static final String CATALOG_NUMBERS_REGEXP = "[1-9][0-9]{0,3}(,[1-9][0-9]{0,3})*";
5757

58+
/** Maximum uploading image size in kilobytes. */
59+
public static final long MAX_IMAGE_SIZE = 500;
60+
5861
public static final int MAX_DAYS_IN_MONTH = 31;
5962
public static final int MAX_MONTHS_IN_YEAR = 12;
6063

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (C) 2009-2016 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.validation.jsr303;
19+
20+
import java.lang.annotation.Documented;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.Target;
23+
24+
import javax.validation.Constraint;
25+
import javax.validation.Payload;
26+
27+
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
28+
import static java.lang.annotation.ElementType.FIELD;
29+
import static java.lang.annotation.ElementType.METHOD;
30+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
31+
32+
/**
33+
* @author Sergey Chechenev
34+
*/
35+
@Target({ METHOD, FIELD, ANNOTATION_TYPE })
36+
@Retention(RUNTIME)
37+
@Constraint(validatedBy = MaxFileSizeValidator.class)
38+
@Documented
39+
public @interface MaxFileSize {
40+
String message() default "{ru.mystamps.web.validation.jsr303.MaxFileSize.message}";
41+
Class<?>[] groups() default {};
42+
Class<? extends Payload>[] payload() default {};
43+
long value();
44+
Unit unit();
45+
46+
enum Unit {
47+
bytes(1), Kbytes(1024), Mbytes(1024 * 1024);
48+
49+
private long size;
50+
51+
Unit(long size) {
52+
this.size = size;
53+
}
54+
55+
public long getSize() {
56+
return size;
57+
}
58+
}
59+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (C) 2009-2016 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.validation.jsr303;
19+
20+
import javax.validation.ConstraintValidator;
21+
import javax.validation.ConstraintValidatorContext;
22+
23+
import org.springframework.web.multipart.MultipartFile;
24+
25+
/**
26+
* @author Sergey Chechenev
27+
*/
28+
public class MaxFileSizeValidator implements ConstraintValidator<MaxFileSize, MultipartFile> {
29+
private long maxFileSizeInBytes;
30+
31+
@Override
32+
public void initialize(MaxFileSize annotation) {
33+
maxFileSizeInBytes = annotation.value() * annotation.unit().getSize();
34+
}
35+
36+
@Override
37+
public boolean isValid(MultipartFile file, ConstraintValidatorContext context) {
38+
39+
if (file == null) {
40+
return true;
41+
}
42+
43+
return file.getSize() <= maxFileSizeInBytes;
44+
}
45+
}

src/main/resources/ru/mystamps/i18n/ValidationMessages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ ru.mystamps.web.validation.jsr303.CatalogNumbers.message = Value must be comma d
1717
ru.mystamps.web.validation.jsr303.NotNullIfFirstField.message = Field '{second}' must not be empty
1818
ru.mystamps.web.validation.jsr303.Price.message = Invalid value
1919
ru.mystamps.web.validation.jsr303.ImageFile.message = Cannot detect file type. Must be image in JPEG or PNG format
20+
ru.mystamps.web.validation.jsr303.MaxFileSize.message = File size must be not greater than {value} ${unit.name()}
2021

2122
value.too-short = Value is less than allowable minimum of {min} characters
2223
value.too-long = Value is greater than allowable maximum of {max} characters

src/main/resources/ru/mystamps/i18n/ValidationMessages_ru.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ ru.mystamps.web.validation.jsr303.CatalogNumbers.message = Значение до
1717
ru.mystamps.web.validation.jsr303.NotNullIfFirstField.message = Поле '{second}' обязательно для заполнения
1818
ru.mystamps.web.validation.jsr303.Price.message = Некорректное значение
1919
ru.mystamps.web.validation.jsr303.ImageFile.message = Не удалось определить тип файла. Должен быть изображением в формате JPEG или PNG
20+
ru.mystamps.web.validation.jsr303.MaxFileSize.message = Размер файла должен быть не более {value} ${unit.name().equals('Mbytes') ? 'Мбайт' : unit.name().equals('Kbytes') ? 'Кбайт' : 'байт'}
2021

2122
value.too-short = Значение должно быть не менее {min} символов
2223
value.too-long = Значение должно быть не более {max} символов

0 commit comments

Comments
 (0)