use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class DocumentSolrMetadataExtractorTest method createMockAttachment.
private XWikiAttachment createMockAttachment(String fileName, String mimeType, Date date, byte[] content, String authorAlias, String authorDisplayName) throws Exception {
XWikiAttachment attachment = mock(XWikiAttachment.class, fileName);
when(attachment.getReference()).thenReturn(new AttachmentReference(fileName, this.documentReference));
when(attachment.getFilename()).thenReturn(fileName);
when(attachment.getMimeType(this.xcontext)).thenReturn(mimeType);
when(attachment.getDate()).thenReturn(date);
when(attachment.getLongSize()).thenReturn((long) content.length);
when(attachment.getContentInputStream(this.xcontext)).thenReturn(new ByteArrayInputStream(content));
String authorFullName = "XWiki." + authorAlias;
DocumentReference authorReference = new DocumentReference("wiki", "XWiki", authorAlias);
when(attachment.getAuthorReference()).thenReturn(authorReference);
EntityReferenceSerializer<String> serializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING);
String authorStringReference = "wiki:" + authorFullName;
when(serializer.serialize(authorReference)).thenReturn(authorStringReference);
when(this.xcontext.getWiki().getPlainUserName(authorReference, this.xcontext)).thenReturn(authorDisplayName);
return attachment;
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class DocumentSolrMetadataExtractorTest method getDocumentWithAttachments.
@Test
public void getDocumentWithAttachments() throws Exception {
Date logoDate = new Date(123);
XWikiAttachment logo = createMockAttachment("logo.png", "image/png", logoDate, "foo", "Alice", "Shy Alice");
Date todoDate = new Date(456);
XWikiAttachment todo = createMockAttachment("todo.txt", "text/plain", todoDate, "bar bar", "Bob", "Angry Bob");
when(this.document.getAttachmentList()).thenReturn(Arrays.<XWikiAttachment>asList(logo, todo));
SolrInputDocument solrDocument = this.mocker.getComponentUnderTest().getSolrDocument(this.frenchDocumentReference);
assertEquals(Arrays.asList("logo.png", "todo.txt"), solrDocument.getFieldValues(FieldUtils.FILENAME));
assertEquals(Arrays.asList("image/png", "text/plain"), solrDocument.getFieldValues(FieldUtils.MIME_TYPE));
assertEquals(Arrays.asList(logoDate, todoDate), solrDocument.getFieldValues(FieldUtils.ATTACHMENT_DATE));
assertEquals(Arrays.asList(3L, 7L), solrDocument.getFieldValues(FieldUtils.ATTACHMENT_SIZE));
assertEquals(Arrays.asList("foo\n", "bar bar\n"), solrDocument.getFieldValues("attcontent_fr"));
assertEquals(Arrays.asList("wiki:XWiki.Alice", "wiki:XWiki.Bob"), solrDocument.getFieldValues(FieldUtils.ATTACHMENT_AUTHOR));
assertEquals(Arrays.asList("Shy Alice", "Angry Bob"), solrDocument.getFieldValues(FieldUtils.ATTACHMENT_AUTHOR_DISPLAY));
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class DefaultUserAvatarAttachmentExtractor method getUserAvatar.
@Override
public Attachment getUserAvatar(DocumentReference userReference, int width, int height, String fileName) {
// FIXME: Unfortunately, the ImagePlugin is too much request-oriented and not generic enough to be reused
// without rewriting. In the end, that might be the right way to go and rewriting it might be inevitable.
Attachment result = null;
InputStream sourceImageInputStream = null;
try {
XWikiContext context = xwikiContextProvider.get();
XWiki wiki = context.getWiki();
XWikiAttachment realAvatarAttachment;
XWikiAttachment fakeAvatarAttachment = null;
// Use a second variable to be able to reassign it on the else branch below.
DocumentReference actualUserReference = userReference;
if (actualUserReference != null) {
// Registered user.
XWikiDocument userProfileDocument = wiki.getDocument(userReference, context);
DocumentReference usersClassReference = wiki.getUserClass(context).getDocumentReference();
String avatarFileName = userProfileDocument.getStringValue(usersClassReference, "avatar");
realAvatarAttachment = userProfileDocument.getAttachment(avatarFileName);
if (realAvatarAttachment != null && realAvatarAttachment.isImage(context)) {
// Valid avatar, use the real attachment (and image), but make sure to use a clone as to not impact
// the
// real document.
fakeAvatarAttachment = (XWikiAttachment) realAvatarAttachment.clone();
sourceImageInputStream = realAvatarAttachment.getContentInputStream(context);
result = new Attachment(new Document(userProfileDocument, context), fakeAvatarAttachment, context);
} else {
// No or invalid avatar, treat the user reference as it did not exist (guest user) so it can be
// handled below for both cases.
actualUserReference = null;
}
}
if (actualUserReference == null) {
// Guest user.
// No avatar. Return a fake attachment with the "noavatar.png" standard image.
fakeAvatarAttachment = new XWikiAttachment();
sourceImageInputStream = environment.getResourceAsStream("/resources/icons/xwiki/noavatar.png");
result = new Attachment(null, fakeAvatarAttachment, context);
}
// In both cases, set an empty attachment content that will be filled with the resized image. This way we
// also avoid a request to the DB for the attachment content, since it will already be available.
fakeAvatarAttachment.setAttachment_content(new XWikiAttachmentContent(fakeAvatarAttachment));
// Resize the image and write it to the fake attachment.
int resizedWidth = 50;
int resizedHeight = 50;
resizeImageToAttachment(sourceImageInputStream, resizedWidth, resizedHeight, fakeAvatarAttachment);
// Set a fixed name for the user avatar file so that it is easy to work with in a template, for example.
fakeAvatarAttachment.setFilename(fileName);
} catch (Exception e) {
logger.error("Failed to retrieve the avatar for the user {}", userReference, e);
return null;
} finally {
// Close the source image input stream since we are done reading from it.
if (sourceImageInputStream != null) {
IOUtils.closeQuietly(sourceImageInputStream);
}
}
return result;
}
use of com.xpn.xwiki.doc.XWikiAttachment in project celements-blog by celements.
the class NewsletterAttachmentServiceTest method testClearAttachmentList_add_afterClear.
@Test
public void testClearAttachmentList_add_afterClear() throws Exception {
DocumentReference docRef = new DocumentReference(getContext().getDatabase(), "Test", "Img");
XWikiDocument doc = createMockAndAddToDefault(XWikiDocument.class);
XWikiAttachment att = new XWikiAttachment();
expect(attService.getAttachmentNameEqual(same(doc), eq("file.pdf"))).andReturn(att).anyTimes();
expect(attService.getApiAttachment(same(att))).andReturn(new Attachment(new Document(doc, getContext()), att, getContext())).anyTimes();
expect(xwiki.getDocument(eq(docRef), same(getContext()))).andReturn(doc).atLeastOnce();
replayDefault();
service.addAttachment("Test.Img;file.pdf");
service.clearAttachmentList();
service.addAttachment("Test.Img;file.pdf");
verifyDefault();
List<Attachment> atts = service.getAttachmentList(false);
assertNotNull(atts);
assertEquals(1, atts.size());
}
use of com.xpn.xwiki.doc.XWikiAttachment in project celements-blog by celements.
the class NewsletterAttachmentServiceTest method testGetEmbedAttList_validList.
@Test
public void testGetEmbedAttList_validList() {
VelocityContext vcontext = (VelocityContext) getContext().get("vcontext");
List<Attachment> list = new ArrayList<>();
Attachment att = new Attachment(null, new XWikiAttachment(), getContext());
list.add(att);
vcontext.put("nlEmbedAttList", list);
List<Attachment> resList = service.getAttachmentList(true);
assertNotNull(resList);
assertSame(att, resList.get(0));
}
Aggregations