1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.watchlist.internal; |
21 |
|
|
22 |
|
import java.awt.Image; |
23 |
|
import java.awt.image.RenderedImage; |
24 |
|
import java.io.IOException; |
25 |
|
import java.io.InputStream; |
26 |
|
import java.io.OutputStream; |
27 |
|
|
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 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
38 |
|
|
39 |
|
import com.xpn.xwiki.XWiki; |
40 |
|
import com.xpn.xwiki.XWikiContext; |
41 |
|
import com.xpn.xwiki.api.Attachment; |
42 |
|
import com.xpn.xwiki.api.Document; |
43 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
44 |
|
import com.xpn.xwiki.doc.XWikiAttachmentContent; |
45 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
46 |
|
import com.xpn.xwiki.plugin.image.ImageProcessor; |
47 |
|
import com.xpn.xwiki.user.api.XWikiRightService; |
48 |
|
|
49 |
|
|
50 |
|
@link |
51 |
|
|
52 |
|
@version |
53 |
|
@since |
54 |
|
|
55 |
|
@Component |
56 |
|
@Singleton |
|
|
| 81.2% |
Uncovered Elements: 12 (64) |
Complexity: 12 |
Complexity Density: 0.25 |
|
57 |
|
public class DefaultUserAvatarAttachmentExtractor implements UserAvatarAttachmentExtractor |
58 |
|
{ |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
@Inject |
63 |
|
private Logger logger; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
@Inject |
69 |
|
private Environment environment; |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
@Inject |
75 |
|
private ImageProcessor imageProcessor; |
76 |
|
|
77 |
|
@Inject |
78 |
|
private Provider<XWikiContext> xwikiContextProvider; |
79 |
|
|
80 |
|
@Inject |
81 |
|
private EntityReferenceSerializer<String> serializer; |
82 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
83 |
1 |
@Override... |
84 |
|
public Attachment getUserAvatar(DocumentReference userReference) |
85 |
|
{ |
86 |
1 |
String fileName = getFileName(userReference); |
87 |
1 |
return getUserAvatar(userReference, 50, 50, fileName); |
88 |
|
} |
89 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
90 |
1 |
private String getFileName(DocumentReference userReference)... |
91 |
|
{ |
92 |
1 |
String fileName; |
93 |
1 |
if (userReference == null) { |
94 |
0 |
fileName = XWikiRightService.GUEST_USER_FULLNAME; |
95 |
|
} else { |
96 |
1 |
fileName = serializer.serialize(userReference); |
97 |
|
} |
98 |
1 |
fileName = String.format("%s.png", fileName); |
99 |
1 |
return fileName; |
100 |
|
} |
101 |
|
|
|
|
| 77.5% |
Uncovered Elements: 9 (40) |
Complexity: 7 |
Complexity Density: 0.22 |
|
102 |
1 |
@Override... |
103 |
|
public Attachment getUserAvatar(DocumentReference userReference, int width, int height, String fileName) |
104 |
|
{ |
105 |
|
|
106 |
|
|
107 |
|
|
108 |
1 |
Attachment result = null; |
109 |
|
|
110 |
1 |
InputStream sourceImageInputStream = null; |
111 |
1 |
try { |
112 |
1 |
XWikiContext context = xwikiContextProvider.get(); |
113 |
1 |
XWiki wiki = context.getWiki(); |
114 |
|
|
115 |
1 |
XWikiAttachment realAvatarAttachment; |
116 |
1 |
XWikiAttachment fakeAvatarAttachment = null; |
117 |
|
|
118 |
|
|
119 |
1 |
DocumentReference actualUserReference = userReference; |
120 |
1 |
if (actualUserReference != null) { |
121 |
|
|
122 |
|
|
123 |
1 |
XWikiDocument userProfileDocument = wiki.getDocument(userReference, context); |
124 |
|
|
125 |
1 |
DocumentReference usersClassReference = wiki.getUserClass(context).getDocumentReference(); |
126 |
1 |
String avatarFileName = userProfileDocument.getStringValue(usersClassReference, "avatar"); |
127 |
|
|
128 |
1 |
realAvatarAttachment = userProfileDocument.getAttachment(avatarFileName); |
129 |
|
|
130 |
1 |
if (realAvatarAttachment != null && realAvatarAttachment.isImage(context)) { |
131 |
|
|
132 |
|
|
133 |
|
|
134 |
0 |
fakeAvatarAttachment = (XWikiAttachment) realAvatarAttachment.clone(); |
135 |
0 |
sourceImageInputStream = realAvatarAttachment.getContentInputStream(context); |
136 |
|
|
137 |
0 |
result = new Attachment(new Document(userProfileDocument, context), fakeAvatarAttachment, context); |
138 |
|
} else { |
139 |
|
|
140 |
|
|
141 |
1 |
actualUserReference = null; |
142 |
|
} |
143 |
|
} |
144 |
|
|
145 |
1 |
if (actualUserReference == null) { |
146 |
|
|
147 |
|
|
148 |
|
|
149 |
1 |
fakeAvatarAttachment = new XWikiAttachment(); |
150 |
1 |
sourceImageInputStream = environment.getResourceAsStream("/resources/icons/xwiki/noavatar.png"); |
151 |
|
|
152 |
1 |
result = new Attachment(null, fakeAvatarAttachment, context); |
153 |
|
} |
154 |
|
|
155 |
|
|
156 |
|
|
157 |
1 |
fakeAvatarAttachment.setAttachment_content(new XWikiAttachmentContent(fakeAvatarAttachment)); |
158 |
|
|
159 |
|
|
160 |
1 |
int resizedWidth = 50; |
161 |
1 |
int resizedHeight = 50; |
162 |
1 |
resizeImageToAttachment(sourceImageInputStream, resizedWidth, resizedHeight, fakeAvatarAttachment); |
163 |
|
|
164 |
|
|
165 |
1 |
fakeAvatarAttachment.setFilename(fileName); |
166 |
|
} catch (Exception e) { |
167 |
0 |
logger.error("Failed to retrieve the avatar for the user {}", userReference, e); |
168 |
0 |
return null; |
169 |
|
} finally { |
170 |
|
|
171 |
1 |
if (sourceImageInputStream != null) { |
172 |
1 |
IOUtils.closeQuietly(sourceImageInputStream); |
173 |
|
} |
174 |
|
} |
175 |
|
|
176 |
1 |
return result; |
177 |
|
} |
178 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
179 |
1 |
private void resizeImageToAttachment(InputStream imageFileInputStream, int width, int height,... |
180 |
|
XWikiAttachment outputAttachment) throws IOException |
181 |
|
{ |
182 |
1 |
OutputStream attachmentOutputStream = null; |
183 |
1 |
try { |
184 |
1 |
Image originalImage = imageProcessor.readImage(imageFileInputStream); |
185 |
|
|
186 |
1 |
RenderedImage resizedImage = imageProcessor.scaleImage(originalImage, width, height); |
187 |
|
|
188 |
1 |
attachmentOutputStream = outputAttachment.getAttachment_content().getContentOutputStream(); |
189 |
1 |
imageProcessor.writeImage(resizedImage, "image/png", 1.0f, attachmentOutputStream); |
190 |
|
} finally { |
191 |
|
|
192 |
1 |
if (attachmentOutputStream != null) { |
193 |
1 |
IOUtils.closeQuietly(attachmentOutputStream); |
194 |
|
} |
195 |
|
} |
196 |
|
} |
197 |
|
} |