Search in sources :

Example 1 with ImageSizeException

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;
}
Also used : ImageBag(com.liferay.portal.kernel.image.ImageBag) User(com.liferay.portal.model.User) ImageSizeException(com.liferay.portlet.documentlibrary.ImageSizeException) UserPortraitTypeException(com.liferay.portal.UserPortraitTypeException) IOException(java.io.IOException) RenderedImage(java.awt.image.RenderedImage) UserPortraitSizeException(com.liferay.portal.UserPortraitSizeException)

Aggregations

UserPortraitSizeException (com.liferay.portal.UserPortraitSizeException)1 UserPortraitTypeException (com.liferay.portal.UserPortraitTypeException)1 ImageBag (com.liferay.portal.kernel.image.ImageBag)1 User (com.liferay.portal.model.User)1 ImageSizeException (com.liferay.portlet.documentlibrary.ImageSizeException)1 RenderedImage (java.awt.image.RenderedImage)1 IOException (java.io.IOException)1