Skip to content

Commit

Permalink
Respect Asset Compute Rendition Metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Aug 27, 2024
1 parent 393bf76 commit a315dbc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
38 changes: 31 additions & 7 deletions src/main/java/io/wcm/handler/mediasource/dam/AssetRendition.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static com.day.cq.commons.jcr.JcrConstants.JCR_CONTENT;
import static com.day.cq.dam.api.DamConstants.EXIF_PIXELXDIMENSION;
import static com.day.cq.dam.api.DamConstants.EXIF_PIXELYDIMENSION;
import static com.day.cq.dam.api.DamConstants.METADATA_FOLDER;
import static com.day.cq.dam.api.DamConstants.ORIGINAL_FILE;
import static com.day.cq.dam.api.DamConstants.TIFF_IMAGELENGTH;
import static com.day.cq.dam.api.DamConstants.TIFF_IMAGEWIDTH;
Expand Down Expand Up @@ -106,9 +107,14 @@ private AssetRendition() {

// dimensions for non-original renditions only supported for image binaries
if (MediaFileType.isImage(fileExtension)) {
if (dimension == null) {
// check if rendition metadata is present in <rendition>/jcr:content/metadata provided by AEMaaCS asset compute
dimension = getDimensionFromAemRenditionMetadata(rendition);
}

if (dimension == null) {
// otherwise get from rendition metadata written by {@link DamRenditionMetadataService}
dimension = getDimensionFromRenditionMetadata(rendition);
dimension = getDimensionFromMediaHandlerRenditionMetadata(rendition);
}

// fallback: if width/height could not be read from either asset or rendition metadata load the image
Expand All @@ -131,7 +137,7 @@ private AssetRendition() {
// asset may have stored dimension in different property names
long width = getAssetMetadataValueAsLong(asset, TIFF_IMAGEWIDTH, EXIF_PIXELXDIMENSION);
long height = getAssetMetadataValueAsLong(asset, TIFF_IMAGELENGTH, EXIF_PIXELYDIMENSION);
return toDimension(width, height);
return toValidDimension(width, height);
}

private static long getAssetMetadataValueAsLong(Asset asset, String... propertyNames) {
Expand All @@ -150,15 +156,33 @@ private static long getAssetMetadataValueAsLong(Asset asset, String... propertyN
* @return Dimension or null
*/
@SuppressWarnings("java:S1075") // not a file path
private static @Nullable Dimension getDimensionFromRenditionMetadata(@NotNull Rendition rendition) {
private static @Nullable Dimension getDimensionFromMediaHandlerRenditionMetadata(@NotNull Rendition rendition) {
Asset asset = rendition.getAsset();
String metadataPath = JCR_CONTENT + "/" + NN_RENDITIONS_METADATA + "/" + rendition.getName();
Resource metadataResource = AdaptTo.notNull(asset, Resource.class).getChild(metadataPath);
if (metadataResource != null) {
ValueMap props = metadataResource.getValueMap();
long width = props.get(PN_IMAGE_WIDTH, 0L);
long height = props.get(PN_IMAGE_HEIGHT, 0L);
return toDimension(width, height);
return toValidDimension(width, height);
}
return null;
}

/**
* Asset Compute from AEMaaCS writes rendition metadata including width/height to jcr:content/metadata of the
* rendition resource - try to read it from there (it may be missing for not fully processed assets, or in local
* AEMaaCS SDK or AEM 6.5 instances).
* @param rendition Rendition
* @return Dimension or null
*/
private static @Nullable Dimension getDimensionFromAemRenditionMetadata(@NotNull Rendition rendition) {
Resource metadataResource = rendition.getChild(JCR_CONTENT + "/" + METADATA_FOLDER);
if (metadataResource != null) {
ValueMap props = metadataResource.getValueMap();
long width = props.get(TIFF_IMAGEWIDTH, 0L);
long height = props.get(TIFF_IMAGELENGTH, 0L);
return toValidDimension(width, height);
}
return null;
}
Expand All @@ -178,7 +202,7 @@ private static long getAssetMetadataValueAsLong(Asset asset, String... propertyN
Layer layer = new Layer(is);
long width = layer.getWidth();
long height = layer.getHeight();
Dimension dimension = toDimension(width, height);
Dimension dimension = toValidDimension(width, height);
if (!suppressLogWarningNoRenditionsMetadata) {
log.warn("Unable to detect rendition metadata for {}, "
+ "fallback to inefficient detection by loading image into in memory (detected dimension={}). "
Expand All @@ -198,12 +222,12 @@ private static long getAssetMetadataValueAsLong(Asset asset, String... propertyN
}

/**
* Convert width/height to dimension
* Convert width/height to dimension.
* @param width Width
* @param height Height
* @return Dimension or null if width or height are not valid
*/
private static @Nullable Dimension toDimension(long width, long height) {
private static @Nullable Dimension toValidDimension(long width, long height) {
if (width > 0L && height > 0L) {
return new Dimension(width, height);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
import static com.day.cq.commons.jcr.JcrConstants.JCR_MIMETYPE;
import static com.day.cq.commons.jcr.JcrConstants.JCR_PRIMARYTYPE;
import static com.day.cq.commons.jcr.JcrConstants.NT_UNSTRUCTURED;
import static com.day.cq.dam.api.DamConstants.METADATA_FOLDER;
import static com.day.cq.dam.api.DamConstants.ORIGINAL_FILE;
import static com.day.cq.dam.api.DamConstants.RENDITIONS_FOLDER;
import static com.day.cq.dam.api.DamConstants.TIFF_IMAGELENGTH;
import static com.day.cq.dam.api.DamConstants.TIFF_IMAGEWIDTH;
import static io.wcm.handler.mediasource.dam.impl.metadata.RenditionMetadataNameConstants.NN_RENDITIONS_METADATA;
import static io.wcm.handler.mediasource.dam.impl.metadata.RenditionMetadataNameConstants.PN_IMAGE_HEIGHT;
import static io.wcm.handler.mediasource.dam.impl.metadata.RenditionMetadataNameConstants.PN_IMAGE_WIDTH;
Expand All @@ -47,6 +50,7 @@
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
Expand Down Expand Up @@ -85,7 +89,10 @@ public RenditionMetadataGenerator(ResourceResolver resourceResolver, AssetStore
* Generate/validate rendition metadata of all renditions of this asset.
* @param asset Asset
*/
@SuppressWarnings("java:S1075") // not a file path
@SuppressWarnings({
"java:S1075", // not a file path
"java:S3776" // complexity
})
public void processAllRenditions(Asset asset) {
Set<String> existingRenditionNames = new HashSet<>();
List<String> renditionPaths = new ArrayList<>();
Expand All @@ -96,6 +103,10 @@ public void processAllRenditions(Asset asset) {

// get existing rendition names and paths
for (Rendition rendition : asset.getRenditions()) {
// skip renditions where AEMaaCS asset compute already provided metadata
if (hasAemRenditionMetadata(rendition.getPath())) {
continue;
}
existingRenditionNames.add(rendition.getName());
renditionPaths.add(rendition.getPath());
}
Expand Down Expand Up @@ -304,4 +315,22 @@ private String getRenditionMetadataResourcePath(String renditionPath) {
return assetPath + "/" + JCR_CONTENT + "/" + NN_RENDITIONS_METADATA + "/" + renditionName;
}

/**
* Checks if rendition metadata provided by AEMaaCS asset compute already exists at rendition/jcr:content/metadata.
* @param renditionPath Rendition path
* @return true if metadata with a valid dimension exists
*/
@SuppressWarnings("java:S1075") // not a file path
private boolean hasAemRenditionMetadata(String renditionPath) {
String metadatPath = renditionPath + "/" + JCR_CONTENT + "/" + METADATA_FOLDER;
Resource metadataResource = resourceResolver.getResource(metadatPath);
if (metadataResource != null) {
ValueMap props = metadataResource.getValueMap();
long width = props.get(TIFF_IMAGEWIDTH, 0L);
long height = props.get(TIFF_IMAGELENGTH, 0L);
return width > 0 && height > 0;
}
return false;
}

}

0 comments on commit a315dbc

Please sign in to comment.