Search in sources :

Example 1 with WrongFileFormatException

use of com.odysseusinc.arachne.portal.exception.WrongFileFormatException in project ArachneCentralAPI by OHDSI.

the class BaseUserServiceImpl method saveAvatar.

@Override
public void saveAvatar(U user, MultipartFile file) throws IOException, WrongFileFormatException, ImageProcessingException, MetadataException, IllegalAccessException, SolrServerException, NoSuchFieldException {
    String fileExt = FilenameUtils.getExtension(file.getOriginalFilename());
    BufferedImage img = ImageIO.read(file.getInputStream());
    if (img == null) {
        throw new WrongFileFormatException("file", "File format is not supported");
    }
    final File avatar = getUserAvatarFile(user);
    Metadata metadata = ImageMetadataReader.readMetadata(file.getInputStream());
    ExifIFD0Directory exifIFD0Directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
    int orientation = 1;
    try {
        orientation = exifIFD0Directory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
    } catch (Exception ignore) {
        LOGGER.debug(ignore.getMessage(), ignore);
    }
    List<Scalr.Rotation> rotations = new LinkedList<>();
    switch(orientation) {
        case 1:
            break;
        case // Flip X
        2:
            rotations.add(Scalr.Rotation.FLIP_HORZ);
            break;
        case // PI rotation
        3:
            rotations.add(Scalr.Rotation.CW_180);
            break;
        case // Flip Y
        4:
            rotations.add(Scalr.Rotation.FLIP_VERT);
            break;
        case // - PI/2 and Flip X
        5:
            rotations.add(Scalr.Rotation.CW_90);
            rotations.add(Scalr.Rotation.FLIP_HORZ);
            break;
        case // -PI/2 and -width
        6:
            rotations.add(Scalr.Rotation.CW_90);
            break;
        case // PI/2 and Flip
        7:
            rotations.add(Scalr.Rotation.CW_90);
            rotations.add(Scalr.Rotation.FLIP_VERT);
            break;
        case // PI / 2
        8:
            rotations.add(Scalr.Rotation.CW_270);
            break;
        default:
            break;
    }
    for (Scalr.Rotation rotation : rotations) {
        img = Scalr.rotate(img, rotation);
    }
    BufferedImage thumbnail = Scalr.resize(img, Math.min(Math.max(img.getHeight(), img.getWidth()), 640), Scalr.OP_ANTIALIAS);
    ImageIO.write(thumbnail, fileExt, avatar);
    user.setUpdated(new Date());
    U savedUser = userRepository.save(user);
    indexBySolr(savedUser);
}
Also used : WrongFileFormatException(com.odysseusinc.arachne.portal.exception.WrongFileFormatException) Metadata(com.drew.metadata.Metadata) BufferedImage(java.awt.image.BufferedImage) SolrServerException(org.apache.solr.client.solrj.SolrServerException) UserNotFoundException(com.odysseusinc.arachne.portal.exception.UserNotFoundException) MetadataException(com.drew.metadata.MetadataException) WrongFileFormatException(com.odysseusinc.arachne.portal.exception.WrongFileFormatException) IOException(java.io.IOException) PasswordValidationException(com.odysseusinc.arachne.portal.exception.PasswordValidationException) ImageProcessingException(com.drew.imaging.ImageProcessingException) NotUniqueException(com.odysseusinc.arachne.portal.exception.NotUniqueException) ValidationException(com.odysseusinc.arachne.portal.exception.ValidationException) ArachneSystemRuntimeException(com.odysseusinc.arachne.portal.exception.ArachneSystemRuntimeException) PermissionDeniedException(com.odysseusinc.arachne.portal.exception.PermissionDeniedException) NotExistException(com.odysseusinc.arachne.portal.exception.NotExistException) LinkedList(java.util.LinkedList) Date(java.util.Date) Scalr(org.imgscalr.Scalr) ExifIFD0Directory(com.drew.metadata.exif.ExifIFD0Directory) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile)

Aggregations

ImageProcessingException (com.drew.imaging.ImageProcessingException)1 Metadata (com.drew.metadata.Metadata)1 MetadataException (com.drew.metadata.MetadataException)1 ExifIFD0Directory (com.drew.metadata.exif.ExifIFD0Directory)1 ArachneSystemRuntimeException (com.odysseusinc.arachne.portal.exception.ArachneSystemRuntimeException)1 NotExistException (com.odysseusinc.arachne.portal.exception.NotExistException)1 NotUniqueException (com.odysseusinc.arachne.portal.exception.NotUniqueException)1 PasswordValidationException (com.odysseusinc.arachne.portal.exception.PasswordValidationException)1 PermissionDeniedException (com.odysseusinc.arachne.portal.exception.PermissionDeniedException)1 UserNotFoundException (com.odysseusinc.arachne.portal.exception.UserNotFoundException)1 ValidationException (com.odysseusinc.arachne.portal.exception.ValidationException)1 WrongFileFormatException (com.odysseusinc.arachne.portal.exception.WrongFileFormatException)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 IOException (java.io.IOException)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 SolrServerException (org.apache.solr.client.solrj.SolrServerException)1 Scalr (org.imgscalr.Scalr)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1