Skip to content

Commit

Permalink
add last-mod timestamp as version string
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidsunset committed Nov 4, 2024
1 parent f93bb8c commit 4f64297
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,36 @@
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.util.Calendar;

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)
.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);
Expand All @@ -29,15 +43,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/1733180400000/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);
Expand All @@ -46,7 +59,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/1733180400000/width/100/someImage.jpg"),
new FlexibleImageUriFactory().create(parameter)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Calendar;

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;
Expand All @@ -32,6 +34,11 @@ 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)
.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);
Expand All @@ -40,7 +47,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/5cb4746eea6769f8b97c6a95c709ed52/ratio/16:9/v/1733180400000/width/100/someImage.jpg"),
uriFactory.create(parameter)
);
}
Expand Down

0 comments on commit 4f64297

Please sign in to comment.