use of com.liferay.portlet.documentlibrary.ImageSizeException in project liferay-ide by liferay.
the class UserLocalServiceImpl method updatePortrait.
/**
* Updates the user's portrait image.
*
* @param userId the primary key of the user
* @param bytes the new portrait image data
* @return the user
* @throws PortalException if a user with the primary key could not be found
* or if the new portrait was invalid
* @throws SystemException if a system exception occurred
*/
@Override
public User updatePortrait(long userId, byte[] bytes) throws PortalException, SystemException {
User user = userPersistence.findByPrimaryKey(userId);
long imageMaxSize = PrefsPropsUtil.getLong(PropsKeys.USERS_IMAGE_MAX_SIZE);
if ((imageMaxSize > 0) && ((bytes == null) || (bytes.length > imageMaxSize))) {
throw new UserPortraitSizeException();
}
long portraitId = user.getPortraitId();
if (portraitId <= 0) {
portraitId = counterLocalService.increment();
user.setPortraitId(portraitId);
}
try {
ImageBag imageBag = ImageToolUtil.read(bytes);
RenderedImage renderedImage = imageBag.getRenderedImage();
if (renderedImage == null) {
throw new UserPortraitTypeException();
}
renderedImage = ImageToolUtil.scale(renderedImage, PropsValues.USERS_IMAGE_MAX_HEIGHT, PropsValues.USERS_IMAGE_MAX_WIDTH);
String contentType = imageBag.getType();
imageLocalService.updateImage(portraitId, ImageToolUtil.getBytes(renderedImage, contentType));
} catch (IOException ioe) {
throw new ImageSizeException(ioe);
}
userPersistence.update(user);
return user;
}
Aggregations