2021-07-06 15:09:56 -06:00
|
|
|
using System.Linq;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Configuration.Models;
|
|
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
using Umbraco.Cms.Core.PropertyEditors;
|
2016-02-11 15:54:00 +01:00
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
namespace Umbraco.Extensions
|
2016-02-11 15:54:00 +01:00
|
|
|
{
|
2016-10-20 12:04:30 +02:00
|
|
|
public static class MediaExtensions
|
2016-02-11 15:54:00 +01:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
2020-10-05 20:48:38 +02:00
|
|
|
/// Gets the URL of a media item.
|
2016-02-11 15:54:00 +01:00
|
|
|
/// </summary>
|
2020-02-19 16:37:00 +11:00
|
|
|
public static string GetUrl(this IMedia media, string propertyAlias, MediaUrlGeneratorCollection mediaUrlGenerators)
|
2016-02-11 15:54:00 +01:00
|
|
|
{
|
2021-07-07 14:36:52 -06:00
|
|
|
if (media.TryGetMediaPath(propertyAlias, mediaUrlGenerators, out var mediaPath))
|
2021-02-18 11:06:02 +01:00
|
|
|
{
|
2021-07-07 14:36:52 -06:00
|
|
|
return mediaPath;
|
2016-02-11 15:54:00 +01:00
|
|
|
}
|
2016-10-20 12:04:30 +02:00
|
|
|
|
2016-02-11 15:54:00 +01:00
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-10-05 20:48:38 +02:00
|
|
|
/// Gets the URLs of a media item.
|
2016-02-11 15:54:00 +01:00
|
|
|
/// </summary>
|
2020-08-20 22:18:50 +01:00
|
|
|
public static string[] GetUrls(this IMedia media, ContentSettings contentSettings, MediaUrlGeneratorCollection mediaUrlGenerators)
|
2021-07-06 15:09:56 -06:00
|
|
|
=> contentSettings.Imaging.AutoFillImageProperties
|
2020-02-19 16:37:00 +11:00
|
|
|
.Select(field => media.GetUrl(field.Alias, mediaUrlGenerators))
|
2016-10-20 12:04:30 +02:00
|
|
|
.Where(link => string.IsNullOrWhiteSpace(link) == false)
|
|
|
|
|
.ToArray();
|
2016-02-11 15:54:00 +01:00
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
}
|