@RequestMapping(value = "/setting/avatar", method = RequestMethod.POST)
public @ResponseBody
AjaxResult changeAvatar(@Valid UserAvatarFormBean formBean,
BindingResult result, ModelAndView mav, HttpSession session) {
User signInUser = sessionUtil.getSignInUser(session);
if (signInUser == null) {
return new AjaxResult(AjaxResultCode.NEED_SIGNIN);
}
if (result.hasErrors()) {
return new AjaxResult(AjaxResultCode.INVALID,
BindingErrors.from(result));
}
try {
int idx = formBean.getImageUrl().lastIndexOf("/");
org.springframework.core.io.Resource resource = ac
.getResource(ApplicationConfig.uploadTempRepository + "/"
+ formBean.getImageUrl().substring(idx + 1));
File file = resource.getFile();
String ext = null;
if (file != null && ext == null) {
ext = FilenameUtils.getExtension(file.getName());
}
BufferedImage orgImg = ImageIO.read(file);
// save original avatar file
String resId = imageService.put(file);
Resource res = new Resource();
res.setOrgSize(new Integer[]{ orgImg.getHeight(), orgImg.getWidth() });
res.setResId(resId);
res.setExt(ext);
resourceRepository.save(res);
signInUser.setAvatarOrg(res);
// save avatar file
BufferedImage avatarImg = orgImg.getSubimage(formBean.getX(),
formBean.getY(), formBean.getW(), formBean.getH());
ImageIO.write(avatarImg, ext, file);
resId = imageService.put(file);
res = new Resource();
res.setOrgSize(new Integer[] { avatarImg.getHeight(), avatarImg.getWidth() });
res.setResId(resId);
res.setExt(ext);
resourceRepository.save(res);
signInUser.setAvatar(res);
userRepository.save(signInUser);
} catch (Exception e) {
return new AjaxResult(AjaxResultCode.EXCEPTION, e.getMessage());
}
return new AjaxResult(AjaxResultCode.SUCCESS);