Skip to content

Commit

Permalink
add last-mod timestamp as version string
Browse files Browse the repository at this point in the history
liquidsunset committed Nov 4, 2024
1 parent f93bb8c commit 5344fd1
Showing 3 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@

import javax.annotation.Nullable;
import javax.inject.Inject;
import java.util.Calendar;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
@@ -49,11 +50,23 @@ public Map<String, String> toMap() {
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.WIDTH_PARAM, String.valueOf(getWidth()),
Factory.VERSION_PARAM, getVersion()
).entrySet().stream()
).flatMap(Function.identity()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (dynamicImageParam, flexibleParam) -> flexibleParam));
}

private String getVersion() {
Calendar timeStamp = getLastModified();
if (timeStamp == null) {
timeStamp = getCreated();
}
if (timeStamp == null) {
timeStamp = Calendar.getInstance();
}
return String.valueOf(timeStamp.toInstant().toEpochMilli());
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
@@ -80,6 +93,7 @@ public String toString() {
public static class Factory {
public static final String WIDTH_PARAM = "width";
public static final String RATIO_PARAM = "ratio";
public static final String VERSION_PARAM = "v";

private final DynamicImageParameter.Factory dynamicImageParameterFactory;

Original file line number Diff line number Diff line change
@@ -5,22 +5,39 @@
import info.magnolia.context.MgnlContext;
import info.magnolia.dam.api.Asset;
import info.magnolia.dam.api.ItemKey;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.ZoneOffset;
import java.util.Calendar;
import java.util.TimeZone;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class FlexibleImageUriFactoryTest {

private Asset asset;

@BeforeEach
void setUp() throws IOException {
asset = mock(Asset.class);
final Calendar lastModified = new Calendar.Builder()
.setDate(2024, 11, 3)
.setTimeZone(TimeZone.getTimeZone(ZoneOffset.UTC))
.build();
when(asset.getLastModified()).thenReturn(lastModified);
}

@Test
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), "16:9", 100, asset);
@@ -29,15 +46,14 @@ void create() throws URISyntaxException {
mgnlContext.when(MgnlContext::getContextPath).thenReturn("/author");

assertEquals(
new URI("/author/.imaging/flex/jcr:b3ee7444-4830-4454-abbb-20fc35387032/crop/true/ratio/16:9/width/100/someImage.jpg"),
new URI("/author/.imaging/flex/jcr:b3ee7444-4830-4454-abbb-20fc35387032/crop/true/ratio/16:9/v/1733184000000/width/100/someImage.jpg"),
new FlexibleImageUriFactory().create(parameter)
);
}
}

@Test
void createNoContext() 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), "16:9", 100, asset);
@@ -46,7 +62,7 @@ void createNoContext() throws URISyntaxException {
mgnlContext.when(MgnlContext::getContextPath).thenReturn("/");

assertEquals(
new URI("/.imaging/flex/jcr:b3ee7444-4830-4454-abbb-20fc35387032/crop/true/ratio/16:9/width/100/someImage.jpg"),
new URI("/.imaging/flex/jcr:b3ee7444-4830-4454-abbb-20fc35387032/crop/true/ratio/16:9/v/1733184000000/width/100/someImage.jpg"),
new FlexibleImageUriFactory().create(parameter)
);
}
Original file line number Diff line number Diff line change
@@ -14,10 +14,14 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.ZoneOffset;
import java.util.Calendar;
import java.util.TimeZone;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class HashedFlexibleImageUriFactoryTest {
private HashedFlexibleImageUriFactory uriFactory;
@@ -32,6 +36,12 @@ void setUp() throws IOException {
@Test
void create() throws URISyntaxException {
final Asset asset = mock(Asset.class);
final Calendar lastModified = new Calendar.Builder()
.setDate(2024, 11, 3)
.setTimeZone(TimeZone.getTimeZone(ZoneOffset.UTC))
.build();
when(asset.getLastModified()).thenReturn(lastModified);

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), "16:9", 100, asset);
@@ -40,7 +50,7 @@ void create() throws URISyntaxException {
mgnlContext.when(MgnlContext::getContextPath).thenReturn("/author");

assertEquals(
new URI("/author/.imaging/flex/jcr:b3ee7444-4830-4454-abbb-20fc35387032/crop/true/hash/1952df994c77d7b92999fef87833207f/ratio/16:9/width/100/someImage.jpg"),
new URI("/author/.imaging/flex/jcr:b3ee7444-4830-4454-abbb-20fc35387032/crop/true/hash/1c652037de63be8e863d11249cd6386a/ratio/16:9/v/1733184000000/width/100/someImage.jpg"),
uriFactory.create(parameter)
);
}

0 comments on commit 5344fd1

Please sign in to comment.