|
|
|
@ -25,6 +25,7 @@ import controller.BaseController;
|
|
|
|
|
|
|
|
|
|
import com.luoo.user.dto.response.UserRespDTO;
|
|
|
|
|
import com.luoo.user.pojo.UserInfo;
|
|
|
|
|
import com.luoo.user.service.S3Service;
|
|
|
|
|
import com.luoo.user.service.UserInfoService;
|
|
|
|
|
|
|
|
|
|
import annotation.GlobalInterceptor;
|
|
|
|
@ -47,9 +48,11 @@ import util.StringTools;
|
|
|
|
|
@Api(tags = "MyController")
|
|
|
|
|
public class MyController extends BaseController{
|
|
|
|
|
@Autowired
|
|
|
|
|
private S3Service s3Service;
|
|
|
|
|
@Autowired
|
|
|
|
|
private UserInfoService userInfoService;
|
|
|
|
|
|
|
|
|
|
public static String UPLOAD_DIRECTORY = System.getProperty("user.dir") + File.separator+"luoo_uploads"+File.separator;
|
|
|
|
|
public static String UPLOAD_DIRECTORY = "user/avatar/";
|
|
|
|
|
|
|
|
|
|
@ApiOperation(value = "1.获取个人信息", notes = "游客无法获取个人信息")
|
|
|
|
|
@GetMapping("/getUserInfo")
|
|
|
|
@ -104,38 +107,22 @@ public class MyController extends BaseController{
|
|
|
|
|
return Result.success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ApiOperation(value = "3.上传个人头像", notes = "图片压缩为70X70 JPEG,存入服务器用户目录/luoo_uploads/avatar/")
|
|
|
|
|
@ApiOperation(value = "3.上传个人头像", notes = "图片压缩为70X70 JPEG,存入S3,桶为indie,目录为 user/avatar/")
|
|
|
|
|
@PostMapping("/uploadAvatar")
|
|
|
|
|
@GlobalInterceptor(checkLogin = true)
|
|
|
|
|
public Result<Void> uploadAvatar(@RequestHeader(value = "token", required = false) String token,
|
|
|
|
|
@VerifyParam(required = true) MultipartFile file) throws IOException {
|
|
|
|
|
UserLoginDto userLoginDto = getUserLoginDto(token);
|
|
|
|
|
String folderName = UPLOAD_DIRECTORY + Constants.FOLDER_AVATAR;
|
|
|
|
|
File folder = new File(folderName);
|
|
|
|
|
if (!folder.exists()) {
|
|
|
|
|
java.nio.file.Files.createDirectories(Paths.get(folderName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] thumbnail =ScaleFilter.createThumbnail(file.getInputStream(), Constants.LENGTH_70, Constants.LENGTH_70);
|
|
|
|
|
String avatarName = userLoginDto.getUserId() + StringTools.getFileSuffix(file.getOriginalFilename());
|
|
|
|
|
File avatarFileName = new File(folder.getPath() + "/" + avatarName);
|
|
|
|
|
file.transferTo(avatarFileName);
|
|
|
|
|
ScaleFilter.createThumbnail(avatarFileName, Constants.LENGTH_70, Constants.LENGTH_70, avatarFileName);
|
|
|
|
|
String filePath=UPLOAD_DIRECTORY+avatarName;
|
|
|
|
|
s3Service.uploadAvatar("indie",filePath,thumbnail);
|
|
|
|
|
|
|
|
|
|
UserInfo user = userInfoService.findById(userLoginDto.getUserId());
|
|
|
|
|
user.setAvatar(Constants.FOLDER_AVATAR + avatarName);
|
|
|
|
|
user.setAvatar(filePath);
|
|
|
|
|
|
|
|
|
|
userInfoService.update(user);
|
|
|
|
|
return Result.success();
|
|
|
|
|
}
|
|
|
|
|
//待优化
|
|
|
|
|
@ApiOperation(value = "4.下载个人头像")
|
|
|
|
|
@GetMapping(value = "/downloadAvatar", produces = MediaType.IMAGE_JPEG_VALUE)
|
|
|
|
|
@GlobalInterceptor(checkLogin = true)
|
|
|
|
|
public @ResponseBody byte[] getImage(@RequestHeader(value = "token", required = false) String token)
|
|
|
|
|
throws IOException {
|
|
|
|
|
UserLoginDto userLoginDto = getUserLoginDto(token);
|
|
|
|
|
String folderName = UPLOAD_DIRECTORY + "avatar";
|
|
|
|
|
File folder = new File(folderName);
|
|
|
|
|
File avatarFileName = new File(folder.getPath() + File.separator + userLoginDto.getUserId() + ".JPG");
|
|
|
|
|
InputStream in = new FileInputStream(avatarFileName);
|
|
|
|
|
return IOUtils.toByteArray(in);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|