Skip to content

Commit

Permalink
mediaUrl portal function #10610
Browse files Browse the repository at this point in the history
  • Loading branch information
anatol-sialitski committed Feb 12, 2025
1 parent 1caaa2d commit f97fbc0
Show file tree
Hide file tree
Showing 16 changed files with 137 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class AttachmentUrlHandler

private String branch;

private String contentKey;
private String baseUriKey;

private boolean offline;

Expand Down Expand Up @@ -94,9 +94,9 @@ public void setBranch( final String branch )
this.branch = branch;
}

public void setContentKey( final String contentKey )
public void setBaseUriKey( final String baseUriKey )
{
this.contentKey = contentKey;
this.baseUriKey = baseUriKey;
}

public void setOffline( final Boolean offline )
Expand Down Expand Up @@ -145,7 +145,7 @@ public String createUrl()
.download( this.download )
.projectName( this.projectName )
.branch( this.branch )
.contentKey( this.contentKey );
.baseUriKey( this.baseUriKey );

if ( this.queryParams != null )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public final class ImageUrlHandler

private String branch;

private String contentKey;
private String baseUriKey;

private boolean offline;

Expand Down Expand Up @@ -108,9 +108,9 @@ public void setBranch( final String branch )
this.branch = branch;
}

public void setContentKey( final String contentKey )
public void setBaseUriKey( final String baseUriKey )
{
this.contentKey = contentKey;
this.baseUriKey = baseUriKey;
}

public void setOffline( final Boolean offline )
Expand Down Expand Up @@ -156,7 +156,7 @@ public String createUrl()
.scale( this.scale )
.projectName( this.projectName )
.branch( this.branch )
.contentKey( this.contentKey );
.baseUriKey( this.baseUriKey );

