Search in sources :

Example 6 with CreateAttachment

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

the class UpdatedEventSyncCommand method doSyncThumbnail.

private void doSyncThumbnail(final ContentToSync content, final UpdateContentParams updateParams) {
    if (!Objects.equals(content.getSourceContent().getThumbnail(), content.getTargetContent().getThumbnail())) {
        final Thumbnail sourceThumbnail = content.getSourceContent().getThumbnail();
        if (sourceThumbnail != null) {
            final ByteSource sourceBinary = content.getSourceContext().callWith(() -> contentService.getBinary(content.getSourceContent().getId(), sourceThumbnail.getBinaryReference()));
            final CreateAttachment createThumbnail = CreateAttachment.create().name(AttachmentNames.THUMBNAIL).mimeType(sourceThumbnail.getMimeType()).byteSource(sourceBinary).build();
            final CreateAttachments.Builder createAttachments = CreateAttachments.create().add(createThumbnail);
            if (updateParams.getCreateAttachments() != null) {
                createAttachments.add(updateParams.getCreateAttachments());
            }
            updateParams.createAttachments(createAttachments.build());
        } else {
            final Thumbnail targetThumbnail = content.getTargetContent().getThumbnail();
            updateParams.removeAttachments(BinaryReferences.from(targetThumbnail.getBinaryReference()));
        }
    }
}
Also used : CreateAttachments(com.enonic.xp.attachment.CreateAttachments) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) ByteSource(com.google.common.io.ByteSource) Thumbnail(com.enonic.xp.icon.Thumbnail)

Example 7 with CreateAttachment

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

the class UpdatedEventSyncCommand method doSyncMedia.

private void doSyncMedia(final ContentToSync content, final UpdateContentParams updateParams) {
    if (content.getSourceContent() instanceof Media) {
        final Media sourceMedia = (Media) content.getSourceContent();
        final Attachment mediaAttachment = sourceMedia.getMediaAttachment();
        final ByteSource sourceBinary = content.getSourceContext().callWith(() -> contentService.getBinary(sourceMedia.getId(), mediaAttachment.getBinaryReference()));
        final MediaInfo mediaInfo = content.getSourceContext().callWith(() -> mediaInfoService.parseMediaInfo(sourceBinary));
        final ContentTypeName type = ContentTypeFromMimeTypeResolver.resolve(mediaAttachment.getMimeType());
        final CreateAttachment createAttachment = CreateAttachment.create().name(mediaAttachment.getName()).mimeType(mediaAttachment.getMimeType()).label("source").byteSource(sourceBinary).text(type != null && type.isTextualMedia() ? mediaInfo.getTextContent() : null).build();
        updateParams.clearAttachments(true).createAttachments(CreateAttachments.from(createAttachment));
    }
}
Also used : MediaInfo(com.enonic.xp.media.MediaInfo) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) Media(com.enonic.xp.content.Media) ContentTypeName(com.enonic.xp.schema.content.ContentTypeName) ByteSource(com.google.common.io.ByteSource) Attachment(com.enonic.xp.attachment.Attachment) CreateAttachment(com.enonic.xp.attachment.CreateAttachment)

Example 8 with CreateAttachment

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

the class ImageContentProcessor method processCreate.

@Override
public ProcessCreateResult processCreate(final ProcessCreateParams params) {
    final CreateContentParams createContentParams = params.getCreateContentParams();
    final MediaInfo mediaInfo = params.getMediaInfo();
    final CreateAttachments originalAttachments = createContentParams.getCreateAttachments();
    Preconditions.checkArgument(originalAttachments.getSize() == 1, "Expected only one attachment");
    final CreateAttachment sourceAttachment = originalAttachments.first();
    final XDatas contentXDatas = getXDatas(createContentParams.getType());
    ExtraDatas extraDatas = null;
    if (mediaInfo != null) {
        extraDatas = extractMetadata(mediaInfo, contentXDatas, sourceAttachment);
    }
    final CreateAttachments.Builder builder = CreateAttachments.create();
    builder.add(sourceAttachment);
    return new ProcessCreateResult(CreateContentParams.create(createContentParams).createAttachments(builder.build()).extraDatas(extraDatas).build());
}
Also used : CreateAttachments(com.enonic.xp.attachment.CreateAttachments) MediaInfo(com.enonic.xp.media.MediaInfo) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) CreateContentParams(com.enonic.xp.content.CreateContentParams) XDatas(com.enonic.xp.schema.xdata.XDatas) ExtraDatas(com.enonic.xp.content.ExtraDatas) ProcessCreateResult(com.enonic.xp.content.processor.ProcessCreateResult)

Example 9 with CreateAttachment

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

the class ImageContentProcessor method processUpdate.

