Search in sources :

Example 6 with Attachments

use of com.enonic.xp.attachment.Attachments in project xp by enonic.

the class ContentDataSerializer method extractAttachments.

private void extractAttachments(final PropertySet contentAsSet, final Content.Builder<?> builder) {
    final Attachments attachments = dataToAttachments(contentAsSet.getSets(ATTACHMENT));
    builder.attachments(attachments);
    final Attachment thumbnailAttachment = attachments.byName(AttachmentNames.THUMBNAIL);
    if (thumbnailAttachment != null) {
        final BinaryReference thumbnailBinaryRef = thumbnailAttachment.getBinaryReference();
        final Thumbnail thumbnail = Thumbnail.from(thumbnailBinaryRef, thumbnailAttachment.getMimeType(), thumbnailAttachment.getSize());
        builder.thumbnail(thumbnail);
    }
}
Also used : CreateAttachment(com.enonic.xp.attachment.CreateAttachment) Attachment(com.enonic.xp.attachment.Attachment) BinaryReference(com.enonic.xp.util.BinaryReference) Thumbnail(com.enonic.xp.icon.Thumbnail) CreateAttachments(com.enonic.xp.attachment.CreateAttachments) Attachments(com.enonic.xp.attachment.Attachments)

Example 7 with Attachments

use of com.enonic.xp.attachment.Attachments in project xp by enonic.

the class AbstractAttachmentHandlerWorker method resolveAttachment.

protected Attachment resolveAttachment(final Content content, final String name) {
    final Attachments attachments = content.getAttachments();
    final Attachment attachment = attachments.byName(name);
    if (attachment != null) {
        return attachment;
    }
    throw WebException.notFound(String.format("Attachment [%s] not found for [%s]", name, content.getPath()));
}
Also used : ServletRequestUrlHelper.contentDispositionAttachment(com.enonic.xp.web.servlet.ServletRequestUrlHelper.contentDispositionAttachment) Attachment(com.enonic.xp.attachment.Attachment) Attachments(com.enonic.xp.attachment.Attachments)

Example 8 with Attachments

use of com.enonic.xp.attachment.Attachments in project xp by enonic.

the class PortalUrlServiceImpl_processHtmlTest method process_multiple_links.

@Test
public void process_multiple_links() {
    // Creates a content with attachments
    final Attachment thumb = Attachment.create().label("thumb").name("a1.jpg").mimeType("image/jpg").build();
    final Attachment source = Attachment.create().label("source").name("a2.jpg").mimeType("image/jpg").build();
    final Attachments attachments = Attachments.from(thumb, source);
    final Content content = Content.create(ContentFixtures.newContent()).attachments(attachments).build();
    Mockito.when(this.contentService.getById(content.getId())).thenReturn(content);
    Mockito.when(this.contentService.getBinaryKey(content.getId(), source.getBinaryReference())).thenReturn("binaryHash2");
    // Process an html text containing multiple links, on multiple lines, to this content as a media and as a content
    final ProcessHtmlParams params = new ProcessHtmlParams().portalRequest(this.portalRequest).value("<p>A content link:&nbsp;<a href=\"content://" + content.getId() + "\">FirstLink</a></p>\n" + "<p>A second content link:&nbsp;<a href=\"content://" + content.getId() + "\">SecondLink</a>" + "&nbsp;and a download link:&nbsp;<a href=\"media://download/" + content.getId() + "\">Download</a></p>\n" + "<p>An external link:&nbsp;<a href=\"http://www.enonic.com\">An external  link</a></p>\n" + "<p>&nbsp;</p>\n" + "<a href=\"media://inline/" + content.getId() + "\">Inline</a>");
    // Checks the returned value
    final String processedHtml = this.service.processHtml(params);
    assertEquals("<p>A content link:&nbsp;<a href=\"/site/default/draft" + content.getPath() + "\">FirstLink</a></p>\n" + "<p>A second content link:&nbsp;<a href=\"/site/default/draft" + content.getPath() + "\">SecondLink</a>" + "&nbsp;and a download link:&nbsp;<a href=\"/site/default/draft/context/path/_/attachment/download/" + content.getId() + ":binaryHash2/" + source.getName() + "\">Download</a></p>\n" + "<p>An external link:&nbsp;<a href=\"http://www.enonic.com\">An external  link</a></p>\n" + "<p>&nbsp;</p>\n" + "<a href=\"/site/default/draft/context/path/_/attachment/inline/" + content.getId() + ":binaryHash2/" + source.getName() + "\">Inline</a>", processedHtml);
}
Also used : Content(com.enonic.xp.content.Content) ProcessHtmlParams(com.enonic.xp.portal.url.ProcessHtmlParams) Attachment(com.enonic.xp.attachment.Attachment) Attachments(com.enonic.xp.attachment.Attachments) Test(org.junit.jupiter.api.Test)

Example 9 with Attachments

use of com.enonic.xp.attachment.Attachments in project xp by enonic.

the class PortalUrlServiceImpl_processHtmlTest method process_single_media.

