11package alexp .blog .controller ;
22
33import alexp .blog .model .User ;
4- import alexp .blog .service .AuthException ;
5- import alexp .blog .service . UserService ;
4+ import alexp .blog .service .* ;
5+ import alexp .blog .utils . JsonUtils ;
66import org .springframework .beans .factory .annotation .Autowired ;
77import org .springframework .security .access .prepost .PreAuthorize ;
88import org .springframework .stereotype .Controller ;
1212import org .springframework .validation .Validator ;
1313import org .springframework .validation .annotation .Validated ;
1414import org .springframework .web .bind .annotation .*;
15+ import org .springframework .web .multipart .MultipartFile ;
1516import org .springframework .web .servlet .mvc .support .RedirectAttributes ;
1617
1718import javax .servlet .http .HttpServletRequest ;
1819import javax .servlet .http .HttpSession ;
20+ import java .io .IOException ;
1921
2022@ Controller
2123public class UsersController {
2224
2325 @ Autowired
2426 private UserService userService ;
2527
28+ @ Autowired
29+ private AvatarService avatarService ;
30+
2631 @ Autowired
2732 private Validator userValidator ;
2833
@@ -160,8 +165,12 @@ public String showEditProfilePage(ModelMap model) {
160165 @ PreAuthorize ("hasRole('ROLE_USER')" )
161166 @ RequestMapping (value = "/edit_profile" , method = RequestMethod .POST )
162167 public String editProfile (@ Validated ({User .ProfileInfoValidationGroup .class }) @ ModelAttribute (value = "user" ) User user , BindingResult result ,
163- RedirectAttributes redirectAttributes , ModelMap model ) {
168+ RedirectAttributes redirectAttributes ) {
164169 if (result .hasErrors ()) {
170+ // quick workaround to show avatar
171+ User currentUser = userService .currentUser ();
172+ user .setBigAvatarLink (currentUser .getBigAvatarLink ());
173+
165174 return "editprofile" ;
166175 }
167176
@@ -172,6 +181,28 @@ public String editProfile(@Validated({User.ProfileInfoValidationGroup.class}) @M
172181 return "redirect:/edit_profile" ;
173182 }
174183
184+ @ PreAuthorize ("hasRole('ROLE_USER')" )
185+ @ RequestMapping (value = "/upload_avatar" , method = RequestMethod .POST )
186+ public @ ResponseBody String uploadAvatar (@ RequestParam ("avatarFile" ) MultipartFile file ) throws IOException {
187+ try {
188+ UploadedAvatarInfo result = avatarService .upload (file );
189+
190+ userService .changeAvatar (result );
191+
192+ return makeAvatarUploadResponse ("ok" , result );
193+ } catch (UnsupportedFormatException e ) {
194+ return makeAvatarUploadResponse ("invalid_format" , null );
195+ }
196+ }
197+
198+ @ PreAuthorize ("hasRole('ROLE_USER')" )
199+ @ RequestMapping (value = "/remove_avatar" , method = RequestMethod .POST )
200+ public @ ResponseBody String removeAvatar () throws IOException {
201+ userService .removeAvatar ();
202+
203+ return "ok" ;
204+ }
205+
175206 @ RequestMapping (value = "/users/{username}" , method = RequestMethod .GET )
176207 public String showProfile (@ PathVariable ("username" ) String username , ModelMap model ) {
177208 User user = userService .findByUsername (username );
@@ -183,4 +214,10 @@ public String showProfile(@PathVariable("username") String username, ModelMap mo
183214
184215 return "profile" ;
185216 }
217+
218+ private String makeAvatarUploadResponse (String status , UploadedAvatarInfo uploadedAvatarInfo ) {
219+ return "{" + JsonUtils .toJsonField ("status" , status ) +
220+ (uploadedAvatarInfo == null ? "" : (", " + JsonUtils .toJsonField ("link" , uploadedAvatarInfo .bigImageLink ))) +
221+ "}" ;
222+ }
186223}
0 commit comments