# Conflicts: # .github/workflows/codeql-analysis.yml # src/Umbraco.Core/CompositionExtensions.cs # src/Umbraco.Core/Constants-AppSettings.cs # src/Umbraco.Core/GuidUdi.cs # src/Umbraco.Core/Models/PublishedContent/IPublishedContent.cs # src/Umbraco.Core/PublishedContentExtensions.cs # src/Umbraco.Core/Routing/DefaultUrlProvider.cs # src/Umbraco.Core/Routing/UrlProvider.cs # src/Umbraco.Core/Routing/UrlProviderExtensions.cs # src/Umbraco.Core/Runtime/MainDom.cs # src/Umbraco.Core/Services/IRuntimeState.cs # src/Umbraco.Core/StringExtensions.cs # src/Umbraco.Core/Sync/ApplicationUrlHelper.cs # src/Umbraco.Core/Sync/DatabaseServerRegistrar.cs # src/Umbraco.Core/Sync/IServerRegistrar.cs # src/Umbraco.Infrastructure/Media/EmbedProviders/Instagram.cs # src/Umbraco.Infrastructure/Models/ContentBaseExtensions.cs # src/Umbraco.Infrastructure/PropertyEditors/NestedContentPropertyEditor.cs # src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/ImageCropperValue.cs # src/Umbraco.Tests/Misc/ApplicationUrlHelperTests.cs # src/Umbraco.Tests/Routing/UrlProviderTests.cs # src/Umbraco.Tests/Routing/UrlsWithNestedDomains.cs # src/Umbraco.Tests/TestHelpers/TestObjects.cs # src/Umbraco.Web.UI/Umbraco/Views/Default.cshtml # src/Umbraco.Web.UI/Views/Partials/Grid/Editors/Rte.cshtml # src/Umbraco.Web/CompositionExtensions.cs # src/Umbraco.Web/HtmlHelperBackOfficeExtensions.cs # src/Umbraco.Web/ImageCropperTemplateExtensions.cs # src/Umbraco.Web/Models/Mapping/ContentMapDefinition.cs # src/Umbraco.Web/PropertyEditors/MultiUrlPickerPropertyEditor.cs # src/Umbraco.Web/PublishedContentExtensions.cs # src/Umbraco.Web/Runtime/WebInitialComponent.cs # src/Umbraco.Web/Runtime/WebInitialComposer.cs # src/Umbraco.Web/Templates/TemplateUtilities.cs # src/Umbraco.Web/UmbracoContext.cs # src/Umbraco.Web/UmbracoHelper.cs # src/Umbraco.Web/UmbracoInjectedModule.cs
76 lines
2.5 KiB
C#
76 lines
2.5 KiB
C#
using System;
|
|
using Umbraco.Core.Models.PublishedContent;
|
|
using Umbraco.Core.PropertyEditors;
|
|
|
|
namespace Umbraco.Web.Routing
|
|
{
|
|
/// <summary>
|
|
/// Default media URL provider.
|
|
/// </summary>
|
|
public class DefaultMediaUrlProvider : IMediaUrlProvider
|
|
{
|
|
private readonly UriUtility _uriUtility;
|
|
private readonly MediaUrlGeneratorCollection _mediaPathGenerators;
|
|
|
|
public DefaultMediaUrlProvider(MediaUrlGeneratorCollection mediaPathGenerators, UriUtility uriUtility)
|
|
{
|
|
_mediaPathGenerators = mediaPathGenerators ?? throw new ArgumentNullException(nameof(mediaPathGenerators));
|
|
_uriUtility = uriUtility;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public virtual UrlInfo GetMediaUrl(IPublishedContent content,
|
|
string propertyAlias, UrlMode mode, string culture, Uri current)
|
|
{
|
|
var prop = content.GetProperty(propertyAlias);
|
|
|
|
// get the raw source value since this is what is used by IDataEditorWithMediaPath for processing
|
|
var value = prop?.GetSourceValue(culture);
|
|
if (value == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var propType = prop.PropertyType;
|
|
|
|
if (_mediaPathGenerators.TryGetMediaPath(propType.EditorAlias, value, out var path))
|
|
{
|
|
var url = AssembleUrl(path, current, mode);
|
|
return UrlInfo.Url(url.ToString(), culture);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private Uri AssembleUrl(string path, Uri current, UrlMode mode)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(path))
|
|
throw new ArgumentException($"{nameof(path)} cannot be null or whitespace", nameof(path));
|
|
|
|
// the stored path is absolute so we just return it as is
|
|
if (Uri.IsWellFormedUriString(path, UriKind.Absolute))
|
|
return new Uri(path);
|
|
|
|
Uri uri;
|
|
|
|
if (current == null)
|
|
mode = UrlMode.Relative; // best we can do
|
|
|
|
switch (mode)
|
|
{
|
|
case UrlMode.Absolute:
|
|
uri = new Uri(current?.GetLeftPart(UriPartial.Authority) + path);
|
|
break;
|
|
case UrlMode.Relative:
|
|
case UrlMode.Auto:
|
|
uri = new Uri(path, UriKind.Relative);
|
|
break;
|
|
default:
|
|
throw new ArgumentOutOfRangeException(nameof(mode));
|
|
}
|
|
|
|
return _uriUtility.MediaUriFromUmbraco(uri);
|
|
}
|
|
}
|
|
}
|