Search in sources :

Example 36 with XWikiAttachment

use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.

the class GroupsClassPropertyValuesProvider method getIcon.

@Override
protected String getIcon(DocumentReference groupReference) {
    XWikiContext xcontext = this.xcontextProvider.get();
    try {
        XWikiDocument groupProfileDocument = xcontext.getWiki().getDocument(groupReference, xcontext);
        XWikiAttachment avatarAttachment = getFirstImageAttachment(groupProfileDocument, xcontext);
        if (avatarAttachment != null) {
            return xcontext.getWiki().getURL(avatarAttachment.getReference(), "download", "width=30&height=30&keepAspectRatio=true", null, xcontext);
        }
    } catch (XWikiException e) {
        this.logger.warn("Failed to read the avatar of group [{}]. Root cause is [{}]. Using the default avatar instead.", groupReference.getName(), ExceptionUtils.getRootCauseMessage(e));
    }
    return xcontext.getWiki().getSkinFile("icons/xwiki/noavatargroup.png", true, xcontext);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) XWikiException(com.xpn.xwiki.XWikiException)

Example 37 with XWikiAttachment

use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.

the class UsersClassPropertyValuesProvider method getIcon.

@Override
protected String getIcon(DocumentReference userReference) {
    XWikiContext xcontext = this.xcontextProvider.get();
    try {
        XWikiDocument userProfileDocument = xcontext.getWiki().getDocument(userReference, xcontext);
        String avatar = userProfileDocument.getStringValue("avatar");
        XWikiAttachment avatarAttachment = userProfileDocument.getAttachment(avatar);
        if (avatarAttachment != null && avatarAttachment.isImage(xcontext)) {
            return xcontext.getWiki().getURL(avatarAttachment.getReference(), "download", "width=30&height=30&keepAspectRatio=true", null, xcontext);
        }
    } catch (XWikiException e) {
        this.logger.warn("Failed to read the avatar of user [{}]. Root cause is [{}]. Using the default avatar instead.", userReference.getName(), ExceptionUtils.getRootCauseMessage(e));
    }
    return xcontext.getWiki().getSkinFile("icons/xwiki/noavatar.png", true, xcontext);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) XWikiException(com.xpn.xwiki.XWikiException)

Example 38 with XWikiAttachment

use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.

the class ZipExplorerTest method testDownloadAttachmentWhenURLIsNotZipFile.

public void testDownloadAttachmentWhenURLIsNotZipFile() throws Exception {
    Mock mockDocument = mock(XWikiDocument.class);
    mockDocument.stubs().method("getDocumentReference").will(returnValue(new DocumentReference("wiki", "Main", "Document")));
    XWikiAttachment originalAttachment = createAttachment("somefile.whatever", null, (XWikiDocument) mockDocument.proxy());
    XWikiContext context = createXWikiContext("http://server/xwiki/bin/download/Main/Document/somefile.whatever");
    XWikiAttachment newAttachment = this.plugin.downloadAttachment(originalAttachment, context);
    Assert.assertSame(originalAttachment, newAttachment);
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Mock(org.jmock.Mock) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 39 with XWikiAttachment

use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.

the class ZipExplorerTest method testDownloadAttachmentWhenURLIsZipButNotPointingInsideZip.

public void testDownloadAttachmentWhenURLIsZipButNotPointingInsideZip() throws Exception {
    Mock mockDocument = mock(XWikiDocument.class);
    mockDocument.stubs().method("getDocumentReference").will(returnValue(new DocumentReference("wiki", "Main", "Document")));
    XWikiAttachment originalAttachment = createAttachment("zipfile.zip", null, (XWikiDocument) mockDocument.proxy());
    XWikiContext context = createXWikiContext("http://server/xwiki/bin/download/Main/Document/zipfile.zip");
    XWikiAttachment newAttachment = this.plugin.downloadAttachment(originalAttachment, context);
    Assert.assertSame(originalAttachment, newAttachment);
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Mock(org.jmock.Mock) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 40 with XWikiAttachment

use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.

the class ZipExplorerTest method testDownloadAttachmentWithInvalidZipURL.

public void testDownloadAttachmentWithInvalidZipURL() throws Exception {
    Mock mockDocument = mock(XWikiDocument.class);
    mockDocument.stubs().method("getDocumentReference").will(returnValue(new DocumentReference("wiki", "Main", "Document")));
    XWikiAttachment originalAttachment = createAttachment("someFile.txt", "Some text".getBytes(), (XWikiDocument) mockDocument.proxy());
    XWikiContext context = createXWikiContext("http://server/xwiki/bin/download/Main/Document/someFile.txt");
    XWikiAttachment newAttachment = this.plugin.downloadAttachment(originalAttachment, context);
    Assert.assertSame(originalAttachment, newAttachment);
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Mock(org.jmock.Mock) DocumentReference(org.xwiki.model.reference.DocumentReference)

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