1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.notifications.notifiers.internal.email

File UserAvatarAttachmentExtractor.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
41% of files have more coverage

Code metrics

6
30
4
1
141
85
10
0.33
7.5
4
2.5

Classes

Class Line # Actions
UserAvatarAttachmentExtractor 53 30 0% 10 6
0.8585%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.notifications.notifiers.internal.email;
21   
22    import java.awt.image.BufferedImage;
23    import java.io.IOException;
24    import java.io.InputStream;
25    import java.io.OutputStream;
26   
27    import javax.imageio.ImageIO;
28    import javax.inject.Inject;
29    import javax.inject.Provider;
30    import javax.inject.Singleton;
31   
32    import org.apache.commons.io.IOUtils;
33    import org.slf4j.Logger;
34    import org.xwiki.component.annotation.Component;
35    import org.xwiki.environment.Environment;
36    import org.xwiki.model.reference.DocumentReference;
37   
38    import com.xpn.xwiki.XWiki;
39    import com.xpn.xwiki.XWikiContext;
40    import com.xpn.xwiki.api.Attachment;
41    import com.xpn.xwiki.doc.XWikiAttachment;
42    import com.xpn.xwiki.doc.XWikiAttachmentContent;
43    import com.xpn.xwiki.doc.XWikiDocument;
44   
45    import net.coobird.thumbnailator.Thumbnails;
46   
47    /**
48    * @version $Id: fe5ce77fd3101f0cb03fc3a7c5d5acc1d38c567e $
49    * @since 9.10RC1
50    */
51    @Component(roles = UserAvatarAttachmentExtractor.class)
52    @Singleton
 
53    public class UserAvatarAttachmentExtractor
54    {
55    /**
56    * Used to get file resources.
57    */
58    @Inject
59    private Environment environment;
60   
61    @Inject
62    private Logger logger;
63   
64    @Inject
65    private Provider<XWikiContext> xwikiContextProvider;
66   
67    /**
68    * @param userReference reference of the user
69    * @param size the wanted size for the image
70    * @return a fake attachment holding a resized and square-cropped version of the avatar of the given user
71    * @throws Exception if an error happens
72    */
 
73  1 toggle public Attachment getUserAvatar(DocumentReference userReference, int size) throws Exception
74    {
75  1 InputStream imageStream = null;
76  1 try {
77  1 imageStream = getUserAvatarStream(userReference);
78   
79  1 XWikiAttachment fakeAttachment = new XWikiAttachment();
80  1 XWikiAttachmentContent content = new XWikiAttachmentContent(fakeAttachment);
81   
82  1 resizeImage(imageStream, size, content.getContentOutputStream());
83   
84  1 fakeAttachment.setAttachment_content(content);
85  1 fakeAttachment.setFilename(
86  1 String.format("%s.jpg", userReference != null ? userReference.getName() : "XWikiGuest")
87    );
88   
89  1 return new Attachment(null, fakeAttachment, xwikiContextProvider.get());
90   
91    } catch (Exception e) {
92  0 throw new Exception(String.format("Failed to resize the avatar of [%s].", userReference), e);
93    } finally {
94  1 IOUtils.closeQuietly(imageStream);
95    }
96    }
97   
 
98  1 toggle private InputStream getUserAvatarStream(DocumentReference userReference)
99    {
100  1 if (userReference != null) {
101  1 try {
102   
103  1 XWikiContext context = xwikiContextProvider.get();
104  1 XWiki xwiki = context.getWiki();
105   
106  1 XWikiDocument userProfileDocument = xwiki.getDocument(userReference, context);
107  1 DocumentReference usersClassReference = xwiki.getUserClass(context).getDocumentReference();
108  1 String avatarFileName = userProfileDocument.getStringValue(usersClassReference, "avatar");
109  1 XWikiAttachment attachment = userProfileDocument.getAttachment(avatarFileName);
110   
111  1 if (attachment != null && attachment.isImage(context)) {
112  0 return attachment.getContentInputStream(context);
113    }
114    } catch (Exception e) {
115  0 logger.warn("Failed to get the avatar of [{}]. Fallback to default one.", userReference, e);
116    }
117    }
118   
119  1 return getDefaultAvatarStream();
120    }
121   
 
122  1 toggle private InputStream getDefaultAvatarStream()
123    {
124  1 return environment.getResourceAsStream("/resources/icons/xwiki/noavatar.png");
125    }
126   
 
127  1 toggle private void resizeImage(InputStream imageFileInputStream, int size, OutputStream outputStream) throws IOException
128    {
129  1 BufferedImage bufferedImage = ImageIO.read(imageFileInputStream);
130  1 int sourceWidth = bufferedImage.getWidth();
131  1 int sourceHeight = bufferedImage.getHeight();
132   
133  1 int smallestDimension = Math.min(sourceWidth, sourceHeight);
134   
135  1 Thumbnails.of(bufferedImage).sourceRegion(sourceWidth / 2 - smallestDimension / 2,
136    sourceHeight / 2 - smallestDimension / 2, smallestDimension, smallestDimension)
137    .forceSize(size, size).outputFormat("jpg").toOutputStream(outputStream);
138   
139  1 IOUtils.closeQuietly(outputStream);
140    }
141    }