Skip to content

Commit

Permalink
Merge pull request #1159 from davidjgonzalez/oft
Browse files Browse the repository at this point in the history
Fixed issue with thumbnails not showing for assets missing dc:format
  • Loading branch information
davidjgonzalez authored Jul 22, 2024
2 parents 5bd7171 + 64747ab commit e4f452b
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 10 deletions.
3 changes: 3 additions & 0 deletions all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@
<id>cloud</id>
<activation>
<activeByDefault>false</activeByDefault>
<property>
<name>env.CM_BUILD</name>
</property>
</activation>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,13 @@ public boolean accepts(AssetModel assetModel, String renditionName) {
final String assetFormat = StringUtils.lowerCase(assetModel.getProperties().get(DamConstants.DC_FORMAT, String.class));

// Return true if assetFormat matches any regex in ALLOWED_MIME_TYPES
return Arrays.stream(ACCEPTED_MIME_TYPES).anyMatch(regex -> assetFormat.matches(regex));
if (StringUtils.isBlank(assetFormat)) {
// If the asset format is not set, we can't determine if it's an image or document, so we can't use Asset Delivery
return false;
} else {
// Otherwise, check if the asset format is an image or document
return Arrays.stream(ACCEPTED_MIME_TYPES).anyMatch(regex -> assetFormat.matches(regex));
}
}

protected String evaluateExpression(final SlingHttpServletRequest request, String expression) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.adobe.aem.commons.assetshare.content.renditions.impl.dispatchers;

import com.adobe.aem.commons.assetshare.content.AssetModel;
import com.adobe.aem.commons.assetshare.content.impl.AssetModelImpl;
import com.adobe.aem.commons.assetshare.content.renditions.AssetRenditionDispatcher;
import com.adobe.aem.commons.assetshare.content.renditions.impl.AssetRenditionsImpl;
import com.adobe.aem.commons.assetshare.testing.MockAssetModels;
import com.adobe.aem.commons.assetshare.testing.RequireAemMock;
import com.adobe.aem.commons.assetshare.util.ExpressionEvaluator;
import com.adobe.aem.commons.assetshare.util.RequireAem;
import com.adobe.aem.commons.assetshare.util.impl.ExpressionEvaluatorImpl;
import com.adobe.cq.dam.download.api.DownloadTarget;
import com.adobe.cq.wcm.spi.AssetDelivery;
import io.wcm.testing.mock.aem.junit.AemContext;
import org.apache.sling.commons.mime.MimeTypeService;
import org.apache.sling.models.factory.ModelFactory;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

