Last active
April 29, 2024 15:34
-
-
Save davidknipe/7f4e1fb23ccc6b12b17c5d68a5597338 to your computer and use it in GitHub Desktop.
Optimizely DAM alt text helper for Optimizely CMS 12
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
using EPiServer.Cms.WelcomeIntegration.Core; | |
using EPiServer.Cms.WelcomeIntegration.Core.Internal; | |
using EPiServer.Cms.WelcomeIntegration.UI; | |
using EPiServer.Cms.WelcomeIntegration.UI.Internal; | |
using Microsoft.Extensions.Caching.Memory; | |
using Microsoft.Extensions.Options; | |
using Optimizely.Cmp.Client; | |
namespace OptimzelyDamHelpers; | |
// Add following to Startup.cs in ConfigureServices(IServiceCollection services) | |
// | |
// services.AddSingleton<IDamAltTextHelper, DamAltTextHelper>(); | |
// | |
public interface IDamAltTextHelper | |
{ | |
string GetAltText(DAMImageAsset damImageAsset); | |
string GetAltText(ContentReference damContentReference); | |
} | |
public class DamAltTextHelper : IDamAltTextHelper | |
{ | |
private readonly IDAMAssetIdentityResolver _damAssetIdResolver; | |
private readonly IDAMAssetMetadataService _damMetadataService; | |
private readonly ICmpClient _cmpClient; | |
private readonly IMemoryCache _cache; | |
private readonly IOptions<DAMUiOptions> _damUiOptions; | |
private const string AltTextKey = "alt_text"; | |
public DamAltTextHelper(IDAMAssetIdentityResolver damAssetIdResolver, IDAMAssetMetadataService damMetadataService, ICmpClient cmpClient, IMemoryCache cache, IOptions<DAMUiOptions> damUiOptions) | |
{ | |
_damAssetIdResolver = damAssetIdResolver; | |
_damMetadataService = damMetadataService; | |
_cmpClient = cmpClient; | |
_cache = cache; | |
_damUiOptions = damUiOptions; | |
} | |
public string GetAltText(DAMImageAsset damImageAsset) | |
{ | |
return GetAltText(damImageAsset.ContentLink); | |
} | |
public string GetAltText(ContentReference damContentReference) | |
{ | |
// If this isn't an asset selected from the DAM, ignore | |
if (damContentReference == null || damContentReference.ProviderName != "dam") | |
return string.Empty; | |
// Attempt to retrieve resolve the asset identity | |
var assetIdentity = _damAssetIdResolver.Get(damContentReference); | |
if (assetIdentity == null) return string.Empty; | |
// Check if the Guid can be retrieved | |
var assetId = assetIdentity.GetAssetId(); | |
if (!Guid.TryParse(assetId, out var assetGuid)) | |
return string.Empty; | |
// Use a service to retrieve the alt text, these requests are cached but | |
// bear in mind that this generates an API request to Optimizely CMP/DAM | |
Dictionary<string, object?> assetMetadata; | |
if (IsRendition(assetIdentity)) | |
{ | |
assetMetadata = GetRenditionMetaData(assetGuid).Result; | |
} | |
else | |
{ | |
assetMetadata = _damMetadataService.GetAssetMetadata(assetGuid).Result; | |
} | |
if (assetMetadata.Keys.Count > 0 && | |
assetMetadata.ContainsKey(AltTextKey) && | |
!string.IsNullOrEmpty(assetMetadata[AltTextKey]?.ToString())) | |
{ | |
// Return the alt text | |
return assetMetadata[AltTextKey].ToString(); | |
} | |
return string.Empty; | |
} | |
public async Task<Dictionary<string, object?>> GetRenditionMetaData(Guid id) | |
{ | |
var result = await _cache.GetOrCreateAsync(id.ToString(), async factory => | |
{ | |
var data = await _cmpClient.SendAsync<Dictionary<string, object?>>($"renditions/{id:N}", HttpMethod.Get, null); | |
factory.AbsoluteExpirationRelativeToNow = _damUiOptions.Value.AssetMetadataCacheExpiration; | |
return data; | |
}); | |
return result ?? new Dictionary<string, object?>(); | |
} | |
public bool IsRendition(DAMAssetIdentity assetIdentity) | |
{ | |
var assetEncodedId = assetIdentity.DAMAssetUri.Segments.Last(); | |
return Guid.TryParse(assetEncodedId, out _); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment