Search in sources :

Example 66 with XWikiAttachment

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

the class NewsletterAttachmentServiceTest method testEmbedImagesInContent_inner.

@Test
public void testEmbedImagesInContent_inner() 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.jpg"))).andReturn(att).anyTimes();
    expect(attService.getApiAttachment(same(att))).andReturn(new Attachment(new Document(doc, getContext()), att, getContext()));
    expect(xwiki.getDocument(eq(docRef), same(getContext()))).andReturn(doc).once();
    String imgTag = "<img class=\"abc\" src=\"/download/Test/Img/file.jpg?bla=123\" />";
    String content = "Test text with " + imgTag + " image included";
    Set<String> tags = new HashSet<>();
    tags.add(imgTag);
    replayDefault();
    String result = service.embedImagesInContent(content, tags);
    verifyDefault();
    assertTrue(result, result.contains("src=\"cid:file.jpg\""));
    assertFalse(result, result.contains("/download/"));
}
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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 67 with XWikiAttachment

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

the class NewsletterAttachmentServiceTest method testGetImageURL_notEmbedded.

@Test
public void testGetImageURL_notEmbedded() throws Exception {
    String expectedResult = "/download/Test/Img/file.jpg";
    XWikiDocument doc = createMockAndAddToDefault(XWikiDocument.class);
    XWikiAttachment att = new XWikiAttachment();
    expect(attService.getAttachmentNameEqual(same(doc), eq("file.jpg"))).andReturn(att).anyTimes();
    expect(doc.getAttachmentURL(eq("file.jpg"), eq("download"), same(getContext()))).andReturn(expectedResult).once();
    expect(xwiki.getDocument(eq("Test.Img"), same(getContext()))).andReturn(doc).once();
    replayDefault();
    assertTrue(service.getImageURL("Test.Img;file.jpg", false).startsWith(expectedResult));
    verifyDefault();
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Test(org.junit.Test)

Example 68 with XWikiAttachment

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

the class NewsletterAttachmentServiceTest method testEmbedImagesInContent.

@Test
public void testEmbedImagesInContent() 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.jpg"))).andReturn(att).anyTimes();
    expect(attService.getApiAttachment(same(att))).andReturn(new Attachment(new Document(doc, getContext()), att, getContext()));
    expect(xwiki.getDocument(eq(docRef), same(getContext()))).andReturn(doc).once();
    String imgTag = "<img class=\"abc\" src=\"/download/Test/Img/file.jpg?bla=123\" />";
    String content = "Test text with " + imgTag + " image included";
    replayDefault();
    String result = service.embedImagesInContent(content);
    verifyDefault();
    assertTrue(result, result.contains("src=\"cid:file.jpg\""));
    assertFalse(result, result.contains("/download/"));
}
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 69 with XWikiAttachment

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

the class DocumentInstanceOutputFilterStream method setAuthors.

private void setAuthors(XWikiDocument document, XWikiDocument inputDocument) {
    // Document author
    document.setAuthorReference(inputDocument.getAuthorReference());
    document.setContentAuthorReference(inputDocument.getContentAuthorReference());
    if (document.isNew()) {
        document.setCreatorReference(inputDocument.getCreatorReference());
    }
    // Attachments author
    for (XWikiAttachment currentAttachment : document.getAttachmentList()) {
        currentAttachment.setAuthorReference(inputDocument.getAttachment(currentAttachment.getFilename()).getAuthorReference());
    }
}
Also used : XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment)

Example 70 with XWikiAttachment

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

the class XWikiAttachmentOutputFilterStream method onWikiAttachment.

@Override
public void onWikiAttachment(String name, InputStream content, Long size, FilterEventParameters parameters) throws FilterException {
    if (this.entity == null) {
        this.entity = new XWikiAttachment();
    }
    this.entity.setFilename(name);
    if (content != null) {
        try {
            this.entity.setContent(content);
        } catch (IOException e) {
            throw new FilterException("Failed to set attachment content", e);
        }
    }
    // Author
    this.entity.setAuthorReference(getUserReference(WikiAttachmentFilter.PARAMETER_REVISION_AUTHOR, parameters, null));
    if (this.properties == null || this.properties.isVersionPreserved()) {
        setVersion(parameters);
        this.entity.setComment(getString(WikiAttachmentFilter.PARAMETER_REVISION_COMMENT, parameters, ""));
        this.entity.setDate(getDate(WikiAttachmentFilter.PARAMETER_REVISION_DATE, parameters, new Date()));
        this.entity.setMimeType(getString(WikiAttachmentFilter.PARAMETER_MIMETYPE, parameters, null));
        String revisions = getString(XWikiWikiAttachmentFilter.PARAMETER_JRCSREVISIONS, parameters, null);
        if (revisions != null) {
            try {
                this.entity.setArchive(revisions);
            } catch (XWikiException e) {
                throw new FilterException("Failed to set attachment archive", e);
            }
        }
        this.entity.setMetaDataDirty(false);
    }
}
Also used : FilterException(org.xwiki.filter.FilterException) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) IOException(java.io.IOException) Date(java.util.Date) XWikiException(com.xpn.xwiki.XWikiException)

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