From 6d3fd71837e62dd40d29d7a16c46d4ce7f6395ed Mon Sep 17 00:00:00 2001 From: eschleb Date: Tue, 7 Nov 2023 12:02:08 +0100 Subject: [PATCH] Refactor image width/height ratio processing - Defer height calculation to image generation instead of bundle processing - Make ratio optional - Add option to overwrite ratio in imageSize --- README.md | 73 +++++++++++++++++-- .../generator/FlexibleImageUriParser.java | 7 +- .../FlexibleParameterCachingStrategy.java | 2 +- .../generator/ImageOperationProvider.java | 26 ++++++- .../flexible/model/DamImageModelFactory.java | 4 +- .../flexible/model/FlexibleParameter.java | 48 +++++------- .../imaging/flexible/model/ImageModel.java | 20 +---- .../imaging/flexible/model/bundle/Bundle.java | 30 +++++--- .../model/bundle/BundleProcessor.java | 31 ++++++-- .../model/bundle/ProcessedBundle.java | 25 ++++--- .../flexible/model/bundle/RatioParser.java | 19 +++++ .../FlexibleImageUriFactoryTest.java | 4 +- .../generator/FlexibleImageUriParserTest.java | 8 +- .../FlexibleParameterCachingStrategyTest.java | 12 +-- .../model/bundle/BundleProcessorTest.java | 24 +++--- .../model/bundle/BundlesParserTest.java | 40 ++++++---- .../flexible/model/bundle/_bundles/16x9.json | 2 +- .../flexible/model/bundle/_bundles/1x1.json | 2 +- .../flexible/model/bundle/_bundles/stage.json | 31 ++++++++ 19 files changed, 279 insertions(+), 129 deletions(-) create mode 100644 src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/RatioParser.java create mode 100644 src/test/resources/com/merkle/oss/magnolia/imaging/flexible/model/bundle/_bundles/stage.json diff --git a/README.md b/README.md index 5b8f2d4..f9ef671 100644 --- a/README.md +++ b/README.md @@ -18,19 +18,19 @@ Magnolia image generator that uses json definitions to generate images. { "bundle": "16_7-stage", "description": "ratio 16:7 bundle for stage image, VP S and M: 12cols wide, VP L: 12cols + container padding (breaking out of grid)", - "ratio": 2.285714285714286, + "ratio": "16:7", "imageSizes": [ { "width": 720, - "id": "720w" + "media": "720w" }, { "width": 1280, - "id": "1280w" + "media": "1280w" }, { "width": 2880, - "id": "2880w" + "media": "2880w" } ], "customRenditions": [ @@ -43,7 +43,68 @@ Magnolia image generator that uses json definitions to generate images. ] ``` - +## Bundle schema +```json +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "bundle", + "type": "array", + "items": { + "type": "object", + "properties": { + "bundle": { + "type": "string", + "description": "bundle name" + }, + "ratio": { + "type": "string", + "description": "e.g. '16:9'. If not specified here or in imageSize, the original image ratio will be preserved" + }, + "imageSizes": { + "type": "array", + "items": { + "type": "object", + "properties": { + "width": { + "type": "integer" + }, + "media": { + "type": "string", + "description": "The media size for the image (e.g. '2880w')" + }, + "ratio": { + "type": "string", + "description": "e.g. '16:9'. If not specified here or in the bundle, the original image ratio will be preserved" + } + }, + "required": [ "width", "media" ] + } + }, + "customRenditions": { + "type": "array", + "items": { + "type": "object", + "properties": { + "width": { + "type": "integer" + }, + "id": { + "type": "string", + "description": "identifier" + }, + "ratio": { + "type": "string", + "description": "e.g. '16:9'" + } + }, + "required": [ "width", "id" ] + } + } + }, + "required": [ "bundle", "imageSizes", "customRenditions"] + } +} +``` ## Custom image operations It is possible to implement custom image operations by extending and binding your own instances: @@ -78,7 +139,7 @@ public class CustomDynamicImageParameter extends DynamicImageParameter { public Map toMap() { return Stream.concat( super.toMap().entrySet().stream(), - Map.of(Factory.TRIGGER_CUSTOM_OPERATION_PARAM, isTriggerCustomOperation()) + Map.of(Factory.TRIGGER_CUSTOM_OPERATION_PARAM, isTriggerCustomOperation()).entrySet().stream() ).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); } diff --git a/src/main/java/com/merkle/oss/magnolia/imaging/flexible/generator/FlexibleImageUriParser.java b/src/main/java/com/merkle/oss/magnolia/imaging/flexible/generator/FlexibleImageUriParser.java index 12030e7..d5d8d34 100644 --- a/src/main/java/com/merkle/oss/magnolia/imaging/flexible/generator/FlexibleImageUriParser.java +++ b/src/main/java/com/merkle/oss/magnolia/imaging/flexible/generator/FlexibleImageUriParser.java @@ -5,6 +5,7 @@ import info.magnolia.dam.api.Asset; import info.magnolia.dam.templating.functions.DamTemplatingFunctions; +import javax.annotation.Nullable; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import java.util.Objects; @@ -42,7 +43,7 @@ public Optional parse(final HttpServletRequest request) { flexibleParameterFactory.create(asset, key -> getParameter(uri, key)) ) .filter(parameter -> - isSizeValid(parameter.getWidth(), parameter.getHeight()) + isSizeValid(parameter.getWidth(), parameter.getRatio().orElse(null)) ); } @@ -62,7 +63,7 @@ private Optional getAsset(final String uri) { .map(damTemplatingFunctions::getAsset); } - private boolean isSizeValid(final int width, final int height) { + private boolean isSizeValid(final int width, @Nullable final String ratio) { return bundlesProvider.get().stream() .flatMap(bundle -> Stream.concat( @@ -71,7 +72,7 @@ private boolean isSizeValid(final int width, final int height) { ) ) .anyMatch(size -> - Objects.equals(size.getWidth(), width) && Objects.equals(size.getHeight(), height) + Objects.equals(size.getWidth(), width) && Objects.equals(size.getRatio().orElse(null), ratio) ); } } diff --git a/src/main/java/com/merkle/oss/magnolia/imaging/flexible/generator/FlexibleParameterCachingStrategy.java b/src/main/java/com/merkle/oss/magnolia/imaging/flexible/generator/FlexibleParameterCachingStrategy.java index d37b89b..2a6c2c3 100755 --- a/src/main/java/com/merkle/oss/magnolia/imaging/flexible/generator/FlexibleParameterCachingStrategy.java +++ b/src/main/java/com/merkle/oss/magnolia/imaging/flexible/generator/FlexibleParameterCachingStrategy.java @@ -22,7 +22,7 @@ public String getCachePath( return "/" + generator.getName() + "/" + Text.escapeIllegalJcrChars(parameter.getItemKey().asString()) + "/" + parameter.getWidth() + "/" - + parameter.getHeight() + "/" + + parameter.getRatio().map(Text::escapeIllegalJcrChars).orElse("keepRatio") + "/" + parameter.getDynamicImageParameter().hashCode(); } diff --git a/src/main/java/com/merkle/oss/magnolia/imaging/flexible/generator/ImageOperationProvider.java b/src/main/java/com/merkle/oss/magnolia/imaging/flexible/generator/ImageOperationProvider.java index 05398da..a8feb8a 100644 --- a/src/main/java/com/merkle/oss/magnolia/imaging/flexible/generator/ImageOperationProvider.java +++ b/src/main/java/com/merkle/oss/magnolia/imaging/flexible/generator/ImageOperationProvider.java @@ -3,13 +3,25 @@ import com.merkle.oss.magnolia.imaging.flexible.generator.operation.FromFlexibleParameter; import com.merkle.oss.magnolia.imaging.flexible.model.DynamicImageParameter; import com.merkle.oss.magnolia.imaging.flexible.model.FlexibleParameter; +import com.merkle.oss.magnolia.imaging.flexible.model.bundle.RatioParser; import info.magnolia.imaging.ParameterProvider; import info.magnolia.imaging.operations.ImageOperationChain; import info.magnolia.imaging.operations.cropresize.AutoCropAndResize; import info.magnolia.imaging.operations.cropresize.BoundedResize; import info.magnolia.imaging.operations.cropresize.resizers.MultiStepResizer; +import javax.inject.Inject; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.Optional; + public class ImageOperationProvider { + private final RatioParser ratioParser; + + @Inject + public ImageOperationProvider(final RatioParser ratioParser) { + this.ratioParser = ratioParser; + } public ImageOperationChain> get(final FlexibleParameter parameter) { final ImageOperationChain> chain = new ImageOperationChain<>(); @@ -19,15 +31,25 @@ public ImageOperationChain> get(final Flexi final AutoCropAndResize resize = new AutoCropAndResize(); resize.setResizer(new MultiStepResizer()); resize.setTargetWidth(parameter.getWidth()); - resize.setTargetHeight(parameter.getHeight()); + calculateHeight(parameter).ifPresent(resize::setTargetHeight); chain.addOperation(resize); } else { final BoundedResize resize = new BoundedResize(); resize.setResizer(new MultiStepResizer()); resize.setMaxWidth(parameter.getWidth()); - resize.setMaxHeight(parameter.getHeight()); + resize.setMaxHeight(calculateHeight(parameter).orElse(Integer.MAX_VALUE)); chain.addOperation(resize); } return chain; } + + protected Optional calculateHeight(final FlexibleParameter parameter) { + return parameter.getRatio().flatMap(ratioParser::parse).map(ratio -> + calculateHeight(parameter.getWidth(), ratio) + ); + } + + private int calculateHeight(final int width, final double ratio) { + return BigDecimal.valueOf(width / ratio).setScale(0, RoundingMode.UP).intValue(); + } } diff --git a/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/DamImageModelFactory.java b/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/DamImageModelFactory.java index acbe83e..46964ed 100644 --- a/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/DamImageModelFactory.java +++ b/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/DamImageModelFactory.java @@ -73,7 +73,7 @@ private List getSrcSet(final ProcessedBundle bundle, final return bundle.getImageSizes() .stream() .map(size -> - new ImageModel.Rendition(size.getId(), size.getWidth(), size.getHeight(), getUrl(size, asset, dynamicImageParameter)) + new ImageModel.Rendition(size.getId(), getUrl(size, asset, dynamicImageParameter)) ) .collect(Collectors.toList()); } @@ -92,7 +92,7 @@ private Map getCustomRenditions(final ProcessedBundle bundle, fi } private String getUrl(final ProcessedBundle.ImageSize size, final Asset asset, @Nullable final DynamicImageParameter dynamicImageParameter) { - final FlexibleParameter parameter = new FlexibleParameter(dynamicImageParameter, size.getWidth(), size.getHeight(), asset); + final FlexibleParameter parameter = new FlexibleParameter(dynamicImageParameter, size.getRatio().orElse(null), size.getWidth(), asset); return flexibleImageUriFactory.create(parameter).toString(); } diff --git a/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/FlexibleParameter.java b/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/FlexibleParameter.java index d5e0be8..5f275d5 100755 --- a/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/FlexibleParameter.java +++ b/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/FlexibleParameter.java @@ -16,18 +16,19 @@ public class FlexibleParameter extends AssetDecorator { @Nullable private final DynamicImageParameter dynamicImageParameter; + @Nullable + private final String ratio; private final int width; - private final int height; public FlexibleParameter( @Nullable final DynamicImageParameter dynamicImageParameter, + @Nullable final String ratio, final int width, - final int height, final Asset asset ) { super(asset); + this.ratio = ratio; this.width = width; - this.height = height; this.dynamicImageParameter = dynamicImageParameter; } @@ -39,18 +40,18 @@ public int getWidth() { return width; } - public int getHeight() { - return height; + public Optional getRatio() { + return Optional.ofNullable(ratio); } public Map toMap() { - return Stream.concat( + return Stream.of( getDynamicImageParameter().stream().map(DynamicImageParameter::toMap).map(Map::entrySet).flatMap(Collection::stream), + getRatio().stream().map(ratio -> Map.entry(Factory.RATIO_PARAM, ratio)), Map.of( - Factory.WIDTH_PARAM, String.valueOf(getWidth()), - Factory.HEIGHT_PARAM, String.valueOf(getHeight()) + Factory.WIDTH_PARAM, String.valueOf(getWidth()) ).entrySet().stream() - ).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (dynamicImageParam, flexibleParam) -> flexibleParam)); + ).flatMap(Function.identity()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (dynamicImageParam, flexibleParam) -> flexibleParam)); } @Override @@ -58,26 +59,17 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; FlexibleParameter that = (FlexibleParameter) o; - return width == that.width && height == that.height && Objects.equals(dynamicImageParameter, that.dynamicImageParameter); + return width == that.width && Objects.equals(dynamicImageParameter, that.dynamicImageParameter) && Objects.equals(ratio, that.ratio); } @Override public int hashCode() { - return Objects.hash(dynamicImageParameter, width, height); - } - - @Override - public String toString() { - return "FlexibleParameter{" + - "imageParameter=" + dynamicImageParameter + - ", width=" + width + - ", height=" + height + - '}'; + return Objects.hash(dynamicImageParameter, ratio, width); } public static class Factory { public static final String WIDTH_PARAM = "width"; - public static final String HEIGHT_PARAM = "height"; + public static final String RATIO_PARAM = "ratio"; private final DynamicImageParameter.Factory dynamicImageParameterFactory; @@ -87,14 +79,12 @@ public Factory(final DynamicImageParameter.Factory dynamicImageParameterFactory) } public Optional create(final Asset asset, final Function> parameterProvider) { - return parameterProvider.apply(WIDTH_PARAM).map(Integer::parseInt).flatMap(width -> - parameterProvider.apply(HEIGHT_PARAM).map(Integer::parseInt).map(height -> - new FlexibleParameter( - dynamicImageParameterFactory.create(parameterProvider).orElse(null), - width, - height, - asset - ) + return parameterProvider.apply(WIDTH_PARAM).map(Integer::parseInt).map(width -> + new FlexibleParameter( + dynamicImageParameterFactory.create(parameterProvider).orElse(null), + parameterProvider.apply(RATIO_PARAM).orElse(null), + width, + asset ) ); } diff --git a/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/ImageModel.java b/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/ImageModel.java index a2cb88a..39fd5d1 100644 --- a/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/ImageModel.java +++ b/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/ImageModel.java @@ -86,19 +86,13 @@ public String toString() { public static class Rendition { private final String media; - private final Integer width; - private final Integer height; private final String src; public Rendition( final String media, - final Integer width, - final Integer height, final String src ) { this.media = media; - this.width = width; - this.height = height; this.src = src; } @@ -106,14 +100,6 @@ public String getMedia() { return media; } - public Integer getWidth() { - return width; - } - - public Integer getHeight() { - return height; - } - public String getSrc() { return src; } @@ -123,20 +109,18 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Rendition rendition = (Rendition) o; - return Objects.equals(media, rendition.media) && Objects.equals(width, rendition.width) && Objects.equals(height, rendition.height) && Objects.equals(src, rendition.src); + return Objects.equals(media, rendition.media) && Objects.equals(src, rendition.src); } @Override public int hashCode() { - return Objects.hash(media, width, height, src); + return Objects.hash(media, src); } @Override public String toString() { return "Rendition{" + "media='" + media + '\'' + - ", width=" + width + - ", height=" + height + ", src='" + src + '\'' + '}'; } diff --git a/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/Bundle.java b/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/Bundle.java index db162b4..c4143bb 100755 --- a/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/Bundle.java +++ b/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/Bundle.java @@ -2,19 +2,22 @@ import com.google.gson.annotations.SerializedName; +import javax.annotation.Nullable; import java.util.List; import java.util.Objects; +import java.util.Optional; class Bundle { @SerializedName("bundle") private final String name; - private final double ratio; + @Nullable + private final String ratio; private final List imageSizes; private final List customRenditions; Bundle( final String name, - final double ratio, + @Nullable final String ratio, final List imageSizes, final List customRenditions ) { @@ -28,8 +31,8 @@ public String getName() { return name; } - public double getRatio() { - return ratio; + public Optional getRatio() { + return Optional.ofNullable(ratio); } public List getImageSizes() { @@ -45,7 +48,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Bundle bundle = (Bundle) o; - return Double.compare(ratio, bundle.ratio) == 0 && Objects.equals(name, bundle.name) && Objects.equals(imageSizes, bundle.imageSizes) && Objects.equals(customRenditions, bundle.customRenditions); + return Objects.equals(name, bundle.name) && Objects.equals(ratio, bundle.ratio) && Objects.equals(imageSizes, bundle.imageSizes) && Objects.equals(customRenditions, bundle.customRenditions); } @Override @@ -67,13 +70,17 @@ static class ImageSize { @SerializedName(value = "id", alternate = {"media", "name"}) private final String id; private final int width; + @Nullable + private final String ratio; ImageSize( - String id, - int width + final String id, + final int width, + @Nullable final String ratio ) { this.width = width; this.id = id; + this.ratio = ratio; } public String getId() { @@ -84,17 +91,21 @@ public int getWidth() { return width; } + public Optional getRatio() { + return Optional.ofNullable(ratio); + } + @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ImageSize imageSize = (ImageSize) o; - return width == imageSize.width && Objects.equals(id, imageSize.id); + return width == imageSize.width && Objects.equals(id, imageSize.id) && Objects.equals(ratio, imageSize.ratio); } @Override public int hashCode() { - return Objects.hash(id, width); + return Objects.hash(id, width, ratio); } @Override @@ -102,6 +113,7 @@ public String toString() { return "ImageSize{" + "id='" + id + '\'' + ", width=" + width + + ", ratio=" + ratio + '}'; } } diff --git a/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/BundleProcessor.java b/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/BundleProcessor.java index 65c1101..96b08f9 100644 --- a/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/BundleProcessor.java +++ b/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/BundleProcessor.java @@ -1,33 +1,48 @@ package com.merkle.oss.magnolia.imaging.flexible.model.bundle; -import java.math.BigDecimal; -import java.math.RoundingMode; +import javax.annotation.Nullable; +import javax.inject.Inject; import java.util.List; import java.util.stream.Collectors; public class BundleProcessor { + private final RatioParser ratioParser; + + @Inject + public BundleProcessor(final RatioParser ratioParser) { + this.ratioParser = ratioParser; + } ProcessedBundle process(final Bundle bundle) { return new ProcessedBundle( bundle.getName(), - processImageSizes(bundle.getImageSizes(), bundle.getRatio()), - processImageSizes(bundle.getCustomRenditions(), bundle.getRatio()) + processImageSizes(bundle.getName(), bundle.getImageSizes(), bundle.getRatio().orElse(null)), + processImageSizes(bundle.getName(), bundle.getCustomRenditions(), bundle.getRatio().orElse(null)) ); } - private List processImageSizes(final List imageSizes, final double ratio) { + private List processImageSizes( + final String bundleName, + final List imageSizes, + @Nullable final String bundleRatio + ) { return imageSizes.stream() .map(imageSize -> new ProcessedBundle.ImageSize( + validateRatioOrThrow(bundleName, imageSize.getRatio().orElse(bundleRatio)), imageSize.getWidth(), - calculateHeight(imageSize.getWidth(), ratio), imageSize.getId() ) ) .collect(Collectors.toList()); } - private int calculateHeight(final int width, final double ratio) { - return BigDecimal.valueOf(width / ratio).setScale(0, RoundingMode.UP).intValue(); + private String validateRatioOrThrow(final String bundleName, @Nullable final String ratio) { + if(ratio != null) { + ratioParser.parse(ratio).orElseThrow(() -> + new IllegalArgumentException("invalid ratio "+ratio+" in bundle "+bundleName) + ); + } + return ratio; } } diff --git a/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/ProcessedBundle.java b/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/ProcessedBundle.java index 0c4530d..ebec697 100644 --- a/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/ProcessedBundle.java +++ b/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/ProcessedBundle.java @@ -1,7 +1,9 @@ package com.merkle.oss.magnolia.imaging.flexible.model.bundle; +import javax.annotation.Nullable; import java.util.List; import java.util.Objects; +import java.util.Optional; public class ProcessedBundle { private final String name; @@ -53,26 +55,27 @@ public String toString() { } public static class ImageSize { + @Nullable + private final String ratio; private final int width; - private final int height; private final String id; ImageSize( + @Nullable final String ratio, final int width, - final int height, final String id ){ + this.ratio = ratio; this.width = width; - this.height = height; this.id = id; } - public int getWidth() { - return width; + public Optional getRatio() { + return Optional.ofNullable(ratio); } - public int getHeight() { - return height; + public int getWidth() { + return width; } public String getId() { @@ -84,19 +87,19 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ImageSize imageSize = (ImageSize) o; - return width == imageSize.width && height == imageSize.height && Objects.equals(id, imageSize.id); + return width == imageSize.width && Objects.equals(ratio, imageSize.ratio) && Objects.equals(id, imageSize.id); } @Override public int hashCode() { - return Objects.hash(width, height, id); + return Objects.hash(ratio, width, id); } @Override public String toString() { return "ImageSize{" + - "width=" + width + - ", height=" + height + + "ratio='" + ratio + '\'' + + ", width=" + width + ", id='" + id + '\'' + '}'; } diff --git a/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/RatioParser.java b/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/RatioParser.java new file mode 100644 index 0000000..6786757 --- /dev/null +++ b/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/RatioParser.java @@ -0,0 +1,19 @@ +package com.merkle.oss.magnolia.imaging.flexible.model.bundle; + +import java.util.Optional; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class RatioParser { + private static final Pattern RATIO = Pattern.compile("^(\\d+):(\\d+)$"); + + public Optional parse(final String ratio) { + final Matcher matcher = RATIO.matcher(ratio); + if(matcher.matches()) { + final double dividend = Double.parseDouble(matcher.group(1)); + final double divisor = Double.parseDouble(matcher.group(2)); + return Optional.of(dividend/divisor); + } + return Optional.empty(); + } +} diff --git a/src/test/java/com/merkle/oss/magnolia/imaging/flexible/generator/FlexibleImageUriFactoryTest.java b/src/test/java/com/merkle/oss/magnolia/imaging/flexible/generator/FlexibleImageUriFactoryTest.java index 84fcb48..1d20c4b 100644 --- a/src/test/java/com/merkle/oss/magnolia/imaging/flexible/generator/FlexibleImageUriFactoryTest.java +++ b/src/test/java/com/merkle/oss/magnolia/imaging/flexible/generator/FlexibleImageUriFactoryTest.java @@ -23,13 +23,13 @@ void create() throws URISyntaxException { final Asset asset = mock(Asset.class); doReturn(ItemKey.from("jcr:b3ee7444-4830-4454-abbb-20fc35387032")).when(asset).getItemKey(); doReturn("someImage.jpg").when(asset).getFileName(); - final FlexibleParameter parameter = new FlexibleParameter(new DynamicImageParameter(true), 100, 50, asset); + final FlexibleParameter parameter = new FlexibleParameter(new DynamicImageParameter(true), "16:9", 100, asset); try (MockedStatic mgnlContext = Mockito.mockStatic(MgnlContext.class)) { mgnlContext.when(MgnlContext::getContextPath).thenReturn("/author"); assertEquals( - new URI("/author/.imaging/flex/jcr:b3ee7444-4830-4454-abbb-20fc35387032/crop/true/height/50/width/100/someImage.jpg"), + new URI("/author/.imaging/flex/jcr:b3ee7444-4830-4454-abbb-20fc35387032/crop/true/ratio/16:9/width/100/someImage.jpg"), new FlexibleImageUriFactory().create(parameter) ); } diff --git a/src/test/java/com/merkle/oss/magnolia/imaging/flexible/generator/FlexibleImageUriParserTest.java b/src/test/java/com/merkle/oss/magnolia/imaging/flexible/generator/FlexibleImageUriParserTest.java index eb13a35..e15d081 100644 --- a/src/test/java/com/merkle/oss/magnolia/imaging/flexible/generator/FlexibleImageUriParserTest.java +++ b/src/test/java/com/merkle/oss/magnolia/imaging/flexible/generator/FlexibleImageUriParserTest.java @@ -30,7 +30,7 @@ void setUp() { final ProcessedBundlesProvider processedBundlesProvider = mock(ProcessedBundlesProvider.class); final ProcessedBundle.ImageSize imageSize = mock(ProcessedBundle.ImageSize.class); doReturn(560).when(imageSize).getWidth(); - doReturn(316).when(imageSize).getHeight(); + doReturn(Optional.of("16:9")).when(imageSize).getRatio(); final ProcessedBundle processedBundle = mock(ProcessedBundle.class); doReturn(List.of(imageSize)).when(processedBundle).getImageSizes(); doReturn(Collections.emptyList()).when(processedBundle).getCustomRenditions(); @@ -42,8 +42,8 @@ void setUp() { @Test void parse_valid() { assertEquals( - Optional.of(new FlexibleParameter(new DynamicImageParameter(true), 560, 316, asset)), - flexibleImageUriParser.parse(createRequest("/author/.imaging/flex/jcr:b3ee7444-4830-4454-abbb-20fc35387032/crop/true/height/316/width/560/dummy1-1600x900.jpg")) + Optional.of(new FlexibleParameter(new DynamicImageParameter(true), "16:9", 560, asset)), + flexibleImageUriParser.parse(createRequest("/author/.imaging/flex/jcr:b3ee7444-4830-4454-abbb-20fc35387032/crop/true/ratio/16:9/width/560/dummy1-1600x900.jpg")) ); } @@ -51,7 +51,7 @@ void parse_valid() { void parse_invalid() { assertEquals( Optional.empty(), - flexibleImageUriParser.parse(createRequest("/author/.imaging/flex/jcr:b3ee7444-4830-4454-abbb-20fc35387032/crop/true/height/390/width/560/dummy1-1600x900.jpg")) + flexibleImageUriParser.parse(createRequest("/author/.imaging/flex/jcr:b3ee7444-4830-4454-abbb-20fc35387032/crop/true/ratio/16:10/width/560/dummy1-1600x900.jpg")) ); } diff --git a/src/test/java/com/merkle/oss/magnolia/imaging/flexible/generator/FlexibleParameterCachingStrategyTest.java b/src/test/java/com/merkle/oss/magnolia/imaging/flexible/generator/FlexibleParameterCachingStrategyTest.java index f414a96..928bea7 100644 --- a/src/test/java/com/merkle/oss/magnolia/imaging/flexible/generator/FlexibleParameterCachingStrategyTest.java +++ b/src/test/java/com/merkle/oss/magnolia/imaging/flexible/generator/FlexibleParameterCachingStrategyTest.java @@ -46,16 +46,16 @@ void setUp() throws RepositoryException { @Test void getCachePath_noDynamicImageParameter() { assertEquals( - "/flex/jcr%3Ab3ee7444-4830-4454-abbb-20fc35387032/560/316/0", - flexibleParameterCachingStrategy.getCachePath(generator, () -> new FlexibleParameter(null, 560, 316, asset)) + "/flex/jcr%3Ab3ee7444-4830-4454-abbb-20fc35387032/560/16%3A9/0", + flexibleParameterCachingStrategy.getCachePath(generator, () -> new FlexibleParameter(null, "16:9", 560, asset)) ); } @Test void getCachePath_dynamicImageParameter() { assertEquals( - "/flex/jcr%3Ab3ee7444-4830-4454-abbb-20fc35387032/560/316/1268", - flexibleParameterCachingStrategy.getCachePath(generator, () -> new FlexibleParameter(new DynamicImageParameter(false), 560, 316, asset)) + "/flex/jcr%3Ab3ee7444-4830-4454-abbb-20fc35387032/560/16%3A9/1268", + flexibleParameterCachingStrategy.getCachePath(generator, () -> new FlexibleParameter(new DynamicImageParameter(false), "16:9", 560, asset)) ); } @@ -63,13 +63,13 @@ void getCachePath_dynamicImageParameter() { void shouldRegenerate_assetModifiedAfterCachedBinary_shouldRegenerate() throws RepositoryException { cachedBinaryCalendar.set(2023, Calendar.OCTOBER, 26, 14, 30); assetCalendar.set(2023, Calendar.OCTOBER, 26, 14, 31); - assertTrue(flexibleParameterCachingStrategy.shouldRegenerate(cachedBinary, () -> new FlexibleParameter(null, 560, 316, asset))); + assertTrue(flexibleParameterCachingStrategy.shouldRegenerate(cachedBinary, () -> new FlexibleParameter(null, "16:9", 560, asset))); } @Test void shouldRegenerate_assetModifiedBeforeCachedBinary_shouldNotRegenerate() throws RepositoryException { cachedBinaryCalendar.set(2023, Calendar.OCTOBER, 26, 14, 30); assetCalendar.set(2023, Calendar.OCTOBER, 26, 14, 29); - assertFalse(flexibleParameterCachingStrategy.shouldRegenerate(cachedBinary, () -> new FlexibleParameter(null, 560, 316, asset))); + assertFalse(flexibleParameterCachingStrategy.shouldRegenerate(cachedBinary, () -> new FlexibleParameter(null, "16:9", 560, asset))); } } \ No newline at end of file diff --git a/src/test/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/BundleProcessorTest.java b/src/test/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/BundleProcessorTest.java index 02ebc9f..50845eb 100644 --- a/src/test/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/BundleProcessorTest.java +++ b/src/test/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/BundleProcessorTest.java @@ -12,26 +12,26 @@ class BundleProcessorTest { void process() { assertEquals( new ProcessedBundle( - "16x9", + "someBundle", List.of( - new ProcessedBundle.ImageSize(560, 316, "560w"), - new ProcessedBundle.ImageSize(1120, 631, "1120w"), - new ProcessedBundle.ImageSize(2208, 1243, "2208w") + new ProcessedBundle.ImageSize("1:1", 560, "560w"), + new ProcessedBundle.ImageSize("16:9", 1120, "1120w"), + new ProcessedBundle.ImageSize("16:9", 2208, "2208w") ), List.of( - new ProcessedBundle.ImageSize(560, 316, "fallbackImage"), - new ProcessedBundle.ImageSize(16, 10, "previewImage") + new ProcessedBundle.ImageSize("16:9", 560, "fallbackImage"), + new ProcessedBundle.ImageSize("16:9", 16, "previewImage") ) ), - new BundleProcessor().process(new Bundle("16x9", 1.77777, + new BundleProcessor(new RatioParser()).process(new Bundle("someBundle", "16:9", List.of( - new Bundle.ImageSize("560w", 560), - new Bundle.ImageSize("1120w", 1120), - new Bundle.ImageSize("2208w", 2208) + new Bundle.ImageSize("560w", 560, "1:1"), + new Bundle.ImageSize("1120w", 1120, null), + new Bundle.ImageSize("2208w", 2208, null) ), List.of( - new Bundle.ImageSize("fallbackImage", 560), - new Bundle.ImageSize("previewImage", 16) + new Bundle.ImageSize("fallbackImage", 560, null), + new Bundle.ImageSize("previewImage", 16, null) ) )) ); diff --git a/src/test/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/BundlesParserTest.java b/src/test/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/BundlesParserTest.java index 46ab623..2123bce 100644 --- a/src/test/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/BundlesParserTest.java +++ b/src/test/java/com/merkle/oss/magnolia/imaging/flexible/model/bundle/BundlesParserTest.java @@ -3,6 +3,7 @@ import org.junit.jupiter.api.Test; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -16,31 +17,42 @@ void parse() { final String relativePath = absolutePath.replaceFirst(resourcesRoot, ""); assertEquals( - List.of( - new Bundle("1x1", 1.0, + Set.of( + new Bundle("1x1", "1:1", List.of( - new Bundle.ImageSize("560w", 560), - new Bundle.ImageSize("1000w", 1000), - new Bundle.ImageSize("2000w", 2000) + new Bundle.ImageSize("560w", 560, null), + new Bundle.ImageSize("1000w", 1000, null), + new Bundle.ImageSize("2000w", 2000, null) ), List.of( - new Bundle.ImageSize("fallbackImage", 560), - new Bundle.ImageSize("previewImage", 10) + new Bundle.ImageSize("fallbackImage", 560, null), + new Bundle.ImageSize("previewImage", 10, null) ) ), - new Bundle("16x9", 1.77777, + new Bundle("16x9", "16:9", List.of( - new Bundle.ImageSize("560w", 560), - new Bundle.ImageSize("1120w", 1120), - new Bundle.ImageSize("2208w", 2208) + new Bundle.ImageSize("560w", 560, null), + new Bundle.ImageSize("1120w", 1120, null), + new Bundle.ImageSize("2208w", 2208, null) ), List.of( - new Bundle.ImageSize("fallbackImage", 560), - new Bundle.ImageSize("previewImage", 16) + new Bundle.ImageSize("fallbackImage", 560, null), + new Bundle.ImageSize("previewImage", 16, null) + ) + ), + new Bundle("stage", null, + List.of( + new Bundle.ImageSize("560w", 560, "1:1"), + new Bundle.ImageSize("1120w", 1120, null), + new Bundle.ImageSize("2208w", 2208, null) + ), + List.of( + new Bundle.ImageSize("fallbackImage", 560, null), + new Bundle.ImageSize("previewImage", 16, null) ) ) ), - new BundlesParser().parse(relativePath).collect(Collectors.toList()) + new BundlesParser().parse(relativePath).collect(Collectors.toSet()) ); } } \ No newline at end of file diff --git a/src/test/resources/com/merkle/oss/magnolia/imaging/flexible/model/bundle/_bundles/16x9.json b/src/test/resources/com/merkle/oss/magnolia/imaging/flexible/model/bundle/_bundles/16x9.json index 8fe8199..3b60337 100644 --- a/src/test/resources/com/merkle/oss/magnolia/imaging/flexible/model/bundle/_bundles/16x9.json +++ b/src/test/resources/com/merkle/oss/magnolia/imaging/flexible/model/bundle/_bundles/16x9.json @@ -2,7 +2,7 @@ { "bundle": "16x9", "description": "ratio 16:9 bundle, VP xs - xl 12cols wide", - "ratio": 1.77777, + "ratio": "16:9", "imageSizes": [ { "media": "560w", diff --git a/src/test/resources/com/merkle/oss/magnolia/imaging/flexible/model/bundle/_bundles/1x1.json b/src/test/resources/com/merkle/oss/magnolia/imaging/flexible/model/bundle/_bundles/1x1.json index 80d6c24..27b0690 100644 --- a/src/test/resources/com/merkle/oss/magnolia/imaging/flexible/model/bundle/_bundles/1x1.json +++ b/src/test/resources/com/merkle/oss/magnolia/imaging/flexible/model/bundle/_bundles/1x1.json @@ -2,7 +2,7 @@ { "bundle": "1x1", "description": "ratio 1:1 bundle, VP xs - sm 12cols wide, VP md-xl 8cols wide", - "ratio": 1.0, + "ratio": "1:1", "imageSizes": [ { "id": "560w", diff --git a/src/test/resources/com/merkle/oss/magnolia/imaging/flexible/model/bundle/_bundles/stage.json b/src/test/resources/com/merkle/oss/magnolia/imaging/flexible/model/bundle/_bundles/stage.json new file mode 100644 index 0000000..acd1cf1 --- /dev/null +++ b/src/test/resources/com/merkle/oss/magnolia/imaging/flexible/model/bundle/_bundles/stage.json @@ -0,0 +1,31 @@ +[ + { + "bundle": "stage", + "description": "stage bundle, VP xs - xl 12cols wide", + "imageSizes": [ + { + "ratio": "1:1", + "media": "560w", + "width": 560 + }, + { + "media": "1120w", + "width": 1120 + }, + { + "media": "2208w", + "width": 2208 + } + ], + "customRenditions": [ + { + "name": "fallbackImage", + "width": 560 + }, + { + "name": "previewImage", + "width": 16 + } + ] + } +]