Search in sources :

Example 1 with CreateAttachment

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

the class AddAttachmentHandler method execute.

public void execute() {
    UpdateContentParams updateContent = new UpdateContentParams();
    if (!this.key.startsWith("/")) {
        updateContent.contentId(ContentId.from(this.key));
    } else {
        final Content contentByPath = this.contentService.getByPath(ContentPath.from(key));
        updateContent.contentId(contentByPath.getId());
    }
    final CreateAttachment createAttachment = CreateAttachment.create().name(this.name).label(this.label).mimeType(this.mimeType).byteSource(getData()).build();
    updateContent.createAttachments(CreateAttachments.from(createAttachment));
    contentService.update(updateContent);
}
Also used : CreateAttachment(com.enonic.xp.attachment.CreateAttachment) UpdateContentParams(com.enonic.xp.content.UpdateContentParams) Content(com.enonic.xp.content.Content)

Example 2 with CreateAttachment

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

the class CreateNodeParamsFactory method produce.

public CreateNodeParams produce() {
    final PropertyTree contentAsData = contentDataSerializer.toCreateNodeData(params);
    final PropertySet extraDataSet = contentAsData.getPropertySet(PropertyPath.from(ContentPropertyNames.EXTRA_DATA));
    final String language = contentAsData.getString(PropertyPath.from(ContentPropertyNames.LANGUAGE));
    final SiteConfigs siteConfigs = new SiteConfigsDataSerializer().fromProperties(contentAsData.getPropertySet(PropertyPath.from(ContentPropertyNames.DATA))).build();
    final Page page = contentAsData.hasProperty(COMPONENTS) ? contentDataSerializer.fromPageData(contentAsData.getRoot()) : null;
    final ExtraDatas extraData = extraDataSet != null ? contentDataSerializer.fromExtraData(extraDataSet) : null;
    final ContentIndexConfigFactory.Builder indexConfigFactoryBuilder = ContentIndexConfigFactory.create().contentTypeName(params.getType()).siteConfigs(siteConfigs).siteService(siteService).xDataService(xDataService).contentTypeService(contentTypeService);
    if (page != null) {
        indexConfigFactoryBuilder.page(page).pageDescriptorService(pageDescriptorService).partDescriptorService(partDescriptorService).layoutDescriptorService(layoutDescriptorService);
    }
    if (extraData != null) {
        indexConfigFactoryBuilder.extraDatas(extraData);
    }
    if (!nullToEmpty(language).isBlank()) {
        indexConfigFactoryBuilder.language(language);
    }
    final IndexConfigDocument indexConfigDocument = indexConfigFactoryBuilder.build().produce();
    final CreateNodeParams.Builder builder = CreateNodeParams.create().name(resolveNodeName(params.getName())).parent(ContentNodeHelper.translateContentParentToNodeParentPath(params.getParent())).data(contentAsData).indexConfigDocument(indexConfigDocument).permissions(params.getPermissions()).inheritPermissions(params.isInheritPermissions()).childOrder(params.getChildOrder()).nodeType(ContentConstants.CONTENT_NODE_COLLECTION);
    for (final CreateAttachment attachment : params.getCreateAttachments()) {
        builder.attachBinary(attachment.getBinaryReference(), attachment.getByteSource());
    }
    return builder.build();
}
Also used : SiteConfigsDataSerializer(com.enonic.xp.site.SiteConfigsDataSerializer) Page(com.enonic.xp.page.Page) SiteConfigs(com.enonic.xp.site.SiteConfigs) CreateNodeParams(com.enonic.xp.node.CreateNodeParams) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) PropertyTree(com.enonic.xp.data.PropertyTree) PropertySet(com.enonic.xp.data.PropertySet) ExtraDatas(com.enonic.xp.content.ExtraDatas) IndexConfigDocument(com.enonic.xp.index.IndexConfigDocument) ContentIndexConfigFactory(com.enonic.xp.core.impl.content.index.ContentIndexConfigFactory)

Example 3 with CreateAttachment

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

the class CreateMediaCommand method doExecute.

