From 3e461d794912c4e7450f9e4117ac0dd584a61247 Mon Sep 17 00:00:00 2001 From: Stefan Seifert Date: Mon, 25 Nov 2024 10:56:10 +0100 Subject: [PATCH] Dynamic Media with OpenAPI: Respect Image Dimension from SVG asset metadata (#72) --- changes.xml | 3 ++ .../ngdm/NextGenDynamicMediaRendition.java | 8 ++++- ...amicMedia_RemoteAssetWithMetadataTest.java | 29 +++++++++++++++++-- .../ngdm/impl/metadata/MetadataSample.java | 15 ++++++++++ 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/changes.xml b/changes.xml index 99d5903f..0c13fd2c 100644 --- a/changes.xml +++ b/changes.xml @@ -27,6 +27,9 @@ Dynamic Media with Open API: Use remote metadata call to validate and get metadata for local assets as well. + + Dynamic Media with OpenAPI: Respect Image Dimension from SVG asset metadata. + diff --git a/src/main/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMediaRendition.java b/src/main/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMediaRendition.java index 18a16d0b..10ae223c 100644 --- a/src/main/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMediaRendition.java +++ b/src/main/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMediaRendition.java @@ -96,7 +96,13 @@ final class NextGenDynamicMediaRendition implements Rendition { this.fileExtension = FilenameUtils.getExtension(reference.getFileName()); } - if (isVectorImage() || !isImage() || mediaArgs.isDownload()) { + if (!isImage() || mediaArgs.isDownload()) { + // deliver as binary + this.url = buildBinaryUrl(); + } + else if (isVectorImage()) { + // calculate width/height for rendition metadata + calculateWidthHeight(); // deliver as binary this.url = buildBinaryUrl(); } diff --git a/src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMedia_RemoteAssetWithMetadataTest.java b/src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMedia_RemoteAssetWithMetadataTest.java index a1076417..46aa3cd1 100644 --- a/src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMedia_RemoteAssetWithMetadataTest.java +++ b/src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMedia_RemoteAssetWithMetadataTest.java @@ -27,6 +27,7 @@ import static io.wcm.handler.mediasource.ngdm.impl.NextGenDynamicMediaReferenceSample.SAMPLE_REFERENCE; 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_PDF; +import static io.wcm.handler.mediasource.ngdm.impl.metadata.MetadataSample.METADATA_JSON_SVG; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -273,8 +274,32 @@ void testPDFDownload() { Rendition rendition = media.getRendition(); assertNotNull(rendition); assertEquals(ContentType.PDF, rendition.getMimeType()); - assertEquals( - "https://" + nextGenDynamicMediaConfig.getRepositoryId() + "/adobe/assets/" + SAMPLE_ASSET_ID + "/original/as/myfile.pdf", + assertEquals("https://" + nextGenDynamicMediaConfig.getRepositoryId() + "/adobe/assets/" + SAMPLE_ASSET_ID + "/original/as/myfile.pdf", + rendition.getUrl()); + } + + @Test + @SuppressWarnings("null") + void testSVG() { + stubFor(get("/adobe/assets/" + SAMPLE_ASSET_ID + "/metadata") + .willReturn(aResponse() + .withStatus(HttpStatus.SC_OK) + .withHeader("Content-Type", ContentType.JSON) + .withBody(METADATA_JSON_SVG))); + + Resource downloadResource = context.create().resource(context.currentPage(), "image", + MediaNameConstants.PN_MEDIA_REF, "/" + SAMPLE_ASSET_ID + "/myfile.svg"); + + Media media = mediaHandler.get(downloadResource) + .build(); + assertTrue(media.isValid()); + + Rendition rendition = media.getRendition(); + assertNotNull(rendition); + assertEquals(ContentType.SVG, rendition.getMimeType()); + assertEquals(900, rendition.getWidth()); + assertEquals(600, rendition.getHeight()); + assertEquals("https://" + nextGenDynamicMediaConfig.getRepositoryId() + "/adobe/assets/" + SAMPLE_ASSET_ID + "/original/as/myfile.svg", rendition.getUrl()); } diff --git a/src/test/java/io/wcm/handler/mediasource/ngdm/impl/metadata/MetadataSample.java b/src/test/java/io/wcm/handler/mediasource/ngdm/impl/metadata/MetadataSample.java index 40f291ef..422479c8 100644 --- a/src/test/java/io/wcm/handler/mediasource/ngdm/impl/metadata/MetadataSample.java +++ b/src/test/java/io/wcm/handler/mediasource/ngdm/impl/metadata/MetadataSample.java @@ -73,6 +73,21 @@ public final class MetadataSample { + " }" + "}"; + public static final String METADATA_JSON_SVG = "{" + + " \"assetId\": \"" + SAMPLE_ASSET_ID + "\"," + + " \"repositoryMetadata\": {" + + " \"repo:name\": \"test.svg\"," + + " \"dc:format\": \"image/svg+xml\"" + + " }," + + " \"assetMetadata\": {" + + " \"dam:assetStatus\": \"approved\"," + + " \"dc:description\": \"Test Description\"," + + " \"dc:title\": \"Test Image\"," + + " \"tiff:ImageLength\": 600," + + " \"tiff:ImageWidth\": 900" + + " }" + + "}"; + public static final String METADATA_JSON_PDF = "{" + " \"assetId\": \"" + SAMPLE_ASSET_ID + "\"," + " \"repositoryMetadata\": {"