Skip to content

Commit

Permalink
Use remote metadata call to validate and get metadata for local asset…
Browse files Browse the repository at this point in the history
…s as well (#71)
  • Loading branch information
stefanseifert authored Nov 18, 2024
1 parent 50c8c29 commit e8bbe74
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 408 deletions.
6 changes: 6 additions & 0 deletions changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
xsi:schemaLocation="http://maven.apache.org/changes/1.0.0 http://maven.apache.org/plugins/maven-changes-plugin/xsd/changes-1.0.0.xsd">
<body>

<release version="2.2.4" date="not released">
<action type="update" dev="sseifert" issue="71">
Dynamic Media with Open API: Use remote metadata call to validate and get metadata for local assets as well.
</action>
</release>

<release version="2.2.2" date="2024-09-16">
<action type="update" dev="sseifert" issue="67">
Dynamic Media with Open API: Correctly calculate rendition width/height based on requested dimension or original dimension.
Expand Down
13 changes: 7 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
<dependency>
<groupId>io.wcm</groupId>
<artifactId>io.wcm.testing.aem-mock.junit5</artifactId>
<version>5.6.0</version>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -204,18 +204,19 @@
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.8.0</version>
<version>2.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path-assert</artifactId>
<version>2.8.0</version>
<version>2.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock</artifactId>
<!-- Stich with version 3.4.0 because recent versions require a newer Jackson version -->
<version>3.4.0</version>
<scope>test</scope>
</dependency>
Expand All @@ -224,20 +225,20 @@
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-tiff</artifactId>
<version>3.10.1</version>
<version>3.12.0</version>
<scope>test</scope>
</dependency>
<!-- Java ImageIO Support for SVG -->
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-batik</artifactId>
<version>3.10.1</version>
<version>3.12.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-transcoder</artifactId>
<version>1.17</version>
<version>1.18</version>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.day.cq.dam.api.Asset;
import com.day.cq.wcm.api.WCMMode;
import com.day.cq.wcm.api.components.ComponentContext;
import com.day.cq.wcm.api.components.EditConfig;
Expand Down Expand Up @@ -136,11 +135,7 @@ private boolean isDamAssetReference(@Nullable String mediaRef) {

// If enabled: Fetch asset metadata to validate existence and get original dimensions
NextGenDynamicMediaMetadata metadata = null;
Asset localAsset = reference.getAsset();
if (localAsset != null) {
metadata = getMetadataFromAsset(localAsset);
}
else if (metadataService != null && metadataService.isEnabled()) {
if (metadataService != null && metadataService.isEnabled()) {
metadata = metadataService.fetchMetadata(reference);
if (metadata == null) {
media.setMediaInvalidReason(MediaInvalidReason.MEDIA_REFERENCE_INVALID);
Expand Down Expand Up @@ -194,14 +189,6 @@ else if (nextGenDynamicMediaConfig.isEnabledLocalAssets() && isDamAssetReference
return null;
}

private @Nullable NextGenDynamicMediaMetadata getMetadataFromAsset(@NotNull Asset asset) {
NextGenDynamicMediaMetadata metadata = NextGenDynamicMediaMetadata.fromAsset(asset);
if (metadata.isValid()) {
return metadata;
}
return null;
}

@Override
public void enableMediaDrop(@NotNull HtmlElement element, @NotNull MediaRequest mediaRequest) {
if (wcmMode == WCMMode.DISABLED || wcmMode == null) {
Expand Down
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 @@ -19,36 +19,21 @@
*/
package io.wcm.handler.mediasource.ngdm.impl.metadata;

import static com.day.cq.commons.jcr.JcrConstants.JCR_CONTENT;
import static com.day.cq.dam.api.DamConstants.ASSET_STATUS_PROPERTY;
import static com.day.cq.dam.api.DamConstants.RENDITIONS_FOLDER;
import static io.wcm.handler.mediasource.dam.impl.dynamicmedia.SmartCrop.PN_LEFT;
import static io.wcm.handler.mediasource.dam.impl.dynamicmedia.SmartCrop.PN_NORMALIZED_HEIGHT;
import static io.wcm.handler.mediasource.dam.impl.dynamicmedia.SmartCrop.PN_NORMALIZED_WIDTH;
import static io.wcm.handler.mediasource.dam.impl.dynamicmedia.SmartCrop.PN_TOP;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import com.day.cq.dam.api.Asset;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.json.JsonMapper;

import io.wcm.handler.media.Dimension;
import io.wcm.handler.mediasource.dam.AssetRendition;
import io.wcm.handler.mediasource.ngdm.impl.metadata.MetadataResponse.AssetMetadata;
import io.wcm.handler.mediasource.ngdm.impl.metadata.MetadataResponse.RepositoryMetadata;
import io.wcm.wcm.commons.contenttype.ContentType;
Expand Down Expand Up @@ -163,42 +148,6 @@ public String toString() {
return null;
}

/**
* Gets metadata from DAM asset.
* @param asset Asset
* @return Metadata object
*/
@SuppressWarnings("null")
public static @NotNull NextGenDynamicMediaMetadata fromAsset(@NotNull Asset asset) {
String mimeType = asset.getMimeType();

Dimension dimension = AssetRendition.getDimension(asset.getOriginal());
String assetStatus = asset.getMetadataValueFromJcr(ASSET_STATUS_PROPERTY);
List<SmartCrop> smartCrops = null;

if (dimension != null) {
smartCrops = getRenditionResources(asset)
.filter(rendition -> rendition.isResourceType(RT_RENDITION_SMARTCROP))
.map(rendition -> Map.entry(rendition.getName(), renditionToSmartCropDefinition(rendition)))
.filter(entry -> isSmartCropDefinitionValid(entry.getKey(), entry.getValue()))
.map(entry -> new SmartCrop(entry.getKey(), entry.getValue(), dimension))
.collect(Collectors.toList());
}

return new NextGenDynamicMediaMetadata(mimeType, dimension, assetStatus, smartCrops);
}

private static Stream<Resource> getRenditionResources(@NotNull Asset asset) {
Resource assetResource = asset.adaptTo(Resource.class);
if (assetResource != null) {
Resource renditionsFolder = assetResource.getChild(JCR_CONTENT + "/" + RENDITIONS_FOLDER);
if (renditionsFolder != null) {
return StreamSupport.stream(renditionsFolder.getChildren().spliterator(), false);
}
}
return Stream.empty();
}

private static boolean isSmartCropDefinitionValid(@NotNull String name, @NotNull MetadataResponse.SmartCrop smartCop) {
return StringUtils.isNotBlank(name)
&& smartCop.normalizedWidth > 0
Expand All @@ -207,17 +156,4 @@ private static boolean isSmartCropDefinitionValid(@NotNull String name, @NotNull
&& smartCop.top >= 0;
}

private static @NotNull MetadataResponse.SmartCrop renditionToSmartCropDefinition(Resource rendition) {
MetadataResponse.SmartCrop result = new MetadataResponse.SmartCrop();
Resource content = rendition.getChild(JCR_CONTENT);
if (content != null) {
ValueMap props = content.getValueMap();
result.left = props.get(PN_LEFT, 0d);
result.top = props.get(PN_TOP, 0d);
result.normalizedWidth = props.get(PN_NORMALIZED_WIDTH, 0d);
result.normalizedHeight = props.get(PN_NORMALIZED_HEIGHT, 0d);
}
return result;
}

}
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
Loading

0 comments on commit e8bbe74

Please sign in to comment.