Files
Umbraco-CMS/src/Umbraco.Web/Routing/DefaultMediaUrlProvider.cs

70 lines
2.2 KiB
C#
Raw Normal View History

2019-04-15 15:57:35 +02:00
using System;
using Umbraco.Core;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors.ValueConverters;
namespace Umbraco.Web.Routing
{
/// <summary>
/// Default media url provider.
/// </summary>
public class DefaultMediaUrlProvider : IMediaUrlProvider
{
/// <inheritdoc />
public virtual UrlInfo GetMediaUrl(UmbracoContext umbracoContext, IPublishedContent content,
string propertyAlias,
2019-04-24 09:32:49 +02:00
UrlMode mode, string culture, Uri current)
2019-04-15 15:57:35 +02:00
{
var prop = content.GetProperty(propertyAlias);
var value = prop?.GetValue(culture);
if (value == null)
{
return null;
}
2019-04-23 16:23:06 +02:00
var propType = prop.PropertyType;
2019-04-15 15:57:35 +02:00
string path = null;
switch (propType.EditorAlias)
{
case Constants.PropertyEditors.Aliases.UploadField:
path = value.ToString();
break;
case Constants.PropertyEditors.Aliases.ImageCropper:
//get the url from the json format
2019-04-23 16:23:06 +02:00
path = value is ImageCropperValue stronglyTyped ? stronglyTyped.Src : value.ToString();
2019-04-15 15:57:35 +02:00
break;
}
2019-04-23 16:23:06 +02:00
var url = AssembleUrl(path, current, mode);
return url == null ? null : UrlInfo.Url(url.ToString(), culture);
2019-04-15 15:57:35 +02:00
}
2019-04-24 09:32:49 +02:00
private Uri AssembleUrl(string path, Uri current, UrlMode mode)
2019-04-15 15:57:35 +02:00
{
if (string.IsNullOrEmpty(path))
return null;
Uri uri;
if (current == null)
2019-04-24 09:32:49 +02:00
mode = UrlMode.Relative; // best we can do
2019-04-15 15:57:35 +02:00
switch (mode)
{
2019-04-24 09:32:49 +02:00
case UrlMode.Absolute:
2019-04-15 15:57:35 +02:00
uri = new Uri(current?.GetLeftPart(UriPartial.Authority) + path);
break;
2019-04-24 09:32:49 +02:00
case UrlMode.Relative:
case UrlMode.Auto:
2019-04-15 15:57:35 +02:00
uri = new Uri(path, UriKind.Relative);
break;
default:
throw new ArgumentOutOfRangeException(nameof(mode));
}
2019-04-23 16:23:06 +02:00
return UriUtility.MediaUriFromUmbraco(uri);
2019-04-15 15:57:35 +02:00
}
}
}