@RequestMapping(value = "/setportrait/{photoId}", method = RequestMethod.POST)
public @ResponseBody
Map<String, Object> changeAsUserPortrait(@PathVariable("photoId") long photoId) {
Map<String, Object> result = new HashMap<String, Object>();
User user = (User) WebHelper.getSessionAttribute(null, Constant.SESSION_USER);
if (user == null) {
result.put("success", Boolean.FALSE);
result.put("errmsg", "设置头像必须登录");
result.put("logined", Boolean.FALSE);
return result;
}
result.put("logined", Boolean.TRUE);
try {
Long ownerId = user.getId();
Photo photo = albumService.getPhotoForChange(ownerId, photoId);
Album portraitAlbum = albumService.getUserPortraitAlbum(ownerId);
Long albumId = portraitAlbum.getId();
String photoExt = photo.getExt();
String remoteFileKey = photo.getSmallFile() + '.' + photoExt;
if (SkylineImagineUtils.isExtMultiFrame(photoExt)) {// 如果是动态图,需要压缩成单帧图
remoteFileKey = photo.getMiddleFile() + '.' + photoExt;
String filename = FilenameUtils.getName(remoteFileKey);
String fileKey = SkylineImagineUtils.generateFileKey(ownerId.toString(),
albumId.toString(), filename);
String localFile = SkylineImagineUtils.generateFilePath(localStorePath, ownerId,
albumId, fileKey + '.' + photoExt);
File destFile = new File(localFile);
File dir = destFile.getParentFile();
if (!dir.exists()) {
boolean success = dir.mkdirs();
if (!success && !dir.exists()) {
LOGGER.warn("mkdirs :" + dir + " failed");
}
}
// 保存服务器上的图片到本地目标文件
if (fileOperator == null) {
String srcFile = localStorePath + '/' + remoteFileKey;
FileUtils.copyFile(new File(srcFile), destFile);
} else {
fileOperator.downloadFile(remoteFileKey, localFile);
}
// 压缩该目标图片
LocalImageResizeTask task = new LocalImageResizeTask(localFile, baseSizes);
task.setAlbumId(albumId);
task.setUserId(ownerId);
task.setSupportMulitFrame(false);
ImageResizeResult resizeResult = imagine.processSingleImage(localStorePath, task);
Photo portrait = albumService
.createPortraitPhoto(user, portraitAlbum, resizeResult);
remoteFileKey = portrait.getSmallFile() + '.' + portrait.getExt();
} else {
albumService.copyPhotoToPortraitAlbum(portraitAlbum, photo);
}
SkylineImageCropTask cropTask = new SkylineImageCropTask(remoteFileKey, portraitSize);
cropTask.setAlbumId(albumId);
cropTask.setUserId(ownerId);
ImageCropResult cropResult = crop.processImage(localStorePath, cropTask);
String portraitFile = cropResult.getFileKey();
personalInfoService.changeUserPortrait(ownerId, portraitFile);
user.setPortrait(portraitFile);
WebHelper.setSessionAttribute(null, Constant.SESSION_USER, user);
result.put("success", Boolean.TRUE);
result.put("errmsg", "设置头像成功");
return result;