Search in sources :

Example 1 with AttachmentVersioningStore

use of com.xpn.xwiki.store.AttachmentVersioningStore in project xwiki-platform by xwiki.

the class XWikiAttachmentTest method getContentInputStreamFromArchive.

@Test
public void getContentInputStreamFromArchive() throws Exception {
    XWikiDocument document = mock(XWikiDocument.class);
    when(document.getDocumentReference()).thenReturn(new DocumentReference("wiki", "Space", "Page"));
    when(this.oldcore.getXWikiContext().getWiki().getDocument(document.getDocumentReference(), this.oldcore.getXWikiContext())).thenReturn(document);
    XWikiAttachment attachment = new XWikiAttachment(document, "file.txt");
    attachment.setVersion("3.5");
    XWikiAttachment newAttachment = new XWikiAttachment(document, attachment.getFilename());
    newAttachment.setVersion("5.1");
    when(document.getAttachment(attachment.getFilename())).thenReturn(newAttachment);
    XWikiAttachmentContent content = mock(XWikiAttachmentContent.class);
    when(content.getContentInputStream()).thenReturn(mock(InputStream.class));
    XWikiAttachment archivedAttachment = new XWikiAttachment(document, attachment.getFilename());
    archivedAttachment.setAttachment_content(content);
    XWikiAttachmentArchive archive = mock(XWikiAttachmentArchive.class);
    when(archive.getRevision(attachment, attachment.getVersion(), this.oldcore.getXWikiContext())).thenReturn(archivedAttachment);
    AttachmentVersioningStore store = mock(AttachmentVersioningStore.class);
    when(this.oldcore.getXWikiContext().getWiki().getDefaultAttachmentArchiveStore()).thenReturn(store);
    when(store.loadArchive(attachment, this.oldcore.getXWikiContext(), true)).thenReturn(archive);
    assertSame(content.getContentInputStream(), attachment.getContentInputStream(this.oldcore.getXWikiContext()));
}
Also used : AttachmentVersioningStore(com.xpn.xwiki.store.AttachmentVersioningStore) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 2 with AttachmentVersioningStore

use of com.xpn.xwiki.store.AttachmentVersioningStore in project xwiki-platform by xwiki.

the class XWikiAttachment method loadArchive.

public XWikiAttachmentArchive loadArchive(XWikiContext xcontext) {
    if (this.attachment_archive == null) {
        WikiReference currentWiki = xcontext.getWikiReference();
        try {
            // Make sure we work on the attachment's wiki
            WikiReference attachmentWiki = getReference().getDocumentReference().getWikiReference();
            if (attachmentWiki != null) {
                xcontext.setWikiReference(attachmentWiki);
            }
            try {
                AttachmentVersioningStore store = getAttachmentVersioningStore(xcontext);
                this.attachment_archive = store.loadArchive(this, xcontext, true);
            } catch (Exception e) {
                LOGGER.warn("Failed to load archive for attachment [{}@{}]. " + "This attachment is broken, please consider re-uploading it", this.doc != null ? this.doc.getDocumentReference() : "<unknown>", getFilename(), e);
            }
        } finally {
            if (currentWiki != null) {
                xcontext.setWikiReference(currentWiki);
            }
        }
    }
    return this.attachment_archive;
}
Also used : AttachmentVersioningStore(com.xpn.xwiki.store.AttachmentVersioningStore) WikiReference(org.xwiki.model.reference.WikiReference) XWikiException(com.xpn.xwiki.XWikiException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) IOException(java.io.IOException)

Aggregations

AttachmentVersioningStore (com.xpn.xwiki.store.AttachmentVersioningStore)2 XWikiException (com.xpn.xwiki.XWikiException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ReaderInputStream (org.apache.commons.io.input.ReaderInputStream)1 Test (org.junit.Test)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 WikiReference (org.xwiki.model.reference.WikiReference)1