@Override
public ProcessUpdateResult processUpdate(final ProcessUpdateParams params) {
    final CreateAttachments createAttachments = params.getCreateAttachments();
    final MediaInfo mediaInfo = params.getMediaInfo();
    final CreateAttachment sourceAttachment = createAttachments == null ? null : createAttachments.first();
    final ContentEditor editor;
    if (mediaInfo != null) {
        editor = editable -> {
            final Map<XDataName, ExtraData> extraDatas = editable.extraDatas.stream().collect(Collectors.toMap(ExtraData::getName, o -> o));
            final XDatas contentXDatas = getXDatas(params.getContentType().getName());
            extractMetadata(mediaInfo, contentXDatas, sourceAttachment).forEach(extraData -> extraDatas.put(extraData.getName(), extraData));
            editable.extraDatas = ExtraDatas.create().addAll(extraDatas.values()).build();
        };
    } else {
        editor = editable -> {
            if (!params.getContentType().getName().isDescendantOfMedia()) {
                return;
            }
            editable.extraDatas = updateImageMetadata(editable);
        };
    }
    return new ProcessUpdateResult(createAttachments, editor);
}
Also used : MediaInfo(com.enonic.xp.media.MediaInfo) Cropping(com.enonic.xp.image.Cropping) ContentTypeService(com.enonic.xp.schema.content.ContentTypeService) ContentEditor(com.enonic.xp.content.ContentEditor) MEDIA_INFO_BYTE_SIZE(com.enonic.xp.media.MediaInfo.MEDIA_INFO_BYTE_SIZE) Map(java.util.Map) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) ImageIO(javax.imageio.ImageIO) GetContentTypeParams(com.enonic.xp.schema.content.GetContentTypeParams) XDataName(com.enonic.xp.schema.xdata.XDataName) Media(com.enonic.xp.content.Media) Property(com.enonic.xp.data.Property) IMAGE_INFO_PIXEL_SIZE(com.enonic.xp.media.MediaInfo.IMAGE_INFO_PIXEL_SIZE) BufferedImage(java.awt.image.BufferedImage) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) ContentProcessor(com.enonic.xp.content.processor.ContentProcessor) IMAGE_INFO_IMAGE_WIDTH(com.enonic.xp.media.MediaInfo.IMAGE_INFO_IMAGE_WIDTH) ContentType(com.enonic.xp.schema.content.ContentType) Collectors(java.util.stream.Collectors) ExtraDatas(com.enonic.xp.content.ExtraDatas) EditableContent(com.enonic.xp.content.EditableContent) InputTypeName(com.enonic.xp.inputtype.InputTypeName) FormItem(com.enonic.xp.form.FormItem) IMAGE_INFO_IMAGE_HEIGHT(com.enonic.xp.media.MediaInfo.IMAGE_INFO_IMAGE_HEIGHT) ProcessUpdateResult(com.enonic.xp.content.processor.ProcessUpdateResult) CreateContentParams(com.enonic.xp.content.CreateContentParams) ContentService(com.enonic.xp.content.ContentService) IMAGE_INFO(com.enonic.xp.media.MediaInfo.IMAGE_INFO) ProcessCreateResult(com.enonic.xp.content.processor.ProcessCreateResult) HashMap(java.util.HashMap) Attachment(com.enonic.xp.attachment.Attachment) ContentTypeName(com.enonic.xp.schema.content.ContentTypeName) ProcessCreateParams(com.enonic.xp.content.processor.ProcessCreateParams) Component(org.osgi.service.component.annotations.Component) FormItemType(com.enonic.xp.form.FormItemType) CreateAttachments(com.enonic.xp.attachment.CreateAttachments) ExtraData(com.enonic.xp.content.ExtraData) XData(com.enonic.xp.schema.xdata.XData) Exceptions(com.enonic.xp.util.Exceptions) GeoPoint(com.enonic.xp.util.GeoPoint) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) ByteSource(com.google.common.io.ByteSource) XDataService(com.enonic.xp.schema.xdata.XDataService) PropertyTree(com.enonic.xp.data.PropertyTree) ProcessUpdateParams(com.enonic.xp.content.processor.ProcessUpdateParams) XDatas(com.enonic.xp.schema.xdata.XDatas) IOException(java.io.IOException) ValueTypes(com.enonic.xp.data.ValueTypes) Preconditions(com.google.common.base.Preconditions) Input(com.enonic.xp.form.Input) Reference(org.osgi.service.component.annotations.Reference) InputStream(java.io.InputStream) CreateAttachments(com.enonic.xp.attachment.CreateAttachments) MediaInfo(com.enonic.xp.media.MediaInfo) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) ContentEditor(com.enonic.xp.content.ContentEditor) XDatas(com.enonic.xp.schema.xdata.XDatas) ProcessUpdateResult(com.enonic.xp.content.processor.ProcessUpdateResult) ExtraData(com.enonic.xp.content.ExtraData) XDataName(com.enonic.xp.schema.xdata.XDataName)

Example 10 with CreateAttachment

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

Aggregations

CreateAttachment (com.enonic.xp.attachment.CreateAttachment)10 CreateAttachments (com.enonic.xp.attachment.CreateAttachments)5 MediaInfo (com.enonic.xp.media.MediaInfo)5 ContentTypeName (com.enonic.xp.schema.content.ContentTypeName)4 Attachment (com.enonic.xp.attachment.Attachment)3 Content (com.enonic.xp.content.Content)3 CreateContentParams (com.enonic.xp.content.CreateContentParams)3 ExtraDatas (com.enonic.xp.content.ExtraDatas)3 PropertyTree (com.enonic.xp.data.PropertyTree)3 Media (com.enonic.xp.content.Media)2 UpdateContentParams (com.enonic.xp.content.UpdateContentParams)2 ProcessCreateResult (com.enonic.xp.content.processor.ProcessCreateResult)2 XDatas (com.enonic.xp.schema.xdata.XDatas)2 ByteSource (com.google.common.io.ByteSource)2 ContentEditor (com.enonic.xp.content.ContentEditor)1 ContentService (com.enonic.xp.content.ContentService)1 EditableContent (com.enonic.xp.content.EditableContent)1 ExtraData (com.enonic.xp.content.ExtraData)1 ContentProcessor (com.enonic.xp.content.processor.ContentProcessor)1 ProcessCreateParams (com.enonic.xp.content.processor.ProcessCreateParams)1