Skip to content

Commit

Permalink
Web-Optimized Image Delivery: Sanitize SEO name (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert authored Jan 17, 2024
1 parent 1917ad8 commit b048c83
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -44,6 +46,8 @@ final class ParameterMap {
static final String PARAM_ROTATE = "r";
static final String PARAM_QUALITY = "quality";

static final Pattern SEO_NAME_FILTER_PATTERN = Pattern.compile("[\\W_]");

private static final Set<String> SUPPORTED_FORMATS = Set.of(
FileExtension.JPEG,
FileExtension.PNG,
Expand Down Expand Up @@ -72,7 +76,7 @@ static Map<String, Object> build(@NotNull Asset asset, @NotNull WebOptimizedImag
// please note: AssetDelivery API expects all values as strings
Map<String, Object> map = new HashMap<>();
map.put(PARAM_PATH, path);
map.put(PARAM_SEO_NAME, seoName);
map.put(PARAM_SEO_NAME, sanitizeSeoName(seoName));
map.put(PARAM_FORMAT, format);
map.put(PARAM_PREFER_WEBP, "true");
if (width != null) {
Expand All @@ -90,4 +94,14 @@ static Map<String, Object> build(@NotNull Asset asset, @NotNull WebOptimizedImag
return map;
}

/**
* Sanitizes the SEO name to avoid problems with special characters in URLs.
* @param name Name
* @return Sanitzed name
*/
private static String sanitizeSeoName(String name) {
Matcher matcher = SEO_NAME_FILTER_PATTERN.matcher(name);
return StringUtils.toRootLowerCase(matcher.replaceAll("-"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ void testGetDeliveryUrl_AssetDeliveryNotPresent() {
void testGetDeliveryUrl_AssetDeliveryPresent() {
context.registerInjectActivateService(MockAssetDelivery.class);
WebOptimizedImageDeliveryService underTest = context.registerInjectActivateService(WebOptimizedImageDeliveryServiceImpl.class);
Asset asset = context.create().asset("/content/dam/test.jpg", 10, 10, ContentType.JPEG);
Asset asset = context.create().asset("/content/dam/Test_1.jpg", 10, 10, ContentType.JPEG);
String assetId = MockAssetDelivery.getAssetId(asset);

assertEquals("/asset/delivery/" + assetId + "/test.jpg?preferwebp=true",
assertEquals("/asset/delivery/" + assetId + "/test-1.jpg?preferwebp=true",
underTest.getDeliveryUrl(asset, new WebOptimizedImageDeliveryParams()));

assertEquals("/asset/delivery/" + assetId + "/test.jpg?c=0%2C0%2C2%2C4&preferwebp=true&r=90&width=10",
assertEquals("/asset/delivery/" + assetId + "/test-1.jpg?c=0%2C0%2C2%2C4&preferwebp=true&r=90&width=10",
underTest.getDeliveryUrl(asset, new WebOptimizedImageDeliveryParams()
.width(10L).cropDimension(new CropDimension(0, 0, 2, 4)).rotation(90)));
}
Expand Down

0 comments on commit b048c83

Please sign in to comment.