Search in sources :

Example 1 with Attachment

use of org.mamute.model.Attachment in project mamute by caelum.

the class LDAPApi method updateAvatarImage.

private void updateAvatarImage(LDAPResource ldap, Entry entry, User user) {
    try {
        byte[] jpegBytes = getAvatarImage(ldap, entry);
        if (jpegBytes != null) {
            String fileName = user.getEmail() + ".jpg";
            DefaultUploadedFile avatar = new DefaultUploadedFile(new ByteArrayInputStream(jpegBytes), fileName, "image/jpeg", jpegBytes.length);
            Attachment attachment = imageStore.processAndStore(avatar, user, clientIp);
            Attachment old = user.getAvatar();
            if (old != null) {
                imageStore.delete(old);
            }
            user.setAvatar(attachment);
        }
    } catch (LdapException | IOException e) {
        // problems with avatar processing are non-fatal
        logger.warn("Error updating user avatar from LDAP: " + user.getName(), e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultUploadedFile(br.com.caelum.vraptor.observer.upload.DefaultUploadedFile) Attachment(org.mamute.model.Attachment) IOException(java.io.IOException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 2 with Attachment

use of org.mamute.model.Attachment in project mamute by caelum.

the class AttachmentRepositoryTest method should_delete_database_entry.

@Test
public void should_delete_database_entry() {
    final Attachment attachment = createAttachment("test");
    attachmentRepository.save(attachment);
    attachmentRepository.delete(attachment);
    assertNull(attachments.load(attachment.getId()));
}
Also used : Attachment(org.mamute.model.Attachment) Test(org.junit.Test)

Example 3 with Attachment

use of org.mamute.model.Attachment in project mamute by caelum.

the class AttachmentRepositoryTest method save_should_create_database_entry.

@Test
public void save_should_create_database_entry() {
    final Attachment attachment = createAttachment("test");
    attachmentRepository.save(attachment);
    assertNotNull(attachment.getId());
    assertEquals(attachment, attachments.load(attachment.getId()));
}
Also used : Attachment(org.mamute.model.Attachment) Test(org.junit.Test)

Example 4 with Attachment

use of org.mamute.model.Attachment in project mamute by caelum.

the class ImageStore method processAndStore.

public Attachment processAndStore(UploadedFile avatar, User user, ClientIp clientIp) throws IOException {
    BufferedImage resized = processImage(avatar);
    Attachment attachment = new Attachment(resized, user, clientIp.get(), avatar.getFileName());
    attachments.save(attachment);
    fileStorage.saveImage(attachment);
    return attachment;
}
Also used : Attachment(org.mamute.model.Attachment) BufferedImage(java.awt.image.BufferedImage)

Example 5 with Attachment

use of org.mamute.model.Attachment in project mamute by caelum.

the class AttachmentRepositoryTest method save_should_write_contents_to_disk.

@Test
public void save_should_write_contents_to_disk() throws IOException {
    final String fileContents = "hello";
    final Attachment attachment = createAttachment(fileContents);
    attachmentRepository.save(attachment);
    assertEquals(fileContents, IOUtils.toString(fileStorage.open(attachment)));
}
Also used : Attachment(org.mamute.model.Attachment) Test(org.junit.Test)

Aggregations

Attachment (org.mamute.model.Attachment)8 Test (org.junit.Test)5 User (org.mamute.model.User)3 DefaultUploadedFile (br.com.caelum.vraptor.observer.upload.DefaultUploadedFile)2 Question (org.mamute.model.Question)2 Tag (org.mamute.model.Tag)2 UploadedFile (br.com.caelum.vraptor.observer.upload.UploadedFile)1 BufferedImage (java.awt.image.BufferedImage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)1 Answer (org.mamute.model.Answer)1