Search in sources :

Example 1 with ImageUrlParams

use of com.enonic.xp.portal.url.ImageUrlParams in project xp by enonic.

the class PortalUrlServiceImpl_imageUrlTest method createUrl_withoutContentPath.

@Test
public void createUrl_withoutContentPath() {
    this.portalRequest.setContent(createContent());
    final ImageUrlParams params = new ImageUrlParams().portalRequest(this.portalRequest).contextPathType(ContextPathType.VHOST.getValue()).scale("max(300)").validate();
    final String url = this.service.imageUrl(params);
    assertEquals("/site/default/draft/_/image/123456:8cf45815bba82c9711c673c9bb7304039a790026/max-300/mycontent", url);
}
Also used : ImageUrlParams(com.enonic.xp.portal.url.ImageUrlParams) Test(org.junit.jupiter.api.Test)

Example 2 with ImageUrlParams

use of com.enonic.xp.portal.url.ImageUrlParams in project xp by enonic.

the class PortalUrlServiceImpl_imageUrlTest method createUrl_allOptions.

@Test
public void createUrl_allOptions() {
    this.portalRequest.setContent(createContent());
    final ImageUrlParams params = new ImageUrlParams().quality(90).background("00ff00").filter("scale(10,10)").format("jpg").portalRequest(this.portalRequest).scale("max(300)").validate();
    final String url = this.service.imageUrl(params);
    assertEquals("/site/default/draft/a/b/mycontent/_/image/123456:8cf45815bba82c9711c673c9bb7304039a790026/max-300/mycontent.jpg?" + "quality=90&background=00ff00&filter=scale%2810%2C10%29", url);
}
Also used : ImageUrlParams(com.enonic.xp.portal.url.ImageUrlParams) Test(org.junit.jupiter.api.Test)

Example 3 with ImageUrlParams

use of com.enonic.xp.portal.url.ImageUrlParams in project xp by enonic.

the class HtmlLinkProcessor method process.

public String process(final String text, final String urlType, final PortalRequest portalRequest, final List<Integer> imageWidths, final String imageSizes) {
    String processedHtml = text;
    final ImmutableMap<String, ImageStyle> imageStyleMap = getImageStyleMap(portalRequest);
    final Matcher contentMatcher = CONTENT_PATTERN.matcher(text);
    while (contentMatcher.find()) {
        if (contentMatcher.groupCount() >= NB_GROUPS) {
            final String tagName = contentMatcher.group(TAG_NAME_INDEX);
            final String attr = contentMatcher.group(ATTR_INDEX);
            final String attrValue = contentMatcher.group(ATTR_VALUE_INDEX);
            final String link = contentMatcher.group(LINK_INDEX);
            final String type = contentMatcher.group(TYPE_INDEX);
            final String mode = contentMatcher.group(MODE_INDEX);
            final String id = contentMatcher.group(ID_INDEX);
            final String urlParamsString = contentMatcher.groupCount() == PARAMS_INDEX ? contentMatcher.group(PARAMS_INDEX) : null;
            switch(type) {
                case CONTENT_TYPE:
                    PageUrlParams pageUrlParams = new PageUrlParams().type(urlType).id(id).portalRequest(portalRequest);
                    final String pageUrl = portalUrlService.pageUrl(pageUrlParams);
                    processedHtml = processedHtml.replaceFirst(Pattern.quote(attrValue), "\"" + pageUrl + "\"");
                    break;
                case IMAGE_TYPE:
                    final Map<String, String> urlParams = extractUrlParams(urlParamsString);
                    ImageStyle imageStyle = getImageStyle(imageStyleMap, urlParams);
                    ImageUrlParams imageUrlParams = new ImageUrlParams().type(urlType).id(id).scale(getScale(imageStyle, urlParams, null)).filter(getFilter(imageStyle)).portalRequest(portalRequest);
                    final String imageUrl = portalUrlService.imageUrl(imageUrlParams);
                    final StringBuilder replacement = new StringBuilder("\"" + imageUrl + "\"");
                    if ("img".equals(tagName) && "src".equals(attr)) {
                        final String srcsetValues = Objects.requireNonNullElse(imageWidths, List.<Integer>of()).stream().map(imageWidth -> {
                            final ImageUrlParams imageParams = new ImageUrlParams().type(urlType).id(id).scale(getScale(imageStyle, urlParams, imageWidth)).filter(getFilter(imageStyle)).portalRequest(portalRequest);
                            return portalUrlService.imageUrl(imageParams) + " " + imageWidth + "w";
                        }).collect(Collectors.joining(","));
                        if (!srcsetValues.isEmpty()) {
                            replacement.append(" srcset=\"").append(srcsetValues).append("\"");
                        }
                        if (imageSizes != null && !imageSizes.isBlank()) {
                            replacement.append(" sizes=\"").append(imageSizes).append("\"");
                        }
                    }
                    processedHtml = processedHtml.replaceFirst(Pattern.quote(attrValue), replacement.toString());
                    break;
                default:
                    AttachmentUrlParams attachmentUrlParams = new AttachmentUrlParams().type(urlType).id(id).download(DOWNLOAD_MODE.equals(mode)).portalRequest(portalRequest);
                    final String attachmentUrl = portalUrlService.attachmentUrl(attachmentUrlParams);
                    processedHtml = processedHtml.replaceFirst(Pattern.quote(attrValue), "\"" + attachmentUrl + "\"");
                    break;
            }
        }
    }
    return processedHtml;
}
Also used : PortalUrlService(com.enonic.xp.portal.url.PortalUrlService) ImmutableSet(com.google.common.collect.ImmutableSet) ImageStyle(com.enonic.xp.style.ImageStyle) ImmutableMap(com.google.common.collect.ImmutableMap) Collectors(java.util.stream.Collectors) PageUrlParams(com.enonic.xp.portal.url.PageUrlParams) ApplicationKey(com.enonic.xp.app.ApplicationKey) Objects(java.util.Objects) AttachmentUrlParams(com.enonic.xp.portal.url.AttachmentUrlParams) StyleDescriptors(com.enonic.xp.style.StyleDescriptors) List(java.util.List) Site(com.enonic.xp.site.Site) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) PortalRequest(com.enonic.xp.portal.PortalRequest) ImageUrlParams(com.enonic.xp.portal.url.ImageUrlParams) StyleDescriptorService(com.enonic.xp.style.StyleDescriptorService) Map(java.util.Map) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) Splitter(com.google.common.base.Splitter) ApplicationKeys(com.enonic.xp.app.ApplicationKeys) Matcher(java.util.regex.Matcher) PageUrlParams(com.enonic.xp.portal.url.PageUrlParams) AttachmentUrlParams(com.enonic.xp.portal.url.AttachmentUrlParams) ImageStyle(com.enonic.xp.style.ImageStyle) ImageUrlParams(com.enonic.xp.portal.url.ImageUrlParams)

