Skip to content

Commit

Permalink
use local repo url for local asset metadata calls
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Nov 18, 2024
1 parent 346d2a7 commit 57ce595
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ public NextGenDynamicMediaReference(@NotNull String assetId, @NotNull String fil
return asset;
}

/**
* @return True if reference points to local asset.
*/
public boolean isLocal() {
return asset != null;
}

/**
* @return Reference
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ final class NextGenDynamicMediaMetadataUrlBuilder {
public @Nullable String build(@NotNull NextGenDynamicMediaReference reference) {

// get parameters from nextgen dynamic media config for URL parameters
String repositoryId = config.getRemoteAssetsRepositoryId();
String repositoryId;
if (reference.isLocal()) {
repositoryId = config.getLocalAssetsRepositoryId();
}
else {
repositoryId = config.getRemoteAssetsRepositoryId();
}
String metadataPath = config.getAssetMetadataPath();
if (StringUtils.isAnyEmpty(repositoryId, metadataPath)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class NextGenDynamicMedia_LocalAssetWithMetadataTest {
private final AemContext context = AppAemContext.newAemContext();

private MockNextGenDynamicMediaConfig nextGenDynamicMediaConfig;
private NextGenDynamicMediaConfigServiceImpl nextGenDynamicMediaConfigService;
private MediaHandler mediaHandler;
private Resource resource;

Expand All @@ -81,10 +82,10 @@ class NextGenDynamicMedia_LocalAssetWithMetadataTest {
void setUp(WireMockRuntimeInfo wmRuntimeInfo) {
nextGenDynamicMediaConfig = context.registerInjectActivateService(MockNextGenDynamicMediaConfig.class);
nextGenDynamicMediaConfig.setEnabled(true);
nextGenDynamicMediaConfig.setRepositoryId("localhost:" + wmRuntimeInfo.getHttpPort());
context.registerInjectActivateService(NextGenDynamicMediaConfigServiceImpl.class,
nextGenDynamicMediaConfig.setRepositoryId("remoterepo1");
nextGenDynamicMediaConfigService = context.registerInjectActivateService(NextGenDynamicMediaConfigServiceImpl.class,
"enabledLocalAssets", true,
"localAssetsRepositoryId", "repo1");
"localAssetsRepositoryId", "localhost:" + wmRuntimeInfo.getHttpPort());
context.registerInjectActivateService(NextGenDynamicMediaMetadataServiceImpl.class,
"enabled", true);

Expand Down Expand Up @@ -114,14 +115,16 @@ void testLocalAsset() {
Rendition rendition = media.getRendition();

UriTemplate uriTemplateScaleWidth = rendition.getUriTemplate(UriTemplateType.SCALE_WIDTH);
assertEquals("https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/my-image.jpg?preferwebp=true&quality=85&width={width}",
assertEquals("https://" + nextGenDynamicMediaConfigService.getLocalAssetsRepositoryId()
+ "/adobe/assets/" + SAMPLE_ASSET_ID + "/as/my-image.jpg?preferwebp=true&quality=85&width={width}",
uriTemplateScaleWidth.getUriTemplate());
assertEquals(UriTemplateType.SCALE_WIDTH, uriTemplateScaleWidth.getType());
assertEquals(1200, uriTemplateScaleWidth.getMaxWidth());
assertEquals(800, uriTemplateScaleWidth.getMaxHeight());

UriTemplate uriTemplateScaleHeight = rendition.getUriTemplate(UriTemplateType.SCALE_HEIGHT);
assertEquals("https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/my-image.jpg?height={height}&preferwebp=true&quality=85",
assertEquals("https://" + nextGenDynamicMediaConfigService.getLocalAssetsRepositoryId()
+ "/adobe/assets/" + SAMPLE_ASSET_ID + "/as/my-image.jpg?height={height}&preferwebp=true&quality=85",
uriTemplateScaleHeight.getUriTemplate());
assertEquals(UriTemplateType.SCALE_HEIGHT, uriTemplateScaleHeight.getType());
assertEquals(1200, uriTemplateScaleHeight.getMaxWidth());
Expand Down Expand Up @@ -218,12 +221,13 @@ void testLocalAsset_NoUUID() {
assertEquals(MediaInvalidReason.MEDIA_REFERENCE_INVALID, media.getMediaInvalidReason());
}

private static void assertUrl(Media media, String urlParams, String extension) {
private void assertUrl(Media media, String urlParams, String extension) {
assertEquals(buildUrl(urlParams, extension), media.getUrl());
}

private static String buildUrl(String urlParams, String extension) {
return "https://repo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/my-image."
private String buildUrl(String urlParams, String extension) {
return "https://" + nextGenDynamicMediaConfigService.getLocalAssetsRepositoryId()
+ "/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/my-image."
+ extension + "?" + urlParams;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void testFromReference() {
assertEquals(SAMPLE_FILENAME, underTest.getFileName());
assertEquals(SAMPLE_REFERENCE, underTest.toReference());
assertNull(underTest.getAsset());
assertFalse(underTest.isLocal());
assertEquals(SAMPLE_REFERENCE, underTest.toString());
}

Expand Down Expand Up @@ -119,6 +120,7 @@ void testFromDamAssetReference_AssetWithUUID_Approved() {
assertEquals(SAMPLE_FILENAME, underTest.getFileName());
assertEquals(SAMPLE_REFERENCE, underTest.toReference());
assertEquals(asset, underTest.getAsset());
assertTrue(underTest.isLocal());
assertEquals(SAMPLE_REFERENCE, underTest.toString());
}

Expand Down

0 comments on commit 57ce595

Please sign in to comment.