2019-10-30 21:57:50 +01:00
|
|
|
|
using System.Linq;
|
2016-02-11 15:54:00 +01:00
|
|
|
|
using Umbraco.Core.Configuration.UmbracoSettings;
|
2019-10-30 21:57:50 +01:00
|
|
|
|
using Umbraco.Core.PropertyEditors;
|
2016-02-11 15:54:00 +01:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.Models
|
|
|
|
|
|
{
|
2016-10-20 12:04:30 +02:00
|
|
|
|
public static class MediaExtensions
|
2016-02-11 15:54:00 +01:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2016-10-20 12:04:30 +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
|
|
|
|
{
|
2019-02-07 12:02:47 +11:00
|
|
|
|
if (!media.Properties.TryGetValue(propertyAlias, out var property))
|
|
|
|
|
|
return string.Empty;
|
2016-10-20 12:04:30 +02:00
|
|
|
|
|
2020-02-19 16:37:00 +11:00
|
|
|
|
// TODO: would need to be adjusted to variations, when media become variants
|
|
|
|
|
|
if (mediaUrlGenerators.TryGetMediaPath(property.PropertyType.PropertyEditorAlias, property.GetValue(), out var mediaUrl))
|
|
|
|
|
|
{
|
|
|
|
|
|
return mediaUrl;
|
2016-02-11 15:54:00 +01:00
|
|
|
|
}
|
2016-10-20 12:04:30 +02:00
|
|
|
|
|
2019-01-22 18:03:39 -05:00
|
|
|
|
// Without knowing what it is, just adding a string here might not be very nice
|
2016-02-11 15:54:00 +01:00
|
|
|
|
return string.Empty;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2016-10-20 12:04:30 +02:00
|
|
|
|
/// Gets the urls of a media item.
|
2016-02-11 15:54:00 +01:00
|
|
|
|
/// </summary>
|
2020-02-19 16:37:00 +11:00
|
|
|
|
public static string[] GetUrls(this IMedia media, IContentSection contentSection, MediaUrlGeneratorCollection mediaUrlGenerators)
|
2016-02-11 15:54:00 +01:00
|
|
|
|
{
|
2016-10-20 12:04:30 +02:00
|
|
|
|
return contentSection.ImageAutoFillProperties
|
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
|
|
|
|
}
|