Example 4 with ImageUrlParams

use of com.enonic.xp.portal.url.ImageUrlParams in project xp by enonic.

the class ImageUrlBuilderTest method init.

@BeforeEach
public void init() {
    final PortalRequest portalRequest = new PortalRequest();
    portalRequest.setBranch(Branch.from("draft"));
    portalRequest.setApplicationKey(ApplicationKey.from("myapplication"));
    portalRequest.setBaseUri("/site");
    portalRequest.setContentPath(ContentPath.from("context/path"));
    this.imageUrlParams = new ImageUrlParams().portalRequest(portalRequest).scale("testScale");
    urlBuilder = new ImageUrlBuilder();
    urlBuilder.setParams(imageUrlParams);
    final Attachment attachment = Attachment.create().name("attachmentName").mimeType("attachmentMimeType").size(1).build();
    media = Mockito.mock(Media.class);
    final ContentId contentId = ContentId.from("testID");
    Mockito.when(media.getId()).thenReturn(contentId);
    Mockito.when(media.getName()).thenReturn(ContentName.from("testName"));
    Mockito.when(media.getType()).thenReturn(ContentTypeName.imageMedia());
    Mockito.when(media.getMediaAttachment()).thenReturn(attachment);
    final ContentService contentService = Mockito.mock(ContentService.class);
    Mockito.when(contentService.getByPath(Mockito.any())).thenReturn(media);
    Mockito.when(contentService.getById(Mockito.any())).thenReturn(media);
    Mockito.when(contentService.getBinaryKey(contentId, attachment.getBinaryReference())).thenReturn("binaryHash");
    urlBuilder.contentService = contentService;
}
Also used : Media(com.enonic.xp.content.Media) Attachment(com.enonic.xp.attachment.Attachment) ContentId(com.enonic.xp.content.ContentId) ContentService(com.enonic.xp.content.ContentService) PortalRequest(com.enonic.xp.portal.PortalRequest) ImageUrlParams(com.enonic.xp.portal.url.ImageUrlParams) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with ImageUrlParams

use of com.enonic.xp.portal.url.ImageUrlParams in project xp by enonic.

the class PortalUrlServiceImpl_imageUrlTest method createUrl_absolute.

@Test
public void createUrl_absolute() {
    this.portalRequest.setContent(createContent());
    final ImageUrlParams params = new ImageUrlParams().type(UrlTypeConstants.ABSOLUTE).portalRequest(this.portalRequest).scale("max(300)").validate();
    when(req.getServerName()).thenReturn("localhost");
    when(req.getScheme()).thenReturn("http");
    when(req.getServerPort()).thenReturn(80);
    final String url = this.service.imageUrl(params);
    assertEquals("http://localhost/site/default/draft/a/b/mycontent/_/image/123456:8cf45815bba82c9711c673c9bb7304039a790026/max-300/mycontent", url);
}
Also used : ImageUrlParams(com.enonic.xp.portal.url.ImageUrlParams) Test(org.junit.jupiter.api.Test)

Aggregations

ImageUrlParams (com.enonic.xp.portal.url.ImageUrlParams)12 Test (org.junit.jupiter.api.Test)10 PortalRequest (com.enonic.xp.portal.PortalRequest)2 ApplicationKey (com.enonic.xp.app.ApplicationKey)1 ApplicationKeys (com.enonic.xp.app.ApplicationKeys)1 Attachment (com.enonic.xp.attachment.Attachment)1 ContentId (com.enonic.xp.content.ContentId)1 ContentService (com.enonic.xp.content.ContentService)1 Media (com.enonic.xp.content.Media)1 AttachmentUrlParams (com.enonic.xp.portal.url.AttachmentUrlParams)1 PageUrlParams (com.enonic.xp.portal.url.PageUrlParams)1 PortalUrlService (com.enonic.xp.portal.url.PortalUrlService)1 Site (com.enonic.xp.site.Site)1 ImageStyle (com.enonic.xp.style.ImageStyle)1 StyleDescriptorService (com.enonic.xp.style.StyleDescriptorService)1 StyleDescriptors (com.enonic.xp.style.StyleDescriptors)1 Splitter (com.google.common.base.Splitter)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1