Search in sources :

Example 16 with Attachment

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

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

the class ContentDataSerializer method mapValidationError.

private ValidationError mapValidationError(final PropertySet ve) {
    final Object[] args = Optional.ofNullable(ve.getString("args")).map(argsJson -> {
        try {
            return OBJECT_MAPPER.readValue(argsJson, Object[].class);
        } catch (JsonProcessingException e) {
            throw new UncheckedIOException(e);
        }
    }).orElse(null);
    final ValidationErrorCode errorCode = ValidationErrorCode.parse(ve.getString("errorCode"));
    if (ve.hasProperty("propertyPath")) {
        return ValidationError.dataError(errorCode, PropertyPath.from(ve.getString("propertyPath"))).message(ve.getString("message"), true).i18n(ve.getString("i18n")).args(args).build();
    } else if (ve.hasProperty("attachment")) {
        return ValidationError.attachmentError(errorCode, BinaryReference.from(ve.getString("attachment"))).message(ve.getString("message"), true).i18n(ve.getString("i18n")).args(args).build();
    } else {
        return ValidationError.generalError(errorCode).message(ve.getString("message"), true).i18n(ve.getString("i18n")).args(args).build();
    }
}
Also used : OWNER(com.enonic.xp.content.ContentPropertyNames.OWNER) WorkflowInfo(com.enonic.xp.content.WorkflowInfo) CREATOR(com.enonic.xp.content.ContentPropertyNames.CREATOR) AttachmentSerializer(com.enonic.xp.attachment.AttachmentSerializer) Page(com.enonic.xp.page.Page) ValidationErrors(com.enonic.xp.content.ValidationErrors) ContentName(com.enonic.xp.content.ContentName) ContentId(com.enonic.xp.content.ContentId) INHERIT(com.enonic.xp.content.ContentPropertyNames.INHERIT) DataValidationError(com.enonic.xp.content.DataValidationError) Locale(java.util.Locale) Map(java.util.Map) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) ValidationError(com.enonic.xp.content.ValidationError) WORKFLOW_INFO_CHECKS(com.enonic.xp.content.ContentPropertyNames.WORKFLOW_INFO_CHECKS) Thumbnail(com.enonic.xp.icon.Thumbnail) BinaryReference(com.enonic.xp.util.BinaryReference) DISPLAY_NAME(com.enonic.xp.content.ContentPropertyNames.DISPLAY_NAME) ValidationErrorCode(com.enonic.xp.content.ValidationErrorCode) PROCESSED_REFERENCES(com.enonic.xp.content.ContentPropertyNames.PROCESSED_REFERENCES) PUBLISH_TO(com.enonic.xp.content.ContentPropertyNames.PUBLISH_TO) WORKFLOW_INFO_STATE(com.enonic.xp.content.ContentPropertyNames.WORKFLOW_INFO_STATE) Set(java.util.Set) LayoutDescriptorService(com.enonic.xp.region.LayoutDescriptorService) Collectors(java.util.stream.Collectors) ExtraDatas(com.enonic.xp.content.ExtraDatas) NodeId(com.enonic.xp.node.NodeId) UncheckedIOException(java.io.UncheckedIOException) TYPE(com.enonic.xp.content.ContentPropertyNames.TYPE) AttachmentNames(com.enonic.xp.attachment.AttachmentNames) VALID(com.enonic.xp.content.ContentPropertyNames.VALID) Optional(java.util.Optional) MODIFIED_TIME(com.enonic.xp.content.ContentPropertyNames.MODIFIED_TIME) UpdateContentTranslatorParams(com.enonic.xp.content.UpdateContentTranslatorParams) ARCHIVED_TIME(com.enonic.xp.content.ContentPropertyNames.ARCHIVED_TIME) CREATED_TIME(com.enonic.xp.content.ContentPropertyNames.CREATED_TIME) BinaryReferences(com.enonic.xp.util.BinaryReferences) Strings.nullToEmpty(com.google.common.base.Strings.nullToEmpty) PUBLISH_FIRST(com.enonic.xp.content.ContentPropertyNames.PUBLISH_FIRST) ORIGINAL_PARENT_PATH(com.enonic.xp.content.ContentPropertyNames.ORIGINAL_PARENT_PATH) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) Attachment(com.enonic.xp.attachment.Attachment) ContentTypeName(com.enonic.xp.schema.content.ContentTypeName) ORIGIN_PROJECT(com.enonic.xp.content.ContentPropertyNames.ORIGIN_PROJECT) ORIGINAL_NAME(com.enonic.xp.content.ContentPropertyNames.ORIGINAL_NAME) LinkedHashMap(java.util.LinkedHashMap) PUBLISH_FROM(com.enonic.xp.content.ContentPropertyNames.PUBLISH_FROM) VALIDATION_ERRORS(com.enonic.xp.content.ContentPropertyNames.VALIDATION_ERRORS) PageDescriptorService(com.enonic.xp.page.PageDescriptorService) PUBLISH_INFO(com.enonic.xp.content.ContentPropertyNames.PUBLISH_INFO) CreateAttachments(com.enonic.xp.attachment.CreateAttachments) Exceptions(com.enonic.xp.util.Exceptions) ATTACHMENT(com.enonic.xp.content.ContentPropertyNames.ATTACHMENT) ProjectName(com.enonic.xp.project.ProjectName) DATA(com.enonic.xp.content.ContentPropertyNames.DATA) StreamSupport(java.util.stream.StreamSupport) PropertyPath(com.enonic.xp.data.PropertyPath) LANGUAGE(com.enonic.xp.content.ContentPropertyNames.LANGUAGE) WORKFLOW_INFO(com.enonic.xp.content.ContentPropertyNames.WORKFLOW_INFO) PropertyTree(com.enonic.xp.data.PropertyTree) ObjectMapperHelper(com.enonic.xp.json.ObjectMapperHelper) PartDescriptorService(com.enonic.xp.region.PartDescriptorService) ContentPropertyNames(com.enonic.xp.content.ContentPropertyNames) ContentPath(com.enonic.xp.content.ContentPath) PropertySet(com.enonic.xp.data.PropertySet) MODIFIER(com.enonic.xp.content.ContentPropertyNames.MODIFIER) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ContentInheritType(com.enonic.xp.content.ContentInheritType) Content(com.enonic.xp.content.Content) ARCHIVED_BY(com.enonic.xp.content.ContentPropertyNames.ARCHIVED_BY) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ContentPublishInfo(com.enonic.xp.content.ContentPublishInfo) AttachmentValidationError(com.enonic.xp.content.AttachmentValidationError) COMPONENTS(com.enonic.xp.core.impl.content.serializer.ComponentDataSerializer.COMPONENTS) PrincipalKey(com.enonic.xp.security.PrincipalKey) Reference(com.enonic.xp.util.Reference) EXTRA_DATA(com.enonic.xp.content.ContentPropertyNames.EXTRA_DATA) ContentIds(com.enonic.xp.content.ContentIds) Attachments(com.enonic.xp.attachment.Attachments) Preconditions(com.google.common.base.Preconditions) CreateContentTranslatorParams(com.enonic.xp.content.CreateContentTranslatorParams) UncheckedIOException(java.io.UncheckedIOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ValidationErrorCode(com.enonic.xp.content.ValidationErrorCode)

Example 18 with Attachment

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

the class ContentDataSerializer method mergeExistingAndUpdatedAttachments.

private Attachments mergeExistingAndUpdatedAttachments(final Attachments existingAttachments, final UpdateContentTranslatorParams params) {
    CreateAttachments createAttachments = params.getCreateAttachments();
    BinaryReferences removeAttachments = params.getRemoveAttachments();
    if (createAttachments == null && removeAttachments == null && !params.isClearAttachments()) {
        return existingAttachments;
    }
    createAttachments = createAttachments == null ? CreateAttachments.empty() : createAttachments;
    removeAttachments = removeAttachments == null ? BinaryReferences.empty() : removeAttachments;
    final Map<BinaryReference, Attachment> attachments = new LinkedHashMap<>();
    if (!params.isClearAttachments()) {
        existingAttachments.stream().forEach((a) -> attachments.put(a.getBinaryReference(), a));
    }
    removeAttachments.stream().forEach(attachments::remove);
    // added attachments with same BinaryReference will replace existing ones
    for (final CreateAttachment createAttachment : createAttachments) {
        final Attachment attachment = Attachment.create().name(createAttachment.getName()).label(createAttachment.getLabel()).mimeType(createAttachment.getMimeType()).size(attachmentSize(createAttachment)).textContent(createAttachment.getTextContent()).build();
        attachments.put(attachment.getBinaryReference(), attachment);
    }
    return Attachments.from(attachments.values());
}
Also used : CreateAttachments(com.enonic.xp.attachment.CreateAttachments) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) BinaryReferences(com.enonic.xp.util.BinaryReferences) BinaryReference(com.enonic.xp.util.BinaryReference) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) Attachment(com.enonic.xp.attachment.Attachment) LinkedHashMap(java.util.LinkedHashMap)

