|
|
@@ -1,15 +1,19 @@
|
|
|
package com.fs.company.controller.company;
|
|
|
|
|
|
import com.fs.common.annotation.Log;
|
|
|
-import com.fs.common.config.FSConfig;
|
|
|
import com.fs.common.core.controller.BaseController;
|
|
|
import com.fs.common.core.domain.AjaxResult;
|
|
|
import com.fs.common.core.redis.RedisCache;
|
|
|
import com.fs.common.enums.BusinessType;
|
|
|
import com.fs.common.enums.ImTypeEnum;
|
|
|
+import com.fs.common.exception.file.FileNameLengthLimitExceededException;
|
|
|
+import com.fs.common.exception.file.FileSizeLimitExceededException;
|
|
|
+import com.fs.common.exception.file.InvalidExtensionException;
|
|
|
import com.fs.common.utils.PatternUtils;
|
|
|
import com.fs.common.utils.ServletUtils;
|
|
|
+import com.fs.common.utils.StringUtils;
|
|
|
import com.fs.common.utils.file.FileUploadUtils;
|
|
|
+import com.fs.common.utils.file.MimeTypeUtils;
|
|
|
import com.fs.company.domain.CompanyUser;
|
|
|
import com.fs.company.param.CompanyUserEditParam;
|
|
|
import com.fs.company.service.ICompanyUserService;
|
|
|
@@ -18,14 +22,13 @@ import com.fs.framework.security.SecurityUtils;
|
|
|
import com.fs.framework.service.TokenService;
|
|
|
import com.fs.im.config.ImTypeConfig;
|
|
|
import com.fs.im.service.OpenIMService;
|
|
|
-import com.fs.system.service.ISysConfigService;
|
|
|
+import com.fs.system.oss.CloudStorageService;
|
|
|
+import com.fs.system.oss.OSSFactory;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
-
|
|
|
/**
|
|
|
* 个人信息
|
|
|
*/
|
|
|
@@ -135,22 +138,48 @@ public class CompanyProfileController extends BaseController
|
|
|
*/
|
|
|
@Log(title = "用户头像", businessType = BusinessType.UPDATE)
|
|
|
@PostMapping("/avatar")
|
|
|
- public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException
|
|
|
+ public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file)
|
|
|
{
|
|
|
- if (!file.isEmpty())
|
|
|
+ try
|
|
|
{
|
|
|
+ if (file.isEmpty())
|
|
|
+ {
|
|
|
+ return AjaxResult.error("上传图片异常,请联系管理员");
|
|
|
+ }
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
+ if (originalFilename != null && originalFilename.length() > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
|
|
|
+ {
|
|
|
+ return AjaxResult.error("文件名无效或过长");
|
|
|
+ }
|
|
|
+ FileUploadUtils.assertAllowed(file, MimeTypeUtils.IMAGE_EXTENSION);
|
|
|
+ String ext = FileUploadUtils.getExtension(file);
|
|
|
+ if (StringUtils.isEmpty(ext))
|
|
|
+ {
|
|
|
+ ext = "jpg";
|
|
|
+ }
|
|
|
+ String suffix = "." + ext;
|
|
|
+ CloudStorageService storage = OSSFactory.build();
|
|
|
+ String avatar = storage.uploadSuffix(file.getBytes(), suffix);
|
|
|
+
|
|
|
LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
|
|
|
- String avatar = FileUploadUtils.upload(FSConfig.getAvatarPath(), file);
|
|
|
- if (userService.updateUserAvatar(loginUser.getUsername(), avatar)>0)
|
|
|
+ if (userService.updateUserAvatar(loginUser.getUsername(), avatar) > 0)
|
|
|
{
|
|
|
AjaxResult ajax = AjaxResult.success();
|
|
|
ajax.put("imgUrl", avatar);
|
|
|
- // 更新缓存用户头像
|
|
|
loginUser.getUser().setAvatar(avatar);
|
|
|
tokenService.setLoginUser(loginUser);
|
|
|
return ajax;
|
|
|
}
|
|
|
}
|
|
|
+ catch (InvalidExtensionException | FileSizeLimitExceededException | FileNameLengthLimitExceededException e)
|
|
|
+ {
|
|
|
+ return AjaxResult.error(e.getMessage());
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ logger.error("头像上传失败", e);
|
|
|
+ return AjaxResult.error("上传图片异常,请联系管理员");
|
|
|
+ }
|
|
|
return AjaxResult.error("上传图片异常,请联系管理员");
|
|
|
}
|
|
|
}
|