@RunWith(MockitoJUnitRunner.class)
public class AssetDeliveryRenditionDispatcherImplTest {
@Rule
public AemContext ctx = new AemContext();

@Mock
MimeTypeService mimeTypeService;

@Mock
DownloadTarget downloadTarget;

@Mock
ModelFactory modelFactory;

@Mock
AssetDelivery assetDelivery;

@Before
public void setUp() throws Exception {
ctx.load().json(getClass().getResourceAsStream("AssetDeliveryRenditionDispatcherImplTest.json"), "/content");

RequireAemMock.setAem(ctx,
RequireAem.Distribution.CLOUD_READY,
RequireAem.ServiceType.AUTHOR);

ctx.addModelsForClasses(AssetModelImpl.class);

MockAssetModels.mockModelFactory(ctx, modelFactory, "/content/dam/test.png");
MockAssetModels.mockModelFactory(ctx, modelFactory, "/content/dam/test.oft");

ctx.registerService(ModelFactory.class, modelFactory, org.osgi.framework.Constants.SERVICE_RANKING,
Integer.MAX_VALUE);


ctx.registerService(AssetDelivery.class, assetDelivery);
ctx.registerService(MimeTypeService.class, mimeTypeService);
ctx.registerService(ExpressionEvaluator.class, new ExpressionEvaluatorImpl());

ctx.registerInjectActivateService(new AssetRenditionsImpl());

ctx.registerInjectActivateService(new AssetDeliveryRenditionDispatcherImpl(), "rendition.mappings", new String[]{"test=foo"});
}

@Test
public void testAccepts_True() throws Exception {
ctx.currentResource("/content/dam/test.png");
AssetModel assetModel = ctx.request().adaptTo(AssetModel.class);

AssetRenditionDispatcher dispatcher = ctx.getService(AssetRenditionDispatcher.class);

assertTrue("PNG should be accepted", dispatcher.accepts(assetModel, "test"));
}

@Test
public void testAccepts_False() throws Exception {
ctx.currentResource("/content/dam/test.oft");
AssetModel assetModel = ctx.request().adaptTo(AssetModel.class);

AssetRenditionDispatcher dispatcher = ctx.getService(AssetRenditionDispatcher.class);

assertFalse("OFT should not be accepted", dispatcher.accepts(assetModel, "test"));
}


@Test
public void testAccepts_NoDcFormat_False() {
ctx.currentResource("/content/dam/test.no-dc-format");
AssetModel assetModel = ctx.request().adaptTo(AssetModel.class);

AssetRenditionDispatcher dispatcher = ctx.getService(AssetRenditionDispatcher.class);

assertFalse("Missing dc:format should not be accepted", dispatcher.accepts(assetModel, "test"));
}


@Test
public void testAccepts_BlankDcFormat_False() {
ctx.currentResource("/content/dam/test.blank-dc-format");
AssetModel assetModel = ctx.request().adaptTo(AssetModel.class);

AssetRenditionDispatcher dispatcher = ctx.getService(AssetRenditionDispatcher.class);

assertFalse("Blank dc:format should not be accepted", dispatcher.accepts(assetModel, "test"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"dam": {
"test.png": {
"jcr:primaryType": "dam:Asset",
"jcr:content": {
"jcr:primaryType": "nt:unstructured",
"metadata": {
"jcr:primaryType": "nt:unstructured",
"dc:format": "image/png"
},
"renditions": {
"jcr:primaryType": "nt:unstructured",
"cq5dam.web.1280.1280.png": {
"jcr:primaryType": "nt:file",
"jcr:content": {
"jcr:primaryType": "oak:Resource",
"jcr:mimeType": "image/png"
}
}
}
}
},
"test.oft": {
"jcr:primaryType": "dam:Asset",
"jcr:content": {
"jcr:primaryType": "nt:unstructured",
"metadata": {
"jcr:primaryType": "nt:unstructured",
"dc:format": "application/vnd.oasis.opendocument.text"
},
"renditions": {
"jcr:primaryType": "nt:unstructured",
"cq5dam.web.1280.1280.png": {
"jcr:primaryType": "nt:file",
"jcr:content": {
"jcr:primaryType": "oak:Resource",
"jcr:mimeType": "image/png"
}
}
}
}
},
"test.no-dc-format": {
"jcr:primaryType": "dam:Asset",
"jcr:content": {
"jcr:primaryType": "nt:unstructured",
"metadata": {
"jcr:primaryType": "nt:unstructured"
},
"renditions": {
"jcr:primaryType": "nt:unstructured",
"cq5dam.web.1280.1280.png": {
"jcr:primaryType": "nt:file",
"jcr:content": {
"jcr:primaryType": "oak:Resource",
"jcr:mimeType": "image/png"
}
}
}
}
},
"test.blank-dc-format": {
"jcr:primaryType": "dam:Asset",
"jcr:content": {
"jcr:primaryType": "nt:unstructured",
"metadata": {
"jcr:primaryType": "nt:unstructured",
"dc:format": " "
},
"renditions": {
"jcr:primaryType": "nt:unstructured",
"cq5dam.web.1280.1280.png": {
"jcr:primaryType": "nt:file",
"jcr:content": {
"jcr:primaryType": "oak:Resource",
"jcr:mimeType": "image/png"
}
}
}
}
}
}
}
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,12 @@ Bundle-DocURL: https://opensource.adobe.com/asset-share-commons/
the ones for AEM on prem are always built -->
<profile>
<id>cloud</id>
<activation>
<activeByDefault>false</activeByDefault>
<property>
<name>env.CM_BUILD</name>
</property>
</activation>
<modules>
<module>core.cloud</module>
</modules>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,14 @@
style="height:${height @ context = 'styleToken'}px;"
controls controlsList="nodownload"
data-sly-test.height="${properties['height']}">
<source src="${video.src}"
type="video/webm"/>
<source src="${video.src}"
type="video/mp4"/>

<source src="${video.src}" />
</video>

<!--/* Video element without a max height */-->
<video class="ui centered image cmp-image"
poster="${properties['posterImage']}" controls controlsList="nodownload"
data-sly-test="${!height}">
<source src="${video.src}"
type="video/webm"/>
<source src="${video.src}"
type="video/mp4" />
<source src="${video.src}"/>
</video>

</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#base=js

scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
document.addEventListener('DOMContentLoaded', function() {
const images = document.querySelectorAll('img[data-asset-share-missing-image]');
images.forEach(img => {
img.onerror = function() {
this.src = this.getAttribute('data-asset-share-missing-image');
};
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
class="cmp-image--card"
width="237px"
height="175px"
data-asset-share-missing-image="${properties['missingImage']}"
alt="${asset.properties['title']}"/>
</a>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<a class="cmp-card__link" href="${assetDetails.url @ suffix = asset.path}">
<img src="${asset.properties['rendition?name=card'] || properties['missingImage'] @ context = 'attribute'}"
class="cmp-card__image"
data-asset-share-missing-image="${properties['missingImage']}"
alt="${asset.properties['title']}"/>
</a>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<td class="image">
<a href="${assetDetails.fullUrl}"><img src="${asset.properties['rendition?name=list'] || properties['missingImage'] @ context = 'attribute'}"
width="98px"
data-assets-share-missing-image="${properties['missingImage']}"
alt="${asset.properties['title']}"/></a>
</td>
<td class="header">
Expand Down

0 comments on commit e4f452b

Please sign in to comment.