use of com.enonic.xp.attachment.Attachments 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);
}
}
use of com.enonic.xp.attachment.Attachments 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()));
}
use of com.enonic.xp.attachment.Attachments in project xp by enonic.
the class PortalUrlServiceImpl_processHtmlTest method process_multiple_links.
@Test
public void process_multiple_links() {
// Creates a content with attachments
final Attachment thumb = Attachment.create().label("thumb").name("a1.jpg").mimeType("image/jpg").build();
final Attachment source = Attachment.create().label("source").name("a2.jpg").mimeType("image/jpg").build();
final Attachments attachments = Attachments.from(thumb, source);
final Content content = Content.create(ContentFixtures.newContent()).attachments(attachments).build();
Mockito.when(this.contentService.getById(content.getId())).thenReturn(content);
Mockito.when(this.contentService.getBinaryKey(content.getId(), source.getBinaryReference())).thenReturn("binaryHash2");
// Process an html text containing multiple links, on multiple lines, to this content as a media and as a content
final ProcessHtmlParams params = new ProcessHtmlParams().portalRequest(this.portalRequest).value("<p>A content link: <a href=\"content://" + content.getId() + "\">FirstLink</a></p>\n" + "<p>A second content link: <a href=\"content://" + content.getId() + "\">SecondLink</a>" + " and a download link: <a href=\"media://download/" + content.getId() + "\">Download</a></p>\n" + "<p>An external link: <a href=\"http://www.enonic.com\">An external link</a></p>\n" + "<p> </p>\n" + "<a href=\"media://inline/" + content.getId() + "\">Inline</a>");
// Checks the returned value
final String processedHtml = this.service.processHtml(params);
assertEquals("<p>A content link: <a href=\"/site/default/draft" + content.getPath() + "\">FirstLink</a></p>\n" + "<p>A second content link: <a href=\"/site/default/draft" + content.getPath() + "\">SecondLink</a>" + " and a download link: <a href=\"/site/default/draft/context/path/_/attachment/download/" + content.getId() + ":binaryHash2/" + source.getName() + "\">Download</a></p>\n" + "<p>An external link: <a href=\"http://www.enonic.com\">An external link</a></p>\n" + "<p> </p>\n" + "<a href=\"/site/default/draft/context/path/_/attachment/inline/" + content.getId() + ":binaryHash2/" + source.getName() + "\">Inline</a>", processedHtml);
}
use of com.enonic.xp.attachment.Attachments in project xp by enonic.
the class PortalUrlServiceImpl_processHtmlTest method process_single_media.
@Test
public void process_single_media() {
// Creates a content with attachments
final Attachment thumb = Attachment.create().label("thumb").name("a1.jpg").mimeType("image/jpg").build();
final Attachment source = Attachment.create().label("source").name("a2.jpg").mimeType("image/jpg").build();
final Attachments attachments = Attachments.from(thumb, source);
final Content content = Content.create(ContentFixtures.newContent()).attachments(attachments).build();
Mockito.when(this.contentService.getById(content.getId())).thenReturn(content);
Mockito.when(this.contentService.getBinaryKey(content.getId(), source.getBinaryReference())).thenReturn("binaryHash2");
// Process an html text containing an inline link to this content
ProcessHtmlParams params = new ProcessHtmlParams().portalRequest(this.portalRequest).value("<a href=\"media://inline/" + content.getId() + "\">Media</a>");
// Checks that the URL of the source attachment of the content is returned
String processedHtml = this.service.processHtml(params);
assertEquals("<a href=\"/site/default/draft/context/path/_/attachment/inline/" + content.getId() + ":binaryHash2/" + source.getName() + "\">Media</a>", processedHtml);
// Process an html text containing a download link to this content
params = new ProcessHtmlParams().portalRequest(this.portalRequest).value("<a href=\"media://download/" + content.getId() + "\">Media</a>");
// Checks that the URL of the source attachment of the content is returned
processedHtml = this.service.processHtml(params);
assertEquals("<a href=\"/site/default/draft/context/path/_/attachment/download/" + content.getId() + ":binaryHash2/" + source.getName() + "\">Media</a>", processedHtml);
// Process an html text containing an inline link to this content in a img tag
params = new ProcessHtmlParams().portalRequest(this.portalRequest).value("<a href=\"/some/page\"><img src=\"media://inline/" + content.getId() + "\">Media</a>");
// Checks that the URL of the source attachment of the content is returned
processedHtml = this.service.processHtml(params);
assertEquals("<a href=\"/some/page\"><img src=\"/site/default/draft/context/path/_/attachment/inline/" + content.getId() + ":binaryHash2/" + source.getName() + "\">Media</a>", processedHtml);
}
use of com.enonic.xp.attachment.Attachments in project xp by enonic.
the class ContentServiceImplTest_update method update_content_image.
@Test
public void update_content_image() throws Exception {
final ByteSource image = loadImage("cat-small.jpg");
final CreateContentParams createContentParams = CreateContentParams.create().contentData(new PropertyTree()).displayName("This is my content").parent(ContentPath.ROOT).type(ContentTypeName.imageMedia()).createAttachments(createAttachment("cat", "image/jpg", image)).build();
final Content content = this.contentService.create(createContentParams);
final UpdateContentParams updateContentParams = new UpdateContentParams();
updateContentParams.contentId(content.getId()).editor(edit -> {
edit.displayName = "new display name";
}).clearAttachments(true).createAttachments(createAttachment("darth", "image/jpg", loadImage("darth-small.jpg")));
this.contentService.update(updateContentParams);
final Content storedContent = this.contentService.getById(content.getId());
final Attachments attachments = storedContent.getAttachments();
assertEquals(1, attachments.getSize());
}
Aggregations