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()));
}
}
}
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));
}
}
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());
}
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);
}
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());
}
Aggregations