Skip to content

Commit ae44b73

Browse files
Merge pull request #41 from umbraco/bugfix/mediapath
Remove extra slash between CDN URL and media path (using configured UmbracoMediaPath)
2 parents a33c198 + 105dc0b commit ae44b73

File tree

1 file changed

+64
-17
lines changed

1 file changed

+64
-17
lines changed
Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System;
22
using Microsoft.Extensions.Options;
3+
using Umbraco.Cms.Core.Configuration.Models;
4+
using Umbraco.Cms.Core.Hosting;
35
using Umbraco.Cms.Core.Models.PublishedContent;
46
using Umbraco.Cms.Core.PropertyEditors;
57
using Umbraco.Cms.Core.Routing;
8+
using Umbraco.Extensions;
69

710
namespace Umbraco.StorageProviders.AzureBlob
811
{
@@ -14,45 +17,89 @@ public class CdnMediaUrlProvider : DefaultMediaUrlProvider
1417
{
1518
private bool _removeMediaFromPath;
1619
private Uri _cdnUrl;
20+
private string _mediaPath;
1721

1822
/// <summary>
19-
/// Creates a new instance of <see cref="CdnMediaUrlProvider" />.
23+
/// Initializes a new instance of the <see cref="CdnMediaUrlProvider"/> class.
2024
/// </summary>
2125
/// <param name="options">The options.</param>
26+
/// <param name="globalSettings">The global settings.</param>
27+
/// <param name="hostingEnvironment">The hosting environment.</param>
2228
/// <param name="mediaPathGenerators">The media path generators.</param>
2329
/// <param name="uriUtility">The URI utility.</param>
24-
/// <exception cref="System.ArgumentNullException">options</exception>
25-
public CdnMediaUrlProvider(IOptionsMonitor<CdnMediaUrlProviderOptions> options,
26-
MediaUrlGeneratorCollection mediaPathGenerators, UriUtility uriUtility)
30+
/// <exception cref="ArgumentNullException"><paramref name="options"/> is <c>null</c>.</exception>
31+
/// <exception cref="ArgumentNullException"><paramref name="globalSettings"/> is <c>null</c>.</exception>
32+
/// <exception cref="ArgumentNullException"><paramref name="hostingEnvironment"/> is <c>null</c>.</exception>
33+
public CdnMediaUrlProvider(IOptionsMonitor<CdnMediaUrlProviderOptions> options, IOptionsMonitor<GlobalSettings> globalSettings, IHostingEnvironment hostingEnvironment, MediaUrlGeneratorCollection mediaPathGenerators, UriUtility uriUtility)
34+
: this(options, mediaPathGenerators, uriUtility, string.Empty)
35+
{
36+
if (globalSettings == null) throw new ArgumentNullException(nameof(globalSettings));
37+
if (hostingEnvironment == null) throw new ArgumentNullException(nameof(hostingEnvironment));
38+
39+
_mediaPath = hostingEnvironment.ToAbsolute(globalSettings.CurrentValue.UmbracoMediaPath).EnsureEndsWith('/');
40+
41+
globalSettings.OnChange((options, name) =>
42+
{
43+
if (name == Options.DefaultName)
44+
{
45+
_mediaPath = hostingEnvironment.ToAbsolute(options.UmbracoMediaPath).EnsureEndsWith('/');
46+
}
47+
});
48+
}
49+
50+
/// <summary>
51+
/// Initializes a new instance of the <see cref="CdnMediaUrlProvider"/> class.
52+
/// </summary>
53+
/// <param name="options">The options.</param>
54+
/// <param name="mediaPathGenerators">The media path generators.</param>
55+
/// <param name="uriUtility">The URI utility.</param>
56+
/// <exception cref="ArgumentNullException"><paramref name="options"/> is <c>null</c>.</exception>
57+
[Obsolete("This constructor is obsolete and will be removed in a future version. Use another constructor instead.")]
58+
public CdnMediaUrlProvider(IOptionsMonitor<CdnMediaUrlProviderOptions> options, MediaUrlGeneratorCollection mediaPathGenerators, UriUtility uriUtility)
59+
: this(options, mediaPathGenerators, uriUtility, "/media/")
60+
{ }
61+
62+
private CdnMediaUrlProvider(IOptionsMonitor<CdnMediaUrlProviderOptions> options, MediaUrlGeneratorCollection mediaPathGenerators, UriUtility uriUtility, string mediaPath)
2763
: base(mediaPathGenerators, uriUtility)
2864
{
2965
if (options == null) throw new ArgumentNullException(nameof(options));
3066

3167
_cdnUrl = options.CurrentValue.Url;
3268
_removeMediaFromPath = options.CurrentValue.RemoveMediaFromPath;
69+
_mediaPath = mediaPath;
3370

34-
options.OnChange(OptionsOnChange);
71+
options.OnChange((options, name) =>
72+
{
73+
if (name == Options.DefaultName)
74+
{
75+
_removeMediaFromPath = options.RemoveMediaFromPath;
76+
_cdnUrl = options.Url;
77+
}
78+
});
3579
}
3680

3781
/// <inheritdoc />
3882
public override UrlInfo? GetMediaUrl(IPublishedContent content, string propertyAlias, UrlMode mode, string culture, Uri current)
3983
{
4084
var mediaUrl = base.GetMediaUrl(content, propertyAlias, UrlMode.Relative, culture, current);
41-
if (mediaUrl == null) return null;
42-
43-
return mediaUrl.IsUrl switch
85+
if (mediaUrl?.IsUrl == true)
4486
{
45-
false => mediaUrl,
46-
_ => UrlInfo.Url($"{_cdnUrl}/{mediaUrl.Text[(_removeMediaFromPath ? "/media/" : "/").Length..]}", culture)
47-
};
48-
}
87+
string url = mediaUrl.Text;
4988

50-
private void OptionsOnChange(CdnMediaUrlProviderOptions options, string name)
51-
{
52-
if (name != Options.DefaultName) return;
89+
int startIndex = 0;
90+
if (_removeMediaFromPath && url.StartsWith(_mediaPath, StringComparison.OrdinalIgnoreCase))
91+
{
92+
startIndex = _mediaPath.Length;
93+
}
94+
else if (url.StartsWith('/'))
95+
{
96+
startIndex = 1;
97+
}
98+
99+
return UrlInfo.Url(_cdnUrl + url[startIndex..], culture);
100+
}
53101

54-
_removeMediaFromPath = options.RemoveMediaFromPath;
55-
_cdnUrl = options.Url;
102+
return mediaUrl;
56103
}
57104
}
58105
}

0 commit comments

Comments
 (0)