@Test
public void process_single_media() {
    // Creates a content with attachments
    final Attachment thumb = Attachment.create().label("thumb").name("a1.jpg").mimeType("image/jpg").build();
    final Attachment source = Attachment.create().label("source").name("a2.jpg").mimeType("image/jpg").build();
    final Attachments attachments = Attachments.from(thumb, source);
    final Content content = Content.create(ContentFixtures.newContent()).attachments(attachments).build();
    Mockito.when(this.contentService.getById(content.getId())).thenReturn(content);
    Mockito.when(this.contentService.getBinaryKey(content.getId(), source.getBinaryReference())).thenReturn("binaryHash2");
    // Process an html text containing an inline link to this content
    ProcessHtmlParams params = new ProcessHtmlParams().portalRequest(this.portalRequest).value("<a href=\"media://inline/" + content.getId() + "\">Media</a>");
    // Checks that the URL of the source attachment of the content is returned
    String processedHtml = this.service.processHtml(params);
    assertEquals("<a href=\"/site/default/draft/context/path/_/attachment/inline/" + content.getId() + ":binaryHash2/" + source.getName() + "\">Media</a>", processedHtml);
    // Process an html text containing a download link to this content
    params = new ProcessHtmlParams().portalRequest(this.portalRequest).value("<a href=\"media://download/" + content.getId() + "\">Media</a>");
    // Checks that the URL of the source attachment of the content is returned
    processedHtml = this.service.processHtml(params);
    assertEquals("<a href=\"/site/default/draft/context/path/_/attachment/download/" + content.getId() + ":binaryHash2/" + source.getName() + "\">Media</a>", processedHtml);
    // Process an html text containing an inline link to this content in a img tag
    params = new ProcessHtmlParams().portalRequest(this.portalRequest).value("<a href=\"/some/page\"><img src=\"media://inline/" + content.getId() + "\">Media</a>");
    // Checks that the URL of the source attachment of the content is returned
    processedHtml = this.service.processHtml(params);
    assertEquals("<a href=\"/some/page\"><img src=\"/site/default/draft/context/path/_/attachment/inline/" + content.getId() + ":binaryHash2/" + source.getName() + "\">Media</a>", processedHtml);
}
Also used : Content(com.enonic.xp.content.Content) ProcessHtmlParams(com.enonic.xp.portal.url.ProcessHtmlParams) Attachment(com.enonic.xp.attachment.Attachment) Attachments(com.enonic.xp.attachment.Attachments) Test(org.junit.jupiter.api.Test)

Example 10 with Attachments

use of com.enonic.xp.attachment.Attachments in project xp by enonic.

the class ContentServiceImplTest_update method update_content_image.

@Test
public void update_content_image() throws Exception {
    final ByteSource image = loadImage("cat-small.jpg");
    final CreateContentParams createContentParams = CreateContentParams.create().contentData(new PropertyTree()).displayName("This is my content").parent(ContentPath.ROOT).type(ContentTypeName.imageMedia()).createAttachments(createAttachment("cat", "image/jpg", image)).build();
    final Content content = this.contentService.create(createContentParams);
    final UpdateContentParams updateContentParams = new UpdateContentParams();
    updateContentParams.contentId(content.getId()).editor(edit -> {
        edit.displayName = "new display name";
    }).clearAttachments(true).createAttachments(createAttachment("darth", "image/jpg", loadImage("darth-small.jpg")));
    this.contentService.update(updateContentParams);
    final Content storedContent = this.contentService.getById(content.getId());
    final Attachments attachments = storedContent.getAttachments();
    assertEquals(1, attachments.getSize());
}
Also used : UpdateContentParams(com.enonic.xp.content.UpdateContentParams) CreateContentParams(com.enonic.xp.content.CreateContentParams) Content(com.enonic.xp.content.Content) PropertyTree(com.enonic.xp.data.PropertyTree) ByteSource(com.google.common.io.ByteSource) CreateAttachments(com.enonic.xp.attachment.CreateAttachments) Attachments(com.enonic.xp.attachment.Attachments) Test(org.junit.jupiter.api.Test)

Aggregations

Attachments (com.enonic.xp.attachment.Attachments)14 Content (com.enonic.xp.content.Content)10 Attachment (com.enonic.xp.attachment.Attachment)8 Test (org.junit.jupiter.api.Test)8 CreateMediaParams (com.enonic.xp.content.CreateMediaParams)4 ContentType (com.enonic.xp.schema.content.ContentType)4 CreateAttachments (com.enonic.xp.attachment.CreateAttachments)3 PropertyTree (com.enonic.xp.data.PropertyTree)3 CreateContentParams (com.enonic.xp.content.CreateContentParams)2 ProcessHtmlParams (com.enonic.xp.portal.url.ProcessHtmlParams)2 ServletRequestUrlHelper.contentDispositionAttachment (com.enonic.xp.web.servlet.ServletRequestUrlHelper.contentDispositionAttachment)2 ByteSource (com.google.common.io.ByteSource)2 CreateAttachment (com.enonic.xp.attachment.CreateAttachment)1 UpdateContentParams (com.enonic.xp.content.UpdateContentParams)1 UpdateMediaParams (com.enonic.xp.content.UpdateMediaParams)1 ContentConfig (com.enonic.xp.core.impl.content.ContentConfig)1 PropertySet (com.enonic.xp.data.PropertySet)1 Thumbnail (com.enonic.xp.icon.Thumbnail)1 BinaryReference (com.enonic.xp.util.BinaryReference)1