Search in sources :

Example 61 with XWikiAttachment

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;
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ByteArrayInputStream(java.io.ByteArrayInputStream) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 62 with XWikiAttachment

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));
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Date(java.util.Date) Test(org.junit.Test)

Example 63 with XWikiAttachment

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;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) InputStream(java.io.InputStream) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(com.xpn.xwiki.api.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiAttachmentContent(com.xpn.xwiki.doc.XWikiAttachmentContent) IOException(java.io.IOException)

Example 64 with XWikiAttachment

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());
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(com.xpn.xwiki.api.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 65 with XWikiAttachment

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));
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) ArrayList(java.util.ArrayList) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(com.xpn.xwiki.api.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Test(org.junit.Test)

Aggregations

XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)133 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)71 DocumentReference (org.xwiki.model.reference.DocumentReference)51 Test (org.junit.Test)40 XWikiContext (com.xpn.xwiki.XWikiContext)35 XWikiException (com.xpn.xwiki.XWikiException)25 ByteArrayInputStream (java.io.ByteArrayInputStream)20 Attachment (com.xpn.xwiki.api.Attachment)18 Date (java.util.Date)17 IOException (java.io.IOException)15 ArrayList (java.util.ArrayList)15 Document (com.xpn.xwiki.api.Document)14 XWiki (com.xpn.xwiki.XWiki)13 BaseObject (com.xpn.xwiki.objects.BaseObject)13 AttachmentReference (org.xwiki.model.reference.AttachmentReference)13 InputStream (java.io.InputStream)11 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)10 File (java.io.File)10 URL (java.net.URL)7 List (java.util.List)7