Skip to content

Commit

Permalink
update asset title & description
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Nov 26, 2024
1 parent 6908da9 commit 2da8dcc
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
*/
package io.wcm.handler.mediasource.ngdm;

import static com.day.cq.dam.api.DamConstants.DC_DESCRIPTION;
import static com.day.cq.dam.api.DamConstants.DC_TITLE;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.sling.api.resource.Resource;
Expand All @@ -32,37 +35,50 @@
import io.wcm.handler.media.UriTemplate;
import io.wcm.handler.media.UriTemplateType;
import io.wcm.handler.mediasource.ngdm.impl.NextGenDynamicMediaContext;
import io.wcm.handler.mediasource.ngdm.impl.metadata.NextGenDynamicMediaMetadata;

/**
* {@link Asset} implementation for Next Gen. Dynamic Media remote assets.
*/
final class NextGenDynamicMediaAsset implements Asset {

private final NextGenDynamicMediaContext context;
private final MediaArgs defaultMediaArgs;
private final ValueMap properties;

NextGenDynamicMediaAsset(@NotNull NextGenDynamicMediaContext context) {
this.context = context;
this.defaultMediaArgs = context.getDefaultMediaArgs();

NextGenDynamicMediaMetadata metadata = context.getMetadata();
if (metadata != null) {
this.properties = metadata.getProperties();
}
else {
this.properties = ValueMap.EMPTY;
}
}

@Override
public @Nullable String getTitle() {
return context.getReference().getFileName();
return StringUtils.defaultString(properties.get(DC_TITLE, String.class),
context.getReference().getFileName());
}

@Override
public @Nullable String getAltText() {
if (context.getDefaultMediaArgs().isDecorative()) {
if (defaultMediaArgs.isDecorative()) {
return "";
}
else {
return context.getDefaultMediaArgs().getAltText();
if (!defaultMediaArgs.isForceAltValueFromAsset() && StringUtils.isNotEmpty(defaultMediaArgs.getAltText())) {
return defaultMediaArgs.getAltText();
}
return StringUtils.defaultString(getDescription(), getTitle());
}

@Override
public @Nullable String getDescription() {
// not supported
return null;
return properties.get(DC_DESCRIPTION, String.class);
}

@Override
Expand All @@ -77,7 +93,7 @@ final class NextGenDynamicMediaAsset implements Asset {

@Override
public @Nullable Rendition getDefaultRendition() {
return getRendition(this.context.getDefaultMediaArgs());
return getRendition(defaultMediaArgs);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ void testAsset() {
Asset asset = media.getAsset();
assertNotNull(asset);
assertEquals(SAMPLE_FILENAME, asset.getTitle());
assertNull(asset.getAltText());
assertNull(asset.getDescription());
assertEquals("my-image.jpg", asset.getAltText());
assertNull(asset.getDescription());
assertEquals(SAMPLE_REFERENCE, asset.getPath());
assertEquals(ValueMap.EMPTY, asset.getProperties());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void testAsset() {
Asset asset = media.getAsset();
assertNotNull(asset);
assertEquals(SAMPLE_FILENAME, asset.getTitle());
assertNull(asset.getAltText());
assertEquals("my-image.jpg", asset.getAltText());
assertNull(asset.getDescription());
assertEquals(SAMPLE_REFERENCE, asset.getPath());
assertEquals(ValueMap.EMPTY, asset.getProperties());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
package io.wcm.handler.mediasource.ngdm.impl.metadata;

import static com.day.cq.dam.api.DamConstants.DC_DESCRIPTION;
import static com.day.cq.dam.api.DamConstants.DC_TITLE;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
Expand Down Expand Up @@ -118,8 +120,8 @@ void testValidToken() {
assertEquals(1500, dimension.getWidth());
assertEquals(900, dimension.getHeight());
assertEquals("image/jpeg", metadata.getMimeType());
assertEquals("Test Image", metadata.getProperties().get("dc:title", String.class));
assertEquals("Test Description", metadata.getProperties().get("dc:description", String.class));
assertEquals("Test Image", metadata.getProperties().get(DC_TITLE, String.class));
assertEquals("Test Description", metadata.getProperties().get(DC_DESCRIPTION, String.class));
}

@Test
Expand All @@ -139,8 +141,8 @@ void testInvalidToken_FallbackNoAuth() {
assertEquals(1200, dimension.getWidth());
assertEquals(800, dimension.getHeight());
assertEquals("image/jpeg", metadata.getMimeType());
assertNull(metadata.getProperties().get("dc:title", String.class));
assertNull(metadata.getProperties().get("dc:description", String.class));
assertNull(metadata.getProperties().get(DC_TITLE, String.class));
assertNull(metadata.getProperties().get(DC_DESCRIPTION, String.class));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package io.wcm.handler.mediasource.ngdm.impl.metadata;

import static com.day.cq.dam.api.DamConstants.ASSET_STATUS_APPROVED;
import static com.day.cq.dam.api.DamConstants.DC_DESCRIPTION;
import static com.day.cq.dam.api.DamConstants.DC_TITLE;
import static io.wcm.handler.mediasource.ngdm.impl.metadata.MetadataSample.METADATA_JSON_IMAGE;
import static io.wcm.handler.mediasource.ngdm.impl.metadata.MetadataSample.METADATA_JSON_IMAGE_FULL;
import static io.wcm.handler.mediasource.ngdm.impl.metadata.MetadataSample.METADATA_JSON_PDF;
Expand Down Expand Up @@ -71,8 +73,8 @@ void testSampleJson_Image_Full() throws JsonProcessingException {
assertEquals(1500, dimension.getWidth());
assertEquals(900, dimension.getHeight());
assertEquals(ASSET_STATUS_APPROVED, metadata.getAssetStatus());
assertEquals("Test Image", metadata.getProperties().get("dc:title", String.class));
assertEquals("Test Description", metadata.getProperties().get("dc:description", String.class));
assertEquals("Test Image", metadata.getProperties().get(DC_TITLE, String.class));
assertEquals("Test Description", metadata.getProperties().get(DC_DESCRIPTION, String.class));
assertEquals(0, metadata.getSmartCrops().size());
assertTrue(metadata.isValid());
}
Expand Down

0 comments on commit 2da8dcc

Please sign in to comment.