private Content doExecute() {
    final MediaInfo mediaInfo = mediaInfoService.parseMediaInfo(params.getByteSource());
    if ((params.getMimeType() == null || isBinaryContentType(params.getMimeType())) && mediaInfo.getMediaType() != null) {
        params.mimeType(mediaInfo.getMediaType());
    }
    Preconditions.checkNotNull(params.getMimeType(), "Unable to resolve media type");
    final ContentTypeName resolvedTypeFromMimeType = ContentTypeFromMimeTypeResolver.resolve(params.getMimeType());
    final ContentTypeName type = resolvedTypeFromMimeType != null ? resolvedTypeFromMimeType : isExecutableContentType(params.getMimeType(), params.getName()) ? ContentTypeName.executableMedia() : ContentTypeName.unknownMedia();
    final PropertyTree data = new PropertyTree();
    new MediaFormDataBuilder().type(type).attachment(params.getName()).focalX(params.getFocalX()).focalY(params.getFocalY()).caption(params.getCaption()).artist(params.getArtist()).copyright(params.getCopyright()).tags(params.getTags()).build(data);
    final CreateAttachment mediaAttachment = CreateAttachment.create().name(params.getName()).mimeType(params.getMimeType()).label("source").byteSource(params.getByteSource()).text(type.isTextualMedia() ? mediaInfo.getTextContent() : "").build();
    final CreateContentParams createContentParams = CreateContentParams.create().name(params.getName()).parent(params.getParent()).requireValid(true).type(type).displayName(trimExtension(params.getName())).contentData(data).createAttachments(CreateAttachments.from(mediaAttachment)).inheritPermissions(true).build();
    final CreateContentCommand createCommand = CreateContentCommand.create(this).mediaInfo(mediaInfo).params(createContentParams).siteService(this.siteService).xDataService(this.xDataService).formDefaultValuesProcessor(this.formDefaultValuesProcessor).pageDescriptorService(this.pageDescriptorService).partDescriptorService(this.partDescriptorService).layoutDescriptorService(this.layoutDescriptorService).contentDataSerializer(this.contentDataSerializer).allowUnsafeAttachmentNames(this.allowUnsafeAttachmentNames).build();
    return createCommand.execute();
}
Also used : MediaInfo(com.enonic.xp.media.MediaInfo) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) CreateContentParams(com.enonic.xp.content.CreateContentParams) PropertyTree(com.enonic.xp.data.PropertyTree) ContentTypeName(com.enonic.xp.schema.content.ContentTypeName)

Example 4 with CreateAttachment

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

the class UpdateNodeParamsFactory method produce.

public UpdateNodeParams produce() {
    final Content editedContent = params.getEditedContent();
    final CreateAttachments createAttachments = params.getCreateAttachments();
    final NodeEditor nodeEditor = toNodeEditor(params);
    final UpdateNodeParams.Builder builder = UpdateNodeParams.create().id(NodeId.from(editedContent.getId())).editor(nodeEditor);
    if (createAttachments != null) {
        for (final CreateAttachment createAttachment : createAttachments) {
            builder.attachBinary(createAttachment.getBinaryReference(), createAttachment.getByteSource());
        }
    }
    return builder.build();
}
Also used : CreateAttachments(com.enonic.xp.attachment.CreateAttachments) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) Content(com.enonic.xp.content.Content) NodeEditor(com.enonic.xp.node.NodeEditor) UpdateNodeParams(com.enonic.xp.node.UpdateNodeParams)

Example 5 with CreateAttachment

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

the class UpdateMediaCommand method doExecute.

private Content doExecute() {
    final MediaInfo mediaInfo = mediaInfoService.parseMediaInfo(params.getByteSource());
    if ((params.getMimeType() == null || isBinaryContentType(params.getMimeType())) && mediaInfo.getMediaType() != null) {
        params.mimeType(mediaInfo.getMediaType());
    }
    Preconditions.checkNotNull(params.getMimeType(), "Unable to resolve media type");
    final ContentTypeName resolvedTypeFromMimeType = ContentTypeFromMimeTypeResolver.resolve(params.getMimeType());
    final ContentTypeName type = resolvedTypeFromMimeType != null ? resolvedTypeFromMimeType : isExecutableContentType(params.getMimeType(), params.getName()) ? ContentTypeName.executableMedia() : ContentTypeName.unknownMedia();
    final Content existingContent = getContent(params.getContent());
    Preconditions.checkArgument(existingContent.getType().equals(type), "Updated content must be of type: " + existingContent.getType());
    final CreateAttachment mediaAttachment = CreateAttachment.create().name(params.getName()).mimeType(params.getMimeType()).label("source").byteSource(params.getByteSource()).text(type.isTextualMedia() ? mediaInfo.getTextContent() : null).build();
    final MediaFormDataBuilder mediaFormBuilder = new MediaFormDataBuilder().type(type).attachment(params.getName()).focalX(params.getFocalX()).focalY(params.getFocalY()).caption(params.getCaption()).artist(params.getArtist()).copyright(params.getCopyright()).tags(params.getTags());
    final UpdateContentParams updateParams = new UpdateContentParams().contentId(params.getContent()).clearAttachments(true).createAttachments(CreateAttachments.from(mediaAttachment)).editor(editable -> mediaFormBuilder.build(editable.data));
    return UpdateContentCommand.create(this).params(updateParams).mediaInfo(mediaInfo).contentTypeService(this.contentTypeService).siteService(this.siteService).xDataService(this.xDataService).pageDescriptorService(this.pageDescriptorService).partDescriptorService(this.partDescriptorService).layoutDescriptorService(this.layoutDescriptorService).contentDataSerializer(this.contentDataSerializer).build().execute();
}
Also used : MediaInfo(com.enonic.xp.media.MediaInfo) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) UpdateContentParams(com.enonic.xp.content.UpdateContentParams) Content(com.enonic.xp.content.Content) ContentTypeName(com.enonic.xp.schema.content.ContentTypeName)

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