Search in sources :

Example 31 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)31 Content (com.enonic.xp.content.Content)14 BinaryReference (com.enonic.xp.util.BinaryReference)13 ByteSource (com.google.common.io.ByteSource)12 Attachments (com.enonic.xp.attachment.Attachments)10 ContentId (com.enonic.xp.content.ContentId)10 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 PortalResponse (com.enonic.xp.portal.PortalResponse)4 ServletRequestUrlHelper.contentDispositionAttachment (com.enonic.xp.web.servlet.ServletRequestUrlHelper.contentDispositionAttachment)4 MediaType (com.google.common.net.MediaType)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