if ( this.queryParams != null )
{
Expand Down
16 changes: 8 additions & 8 deletions modules/lib/lib-portal/src/main/resources/lib/xp/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export type ImageUrlParams = IdXorPath & {
| 'full';
project?: string;
branch?: string;
contentKey?: string;
baseUriKey?: string;
offline?: boolean | undefined;
};

Expand Down Expand Up @@ -120,7 +120,7 @@ interface ImageUrlHandler {

setOffline(value: boolean): void;

setContentKey(value?: string | null): void;
setBaseUriKey(value?: string | null): void;

createUrl(): string;
}
Expand All @@ -141,7 +141,7 @@ interface ImageUrlHandler {
* @param {string} [params.type=server] URL type. Either `server` (server-relative URL) or `absolute`.
* @param {string} [params.projectName] Name of the project.
* @param {string} [params.branch] Name of the branch.
* @param {string} [params.contentKey] Key of the content.
* @param {string} [params.baseUriKey] Key of the content.
* @param {boolean} [params.offline=false] Set to true if the URL should be generated without context of the current request.
* @param {object} [params.params] Custom parameters to append to the url.
*
Expand All @@ -161,7 +161,7 @@ export function imageUrl(params: ImageUrlParams): string {
bean.setScale(params.scale);
bean.setProjectName(params.project);
bean.setBranch(params.branch);
bean.setContentKey(params.contentKey);
bean.setBaseUriKey(params.baseUriKey);
bean.setOffline(params.offline || false);

return bean.createUrl();
Expand Down Expand Up @@ -208,7 +208,7 @@ export interface AttachmentUrlParams {
params?: object;
project?: string;
branch?: string;
contentKey?: string;
baseUriKey?: string;
offline?: boolean;
}

Expand All @@ -229,7 +229,7 @@ interface AttachmentUrlHandler {

setBranch(value?: string | null): void;

setContentKey(value?: string | null): void;
setBaseUriKey(value?: string | null): void;

setOffline(value: boolean): void;

Expand All @@ -252,7 +252,7 @@ interface AttachmentUrlHandler {
* @param {string} [params.type=server] URL type. Either `server` (server-relative URL) or `absolute`.
* @param {string} [params.projectName] Name of the project.
* @param {string} [params.branch] Name of the branch.
* @param {string} [params.contentKey] Key of the content.
* @param {string} [params.baseUriKey] Key of the content.
* @param {boolean} [params.offline=false] Set to true if the URL should be generated without context of the current request.
* @param {object} [params.params] Custom parameters to append to the url.
*
Expand All @@ -268,7 +268,7 @@ export function attachmentUrl(params: AttachmentUrlParams): string {
bean.setLabel(params.label);
bean.setProjectName(params.project);
bean.setBranch(params.branch);
bean.setContentKey(params.contentKey);
bean.setBaseUriKey(params.baseUriKey);
bean.setOffline(params.offline || false);
bean.setDownload(params.download || false);
bean.setQueryParams(__.toScriptValue(params.params));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.enonic.xp.portal.url;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;

import com.google.common.collect.HashMultimap;
Expand Down Expand Up @@ -146,9 +148,9 @@ public Builder setLabel( final String label )
return this;
}

public Builder addQueryParams( final Multimap<String, String> queryParams )
public Builder addQueryParams( final Map<String, Collection<String>> queryParams )
{
this.queryParams.putAll( queryParams );
queryParams.forEach( this.queryParams::putAll );
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public final class AttachmentUrlParams

private String branch;

private String contentKey;
private String baseUriKey;

public String getId()
{
Expand Down Expand Up @@ -63,9 +63,9 @@ public String getBranch()
return branch;
}

public String getContentKey()
public String getBaseUriKey()
{
return contentKey;
return baseUriKey;
}

public AttachmentUrlParams id( final String value )
Expand Down Expand Up @@ -115,9 +115,9 @@ public AttachmentUrlParams branch( final String branch )
return this;
}

public AttachmentUrlParams contentKey( final String contentKey )
public AttachmentUrlParams baseUriKey( final String baseUriKey )
{
this.contentKey = contentKey;
this.baseUriKey = baseUriKey;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.enonic.xp.portal.url;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;

import com.google.common.collect.HashMultimap;
Expand Down Expand Up @@ -178,9 +180,9 @@ public Builder setScale( final String scale )
return this;
}

public Builder addQueryParams( final Multimap<String, String> queryParams )
public Builder addQueryParams( final Map<String, Collection<String>> queryParams )
{
this.queryParams.putAll( queryParams );
queryParams.forEach( this.queryParams::putAll );
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public final class ImageUrlParams

private String branch;

private String contentKey;
private String baseUriKey;

public String getId()
{
Expand Down Expand Up @@ -78,9 +78,9 @@ public String getBranch()
return branch;
}

public String getContentKey()
public String getBaseUriKey()
{
return contentKey;
return baseUriKey;
}

public ImageUrlParams id( final String value )
Expand Down Expand Up @@ -142,9 +142,9 @@ public ImageUrlParams branch( final String branch )
return this;
}

public ImageUrlParams contentKey( final String contentKey )
public ImageUrlParams baseUriKey( final String baseUriKey )
{
this.contentKey = contentKey;
this.baseUriKey = baseUriKey;
return this;
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
package com.enonic.xp.portal.url;

import com.enonic.xp.annotation.PublicApi;
import com.enonic.xp.branch.Branch;
import com.enonic.xp.content.Content;
import com.enonic.xp.portal.PortalRequest;
import com.enonic.xp.project.ProjectName;

@PublicApi
public interface UrlStrategyFacade
{
BaseUrlStrategy offlineBaseUrlStrategy( ProjectName projectName, Branch branch, Content content, String urlType );

BaseUrlStrategy requestBaseUrlStrategy( PortalRequest portalRequest, String urlType );

ProjectName offlineProjectName( String projectName );

ProjectName requestProjectName( String projectName, PortalRequest portalRequest );

ProjectName offlinePrefixAndBaseUrlProjectName( String projectName );

Branch offlineBranch( String branch );

Branch requestBranch( String branch, PortalRequest portalRequest );

Branch offlinePrefixAndBaseUrlBranch( String branch );

ImageUrlGeneratorParams offlineImageUrlParams( ImageUrlParams params );

ImageUrlGeneratorParams requestImageUrlParams( ImageUrlParams params );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.enonic.xp.branch.Branch;
import com.enonic.xp.content.ContentConstants;
import com.enonic.xp.content.Media;
import com.enonic.xp.portal.url.PathStrategy;
import com.enonic.xp.project.ProjectName;

import static com.enonic.xp.portal.impl.url.UrlBuilderHelper.appendParams;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.enonic.xp.portal.impl.url;

import java.nio.charset.StandardCharsets;
import java.util.Objects;

import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.hash.Hashing;
import com.google.common.io.Files;

import com.enonic.xp.attachment.Attachment;
import com.enonic.xp.branch.Branch;
import com.enonic.xp.content.Content;
import com.enonic.xp.content.ContentConstants;
import com.enonic.xp.content.Media;
import com.enonic.xp.portal.url.PathStrategy;
import com.enonic.xp.project.ProjectName;

import static com.enonic.xp.portal.impl.url.UrlBuilderHelper.appendParams;
Expand Down Expand Up @@ -80,7 +81,20 @@ private Multimap<String, String> resolveQueryParams()
private String resolveHash( final Media media )
{
final Attachment attachment = media.getMediaAttachment();
return attachment.getSha512() != null ? attachment.getSha512().substring( 0, 32 ) : null;

if ( attachment.getSha512() == null )
{
return null;
}

return Hashing.sha1()
.newHasher()
.putString( attachment.getSha512().substring( 0, 32 ), StandardCharsets.UTF_8 )
.putString( String.valueOf( media.getFocalPoint() ), StandardCharsets.UTF_8 )
.putString( String.valueOf( media.getCropping() ), StandardCharsets.UTF_8 )
.putString( String.valueOf( media.getOrientation() ), StandardCharsets.UTF_8 )
.hash()
.toString();
}

private String resolveName( final Content media, final String format )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,11 @@ private String resolveBaseUrl( final SiteConfigs siteConfigs )
private String normalizeBaseUrl( final String baseUrl )
{
final String path = UrlTypeConstants.SERVER_RELATIVE.equals( urlType ) ? URI.create( baseUrl ).getPath() : baseUrl;

return path + "/_";
if ( path.endsWith( "/" ) )
{
return path.substring( 0, path.length() - 1 ) + "/_/";
}
return path + "/_/";
}

static class Builder
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.enonic.xp.portal.impl.url;

public interface PathStrategy
{
String generatePath();
}
Loading

0 comments on commit f97fbc0

Please sign in to comment.