use of com.enonic.xp.attachment.Attachment in project xp by enonic.
the class AttachmentUrlBuilder method buildUrl.
@Override
protected void buildUrl(final StringBuilder url, final Multimap<String, String> params) {
super.buildUrl(url, params);
appendPart(url, this.portalRequest.getContentPath().toString());
appendPart(url, "_");
appendPart(url, "attachment");
if (this.params.isDownload()) {
appendPart(url, "download");
} else {
appendPart(url, "inline");
}
final Content content = resolveContent();
Attachment attachment = resolveAttachment(content);
String hash = resolveHash(content, attachment);
appendPart(url, content.getId().toString() + ":" + hash);
appendPart(url, attachment.getName());
}
use of com.enonic.xp.attachment.Attachment in project xp by enonic.
the class ContentFixtures method newMedia.
public static Media newMedia() {
final Attachment attachment = Attachment.create().name("logo.png").mimeType("image/png").label("small").build();
final PropertyTree data = newPropertyTree();
data.addString("media", attachment.getName());
final PropertyTree mediaData = newPropertyTree();
mediaData.setLong(IMAGE_INFO_PIXEL_SIZE, 300L * 200L);
mediaData.setLong(IMAGE_INFO_IMAGE_HEIGHT, 200L);
mediaData.setLong(IMAGE_INFO_IMAGE_WIDTH, 300L);
mediaData.setLong(MEDIA_INFO_BYTE_SIZE, 100000L);
final ExtraData mediaExtraData = new ExtraData(MediaInfo.IMAGE_INFO_METADATA_NAME, mediaData);
final Media.Builder builder = Media.create();
builder.id(ContentId.from("123456"));
builder.type(ContentTypeName.imageMedia());
builder.name("mycontent");
builder.displayName("My Content");
builder.parentPath(ContentPath.from("/a/b"));
builder.modifier(PrincipalKey.from("user:system:admin"));
builder.modifiedTime(Instant.ofEpochSecond(0));
builder.creator(PrincipalKey.from("user:system:admin"));
builder.createdTime(Instant.ofEpochSecond(0));
builder.attachments(Attachments.from(attachment));
builder.data(data);
builder.addExtraData(new ExtraData(XDataName.from("myapplication:myschema"), newTinyPropertyTree()));
builder.addExtraData(mediaExtraData);
builder.page(newPage());
return builder.build();
}
use of com.enonic.xp.attachment.Attachment in project xp by enonic.
the class PortalUrlServiceImpl_attachmentUrlTest method createContent.
private Content createContent() {
final Attachment a1 = Attachment.create().label("thumb").name("a1.jpg").mimeType("image/jpg").build();
final Attachment a2 = Attachment.create().label("source").name("a2.jpg").mimeType("image/jpg").build();
final Attachments attachments = Attachments.from(a1, a2);
final Content content = Content.create(ContentFixtures.newContent()).attachments(attachments).build();
Mockito.when(this.contentService.getByPath(content.getPath())).thenReturn(content);
Mockito.when(this.contentService.getById(content.getId())).thenReturn(content);
Mockito.when(this.contentService.getBinaryKey(content.getId(), a1.getBinaryReference())).thenReturn("binaryHash1");
Mockito.when(this.contentService.getBinaryKey(content.getId(), a2.getBinaryReference())).thenReturn("binaryHash2");
return content;
}
use of com.enonic.xp.attachment.Attachment 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.Attachment in project xp by enonic.
the class AttachmentHandlerWorker method execute.
@Override
public PortalResponse execute() throws Exception {
final Content content = getContent(this.id);
final Attachment attachment = resolveAttachment(content, this.name);
final BinaryReference binaryReference = attachment.getBinaryReference();
final ByteSource binary = resolveBinary(this.id, binaryReference);
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 MediaType contentType = MediaType.parse(attachment.getMimeType());
final PortalResponse.Builder portalResponse = PortalResponse.create().contentType(contentType).body(binary);
if (this.download) {
portalResponse.header("Content-Disposition", contentDispositionAttachment(attachment.getName()));
}
if (this.name.endsWith(".svgz")) {
portalResponse.header("Content-Encoding", "gzip");
}
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(this.id, binaryReference))) {
portalResponse.header(HttpHeaders.CACHE_CONTROL, cacheControlHeaderConfig);
}
}
new RangeRequestHelper().handleRangeRequest(request, portalResponse, binary, contentType);
return portalResponse.build();
}
Aggregations