Example 19 with Attachment

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

the class AbstractAttachmentHandlerWorker method execute.

@Override
public PortalResponse execute() throws Exception {
    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 T content = cast(getContent(this.id));
    final Attachment attachment = resolveAttachment(content, this.name);
    final BinaryReference binaryReference = attachment.getBinaryReference();
    final ByteSource binary = getBinary(this.id, binaryReference);
    final MediaType attachmentMimeType = MediaType.parse(attachment.getMimeType());
    final boolean isSvgz = "svgz".equals(attachment.getExtension());
    final boolean isGif = attachmentMimeType.is(MediaType.GIF);
    final PortalResponse.Builder portalResponse = PortalResponse.create();
    final MediaType contentType;
    if (isSvgz) {
        contentType = SVG_MEDIA_TYPE;
        portalResponse.header("Content-Encoding", "gzip");
    } else if (isGif) {
        contentType = MediaType.GIF;
    } else if (shouldConvert(content, this.name)) {
        contentType = MediaTypes.instance().fromFile(this.name);
    } else {
        contentType = attachmentMimeType;
    }
    if (contentType.is(SVG_MEDIA_TYPE)) {
        if (!nullToEmpty(contentSecurityPolicySvg).isBlank()) {
            portalResponse.header("Content-Security-Policy", contentSecurityPolicySvg);
        }
    } else {
        if (!nullToEmpty(contentSecurityPolicy).isBlank()) {
            portalResponse.header("Content-Security-Policy", contentSecurityPolicy);
        }
    }
    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(content, binaryReference))) {
            portalResponse.header(HttpHeaders.CACHE_CONTROL, cacheControlHeaderConfig);
        }
    }
    if (download) {
        portalResponse.header("Content-Disposition", contentDispositionAttachment(attachment.getName()));
    }
    final ByteSource body;
    if (isGif || isSvgz || attachmentMimeType.is(SVG_MEDIA_TYPE)) {
        body = binary;
    } else {
        body = transform(content, binaryReference, binary, contentType);
    }
    addTrace(content);
    writeResponseContent(portalResponse, contentType, body);
    return portalResponse.build();
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) 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)

Example 20 with Attachment

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

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