-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throw if images of assets with unsupported mime types are being gener…
…ated (e.g. gif, video)
- Loading branch information
eschleb
committed
Feb 16, 2024
1 parent
844cac2
commit 8c7953a
Showing
10 changed files
with
216 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...ava/com/merkle/oss/magnolia/imaging/flexible/generator/DefaultImageOperationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.merkle.oss.magnolia.imaging.flexible.generator; | ||
|
||
import com.merkle.oss.magnolia.imaging.flexible.generator.operation.FlexibleAutoCropAndResize; | ||
import com.merkle.oss.magnolia.imaging.flexible.generator.operation.FlexibleBoundedResize; | ||
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 info.magnolia.imaging.ParameterProvider; | ||
import info.magnolia.imaging.operations.ImageOperationChain; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Provider; | ||
|
||
public class DefaultImageOperationProvider implements ImageOperationProvider { | ||
private final Provider<FromFlexibleParameter> fromFlexibleParameter; | ||
private final Provider<FlexibleAutoCropAndResize> flexibleAutoCropAndResize; | ||
private final Provider<FlexibleBoundedResize> flexibleBoundedResize; | ||
|
||
@Inject | ||
public DefaultImageOperationProvider( | ||
final Provider<FromFlexibleParameter> fromFlexibleParameter, | ||
final Provider<FlexibleAutoCropAndResize> flexibleAutoCropAndResize, | ||
final Provider<FlexibleBoundedResize> flexibleBoundedResize | ||
) { | ||
this.fromFlexibleParameter = fromFlexibleParameter; | ||
this.flexibleAutoCropAndResize = flexibleAutoCropAndResize; | ||
this.flexibleBoundedResize = flexibleBoundedResize; | ||
} | ||
|
||
@Override | ||
public ImageOperationChain<ParameterProvider<FlexibleParameter>> get(final FlexibleParameter parameter) { | ||
final ImageOperationChain<ParameterProvider<FlexibleParameter>> chain = new ImageOperationChain<>(); | ||
chain.addOperation(fromFlexibleParameter.get()); | ||
|
||
if (parameter.getDynamicImageParameter().map(DynamicImageParameter::isCrop).orElse(true)) { | ||
chain.addOperation(flexibleAutoCropAndResize.get()); | ||
} else { | ||
chain.addOperation(flexibleBoundedResize.get()); | ||
} | ||
return chain; | ||
} | ||
|
||
|
||
} |
50 changes: 2 additions & 48 deletions
50
src/main/java/com/merkle/oss/magnolia/imaging/flexible/generator/ImageOperationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,9 @@ | ||
package com.merkle.oss.magnolia.imaging.flexible.generator; | ||
|
||
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<ParameterProvider<FlexibleParameter>> get(final FlexibleParameter parameter) { | ||
final ImageOperationChain<ParameterProvider<FlexibleParameter>> chain = new ImageOperationChain<>(); | ||
chain.addOperation(new FromFlexibleParameter()); | ||
|
||
if (parameter.getDynamicImageParameter().map(DynamicImageParameter::isCrop).orElse(true)) { | ||
final AutoCropAndResize resize = new AutoCropAndResize(); | ||
resize.setResizer(new MultiStepResizer()); | ||
resize.setTargetWidth(parameter.getWidth()); | ||
calculateHeight(parameter).ifPresent(resize::setTargetHeight); | ||
chain.addOperation(resize); | ||
} else { | ||
final BoundedResize resize = new BoundedResize(); | ||
resize.setResizer(new MultiStepResizer()); | ||
resize.setMaxWidth(parameter.getWidth()); | ||
resize.setMaxHeight(calculateHeight(parameter).orElse(Integer.MAX_VALUE)); | ||
chain.addOperation(resize); | ||
} | ||
return chain; | ||
} | ||
|
||
protected Optional<Integer> 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(); | ||
} | ||
public interface ImageOperationProvider { | ||
ImageOperationChain<ParameterProvider<FlexibleParameter>> get(FlexibleParameter parameter); | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/merkle/oss/magnolia/imaging/flexible/generator/operation/AssetUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.merkle.oss.magnolia.imaging.flexible.generator.operation; | ||
|
||
import info.magnolia.dam.api.Asset; | ||
|
||
import javax.imageio.ImageIO; | ||
import javax.imageio.ImageReader; | ||
import javax.imageio.stream.ImageInputStream; | ||
import java.io.IOException; | ||
|
||
public class AssetUtil { | ||
|
||
public boolean isAnimated(final Asset asset) throws IOException { | ||
try (ImageInputStream iis = ImageIO.createImageInputStream(asset.getContentStream())) { | ||
final ImageReader imageReader = ImageIO.getImageReadersByMIMEType(asset.getMimeType()).next(); | ||
imageReader.setInput(iis); | ||
|
||
final int numImages = imageReader.getNumImages(true); | ||
return numImages > 1; | ||
} | ||
} | ||
|
||
public boolean isImage(final Asset asset) { | ||
return ImageIO.getImageReadersByMIMEType(asset.getMimeType()).hasNext(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...m/merkle/oss/magnolia/imaging/flexible/generator/operation/FlexibleAutoCropAndResize.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.merkle.oss.magnolia.imaging.flexible.generator.operation; | ||
|
||
import com.merkle.oss.magnolia.imaging.flexible.model.FlexibleParameter; | ||
import info.magnolia.imaging.ImagingException; | ||
import info.magnolia.imaging.ParameterProvider; | ||
import info.magnolia.imaging.operations.ImageOperation; | ||
import info.magnolia.imaging.operations.cropresize.AutoCropAndResize; | ||
import info.magnolia.imaging.operations.cropresize.resizers.MultiStepResizer; | ||
|
||
import javax.inject.Inject; | ||
import java.awt.image.BufferedImage; | ||
|
||
public class FlexibleAutoCropAndResize implements ImageOperation<ParameterProvider<FlexibleParameter>> { | ||
private final AssetUtil assetUtil; | ||
private final HeightCalculator heightCalculator; | ||
|
||
@Inject | ||
public FlexibleAutoCropAndResize( | ||
final AssetUtil assetUtil, | ||
final HeightCalculator heightCalculator | ||
) { | ||
this.assetUtil = assetUtil; | ||
this.heightCalculator = heightCalculator; | ||
} | ||
|
||
@Override | ||
public BufferedImage apply(final BufferedImage source, final ParameterProvider<FlexibleParameter> params) throws ImagingException { | ||
final FlexibleParameter parameter = params.getParameter(); | ||
if (!isSupported(parameter)) { | ||
throw new ImagingException(getClass() + " doesn't support " + parameter); | ||
} | ||
final AutoCropAndResize autoCropAndResize = new AutoCropAndResize(); | ||
autoCropAndResize.setResizer(new MultiStepResizer()); | ||
autoCropAndResize.setTargetWidth(parameter.getWidth()); | ||
heightCalculator.calculateHeight(parameter).ifPresent(autoCropAndResize::setTargetHeight); | ||
return autoCropAndResize.apply(source, params); | ||
} | ||
|
||
public boolean isSupported(final FlexibleParameter parameter) { | ||
try { | ||
return !assetUtil.isAnimated(parameter); | ||
} catch (Exception e) { | ||
return false; | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...a/com/merkle/oss/magnolia/imaging/flexible/generator/operation/FlexibleBoundedResize.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.merkle.oss.magnolia.imaging.flexible.generator.operation; | ||
|
||
import com.merkle.oss.magnolia.imaging.flexible.model.FlexibleParameter; | ||
import info.magnolia.imaging.ImagingException; | ||
import info.magnolia.imaging.ParameterProvider; | ||
import info.magnolia.imaging.operations.ImageOperation; | ||
import info.magnolia.imaging.operations.cropresize.BoundedResize; | ||
import info.magnolia.imaging.operations.cropresize.resizers.MultiStepResizer; | ||
|
||
import javax.inject.Inject; | ||
import java.awt.image.BufferedImage; | ||
|
||
public class FlexibleBoundedResize implements ImageOperation<ParameterProvider<FlexibleParameter>> { | ||
private final AssetUtil assetUtil; | ||
private final HeightCalculator heightCalculator; | ||
|
||
@Inject | ||
public FlexibleBoundedResize( | ||
final AssetUtil assetUtil, | ||
final HeightCalculator heightCalculator | ||
) { | ||
this.assetUtil = assetUtil; | ||
this.heightCalculator = heightCalculator; | ||
} | ||
|
||
@Override | ||
public BufferedImage apply(final BufferedImage source, final ParameterProvider<FlexibleParameter> params) throws ImagingException { | ||
final FlexibleParameter parameter = params.getParameter(); | ||
if (!isSupported(parameter)) { | ||
throw new ImagingException(getClass() + " doesn't support " + parameter); | ||
} | ||
final BoundedResize boundedResize = new BoundedResize(); | ||
boundedResize.setResizer(new MultiStepResizer()); | ||
boundedResize.setMaxWidth(parameter.getWidth()); | ||
boundedResize.setMaxHeight(heightCalculator.calculateHeight(parameter).orElse(Integer.MAX_VALUE)); | ||
return boundedResize.apply(source, params); | ||
} | ||
|
||
public boolean isSupported(final FlexibleParameter parameter) { | ||
try { | ||
return !assetUtil.isAnimated(parameter); | ||
} catch (Exception e) { | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...n/java/com/merkle/oss/magnolia/imaging/flexible/generator/operation/HeightCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.merkle.oss.magnolia.imaging.flexible.generator.operation; | ||
|
||
import com.merkle.oss.magnolia.imaging.flexible.model.FlexibleParameter; | ||
import com.merkle.oss.magnolia.imaging.flexible.model.bundle.RatioParser; | ||
|
||
import javax.inject.Inject; | ||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
import java.util.Optional; | ||
|
||
public class HeightCalculator { | ||
private final RatioParser ratioParser; | ||
|
||
@Inject | ||
public HeightCalculator(final RatioParser ratioParser) { | ||
this.ratioParser = ratioParser; | ||
} | ||
|
||
public Optional<Integer> 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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters