Search in sources :

Example 21 with Attachment

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

the class AttachmentUrlBuilder method buildUrl.

@Override
protected void buildUrl(final StringBuilder url, final Multimap<String, String> params) {
    super.buildUrl(url, params);
    appendPart(url, this.portalRequest.getContentPath().toString());
    appendPart(url, "_");
    appendPart(url, "attachment");
    if (this.params.isDownload()) {
        appendPart(url, "download");
    } else {
        appendPart(url, "inline");
    }
    final Content content = resolveContent();
    Attachment attachment = resolveAttachment(content);
    String hash = resolveHash(content, attachment);
    appendPart(url, content.getId().toString() + ":" + hash);
    appendPart(url, attachment.getName());
}
Also used : Content(com.enonic.xp.content.Content) Attachment(com.enonic.xp.attachment.Attachment)

Example 22 with Attachment

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

the class ContentFixtures method newMedia.

public static Media newMedia() {
    final Attachment attachment = Attachment.create().name("logo.png").mimeType("image/png").label("small").build();
    final PropertyTree data = newPropertyTree();
    data.addString("media", attachment.getName());
    final PropertyTree mediaData = newPropertyTree();
    mediaData.setLong(IMAGE_INFO_PIXEL_SIZE, 300L * 200L);
    mediaData.setLong(IMAGE_INFO_IMAGE_HEIGHT, 200L);
    mediaData.setLong(IMAGE_INFO_IMAGE_WIDTH, 300L);
    mediaData.setLong(MEDIA_INFO_BYTE_SIZE, 100000L);
    final ExtraData mediaExtraData = new ExtraData(MediaInfo.IMAGE_INFO_METADATA_NAME, mediaData);
    final Media.Builder builder = Media.create();
    builder.id(ContentId.from("123456"));
    builder.type(ContentTypeName.imageMedia());
    builder.name("mycontent");
    builder.displayName("My Content");
    builder.parentPath(ContentPath.from("/a/b"));
    builder.modifier(PrincipalKey.from("user:system:admin"));
    builder.modifiedTime(Instant.ofEpochSecond(0));
    builder.creator(PrincipalKey.from("user:system:admin"));
    builder.createdTime(Instant.ofEpochSecond(0));
    builder.attachments(Attachments.from(attachment));
    builder.data(data);
    builder.addExtraData(new ExtraData(XDataName.from("myapplication:myschema"), newTinyPropertyTree()));
    builder.addExtraData(mediaExtraData);
    builder.page(newPage());
    return builder.build();
}
Also used : PropertyTree(com.enonic.xp.data.PropertyTree) Media(com.enonic.xp.content.Media) Attachment(com.enonic.xp.attachment.Attachment) ExtraData(com.enonic.xp.content.ExtraData)

Example 23 with Attachment

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

the class PortalUrlServiceImpl_attachmentUrlTest method createContent.

private Content createContent() {
    final Attachment a1 = Attachment.create().label("thumb").name("a1.jpg").mimeType("image/jpg").build();
    final Attachment a2 = Attachment.create().label("source").name("a2.jpg").mimeType("image/jpg").build();
    final Attachments attachments = Attachments.from(a1, a2);
    final Content content = Content.create(ContentFixtures.newContent()).attachments(attachments).build();
    Mockito.when(this.contentService.getByPath(content.getPath())).thenReturn(content);
    Mockito.when(this.contentService.getById(content.getId())).thenReturn(content);
    Mockito.when(this.contentService.getBinaryKey(content.getId(), a1.getBinaryReference())).thenReturn("binaryHash1");
    Mockito.when(this.contentService.getBinaryKey(content.getId(), a2.getBinaryReference())).thenReturn("binaryHash2");
    return content;
}
Also used : Content(com.enonic.xp.content.Content) Attachment(com.enonic.xp.attachment.Attachment) Attachments(com.enonic.xp.attachment.Attachments)

Example 24 with Attachment

use of com.enonic.xp.attachment.Attachment 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 25 with Attachment

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

the class AttachmentHandlerWorker method execute.

@Override
public PortalResponse execute() throws Exception {
    final Content content = getContent(this.id);
    final Attachment attachment = resolveAttachment(content, this.name);
    final BinaryReference binaryReference = attachment.getBinaryReference();
    final ByteSource binary = resolveBinary(this.id, binaryReference);
    if (request.getMethod() == HttpMethod.OPTIONS) {
        // it will be handled by default OPTIONS handler in BaseWebHandler
        return PortalResponse.create().status(HttpStatus.METHOD_NOT_ALLOWED).build();
    }
    final MediaType contentType = MediaType.parse(attachment.getMimeType());
    final PortalResponse.Builder portalResponse = PortalResponse.create().contentType(contentType).body(binary);
    if (this.download) {
        portalResponse.header("Content-Disposition", contentDispositionAttachment(attachment.getName()));
    }
    if (this.name.endsWith(".svgz")) {
        portalResponse.header("Content-Encoding", "gzip");
    }
    if (!nullToEmpty(this.fingerprint).isBlank()) {
        final boolean isPublic = content.getPermissions().isAllowedFor(RoleKeys.EVERYONE, Permission.READ) && ContentConstants.BRANCH_MASTER.equals(request.getBranch());
        final String cacheControlHeaderConfig = isPublic ? publicCacheControlHeaderConfig : privateCacheControlHeaderConfig;
        if (!nullToEmpty(cacheControlHeaderConfig).isBlank() && this.fingerprint.equals(resolveHash(this.id, binaryReference))) {
            portalResponse.header(HttpHeaders.CACHE_CONTROL, cacheControlHeaderConfig);
        }
    }
    new RangeRequestHelper().handleRangeRequest(request, portalResponse, binary, contentType);
    return portalResponse.build();
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) Content(com.enonic.xp.content.Content) ByteSource(com.google.common.io.ByteSource) MediaType(com.google.common.net.MediaType) ServletRequestUrlHelper.contentDispositionAttachment(com.enonic.xp.web.servlet.ServletRequestUrlHelper.contentDispositionAttachment) Attachment(com.enonic.xp.attachment.Attachment) BinaryReference(com.enonic.xp.util.BinaryReference)

Aggregations

Attachment (com.enonic.xp.attachment.Attachment)29 Content (com.enonic.xp.content.Content)14 BinaryReference (com.enonic.xp.util.BinaryReference)12 ByteSource (com.google.common.io.ByteSource)11 ContentId (com.enonic.xp.content.ContentId)10 Attachments (com.enonic.xp.attachment.Attachments)9 CreateAttachment (com.enonic.xp.attachment.CreateAttachment)6 Media (com.enonic.xp.content.Media)6 ReadImageParams (com.enonic.xp.image.ReadImageParams)6 Test (org.junit.jupiter.api.Test)5 PropertyTree (com.enonic.xp.data.PropertyTree)4 CreateAttachments (com.enonic.xp.attachment.CreateAttachments)3 ContentPath (com.enonic.xp.content.ContentPath)2 ContentService (com.enonic.xp.content.ContentService)2 UpdateMediaParams (com.enonic.xp.content.UpdateMediaParams)2 Thumbnail (com.enonic.xp.icon.Thumbnail)2 PortalRequest (com.enonic.xp.portal.PortalRequest)2 PortalResponse (com.enonic.xp.portal.PortalResponse)2 ProcessHtmlParams (com.enonic.xp.portal.url.ProcessHtmlParams)2 ContentTypeName (com.enonic.xp.schema.content.ContentTypeName)2