Skip to content

Commit ea51b38

Browse files
committed
/series/info: implement
1 parent c93e73a commit ea51b38

File tree

3 files changed

+111
-19
lines changed

3 files changed

+111
-19
lines changed

src/main/java/ru/mystamps/web/config/MvcConfig.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public void addInterceptors(InterceptorRegistry registry) {
130130

131131
registry
132132
.addInterceptor(getDownloadImageInterceptor())
133-
.addPathPatterns(Url.ADD_SERIES_PAGE);
133+
.addPathPatterns(Url.ADD_SERIES_PAGE, Url.ADD_IMAGE_SERIES_PAGE);
134134
}
135135

136136
@Override

src/main/java/ru/mystamps/web/controller/SeriesController.java

+37-2
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,47 @@ public String showInfo(
266266
return "series/info";
267267
}
268268

269-
@PostMapping(Url.ADD_IMAGE_SERIES_PAGE)
269+
@SuppressWarnings("checkstyle:parameternumber")
270+
@PostMapping(path = Url.ADD_IMAGE_SERIES_PAGE, params = "imageUrl")
271+
public String processImageWithImageUrl(
272+
@Validated({
273+
AddImageForm.ImageUrlChecks.class,
274+
AddImageForm.ImageChecks.class
275+
}) AddImageForm form,
276+
BindingResult result,
277+
@PathVariable("id") Integer seriesId,
278+
Model model,
279+
@CurrentUser Integer currentUserId,
280+
Locale userLocale,
281+
HttpServletRequest request,
282+
HttpServletResponse response)
283+
throws IOException {
284+
285+
return processImage(
286+
form,
287+
result,
288+
seriesId,
289+
model,
290+
currentUserId,
291+
userLocale,
292+
request,
293+
response
294+
);
295+
}
296+
297+
@SuppressWarnings("checkstyle:parameternumber")
298+
@PostMapping(path = Url.ADD_IMAGE_SERIES_PAGE, params = "!imageUrl")
270299
public String processImage(
271-
@Valid AddImageForm form,
300+
@Validated({
301+
AddImageForm.RequireImageCheck.class,
302+
AddImageForm.ImageChecks.class })
303+
AddImageForm form,
272304
BindingResult result,
273305
@PathVariable("id") Integer seriesId,
274306
Model model,
275307
@CurrentUser Integer currentUserId,
276308
Locale userLocale,
309+
HttpServletRequest request,
277310
HttpServletResponse response)
278311
throws IOException {
279312

@@ -289,6 +322,8 @@ public String processImage(
289322
return null;
290323
}
291324

325+
loadErrorsFromDownloadInterceptor(form, result, request);
326+
292327
boolean maxQuantityOfImagesExceeded = !isAdmin() && !isAllowedToAddingImages(series);
293328
model.addAttribute("maxQuantityOfImagesExceeded", maxQuantityOfImagesExceeded);
294329

src/main/java/ru/mystamps/web/controller/dto/AddImageForm.java

+73-16
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,96 @@
1818
package ru.mystamps.web.controller.dto;
1919

2020
import javax.validation.GroupSequence;
21-
import javax.validation.constraints.NotNull;
21+
22+
import org.apache.commons.lang3.StringUtils;
23+
24+
import org.hibernate.validator.constraints.URL;
2225

2326
import org.springframework.web.multipart.MultipartFile;
2427

2528
import lombok.Getter;
2629
import lombok.Setter;
2730

2831
import ru.mystamps.web.service.dto.AddImageDto;
32+
import ru.mystamps.web.support.beanvalidation.HasImageOrImageUrl;
2933
import ru.mystamps.web.support.beanvalidation.ImageFile;
3034
import ru.mystamps.web.support.beanvalidation.MaxFileSize;
3135
import ru.mystamps.web.support.beanvalidation.MaxFileSize.Unit;
3236
import ru.mystamps.web.support.beanvalidation.NotEmptyFile;
3337
import ru.mystamps.web.support.beanvalidation.NotEmptyFilename;
38+
import ru.mystamps.web.support.beanvalidation.RequireImageOrImageUrl;
3439

3540
import static ru.mystamps.web.validation.ValidationRules.MAX_IMAGE_SIZE;
3641

3742
@Getter
3843
@Setter
39-
@GroupSequence({
40-
AddImageForm.class,
41-
Group.Level1.class,
42-
Group.Level2.class,
43-
Group.Level3.class,
44-
Group.Level4.class,
45-
Group.Level5.class
46-
})
47-
public class AddImageForm implements AddImageDto {
48-
49-
@NotNull(groups = Group.Level1.class)
50-
@NotEmptyFilename(groups = Group.Level2.class)
51-
@NotEmptyFile(groups = Group.Level3.class)
52-
@MaxFileSize(value = MAX_IMAGE_SIZE, unit = Unit.Kbytes, groups = Group.Level4.class)
53-
@ImageFile(groups = Group.Level5.class)
44+
@RequireImageOrImageUrl(groups = AddImageForm.ImageUrl1Checks.class)
45+
public class AddImageForm implements AddImageDto, HasImageOrImageUrl, NullableImageUrl {
46+
47+
// Name of this field should match with the value of
48+
// DownloadImageInterceptor.UPLOADED_IMAGE_FIELD_NAME.
49+
@NotEmptyFilename(groups = RequireImageCheck.class)
50+
@NotEmptyFile(groups = Group.Level1.class)
51+
@MaxFileSize(value = MAX_IMAGE_SIZE, unit = Unit.Kbytes, groups = Group.Level2.class)
52+
@ImageFile(groups = Group.Level2.class)
5453
private MultipartFile image;
5554

55+
// Name of this field must match with the value of DownloadImageInterceptor.URL_PARAMETER_NAME.
56+
@URL(groups = ImageUrl2Checks.class)
57+
private String imageUrl;
58+
59+
// This field holds a file that was downloaded from imageUrl.
60+
// Name of this field must match with the value of
61+
// DownloadImageInterceptor.DOWNLOADED_IMAGE_FIELD_NAME.
62+
@NotEmptyFile(groups = Group.Level1.class)
63+
@MaxFileSize(value = MAX_IMAGE_SIZE, unit = Unit.Kbytes, groups = Group.Level2.class)
64+
@ImageFile(groups = Group.Level2.class)
65+
private MultipartFile downloadedImage;
66+
67+
@Override
68+
public MultipartFile getImage() {
69+
if (hasImage()) {
70+
return image;
71+
}
72+
73+
return downloadedImage;
74+
}
75+
76+
// This method has to be implemented to satisfy HasImageOrImageUrl requirements.
77+
// The latter is being used by RequireImageOrImageUrl validator.
78+
@Override
79+
public boolean hasImage() {
80+
return image != null && StringUtils.isNotEmpty(image.getOriginalFilename());
81+
}
82+
83+
// This method has to be implemented to satisfy HasImageOrImageUrl requirements.
84+
// The latter is being used by RequireImageOrImageUrl validator.
85+
@Override
86+
public boolean hasImageUrl() {
87+
return StringUtils.isNotEmpty(imageUrl);
88+
}
89+
90+
public interface RequireImageCheck {
91+
}
92+
93+
@GroupSequence({
94+
ImageUrl1Checks.class,
95+
ImageUrl2Checks.class,
96+
})
97+
public interface ImageUrlChecks {
98+
}
99+
100+
public interface ImageUrl1Checks {
101+
}
102+
103+
public interface ImageUrl2Checks {
104+
}
105+
106+
@GroupSequence({
107+
Group.Level1.class,
108+
Group.Level2.class
109+
})
110+
public interface ImageChecks {
111+
}
112+
56113
}

0 commit comments

Comments
 (0)