Skip to content

Commit 01bb12f

Browse files
Ensure RemoveMediaFromPath also removes customized media path
1 parent 77e2dc0 commit 01bb12f

File tree

2 files changed

+40
-20
lines changed

2 files changed

+40
-20
lines changed
Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
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;
@@ -10,48 +12,66 @@ namespace Umbraco.StorageProviders
1012
/// A <see cref="IMediaUrlProvider" /> that returns a CDN URL for a media item.
1113
/// </summary>
1214
/// <seealso cref="Umbraco.Cms.Core.Routing.DefaultMediaUrlProvider" />
13-
public class CdnMediaUrlProvider : DefaultMediaUrlProvider
15+
public sealed class CdnMediaUrlProvider : DefaultMediaUrlProvider
1416
{
15-
private bool _removeMediaFromPath;
1617
private Uri _cdnUrl;
18+
private bool _removeMediaFromPath;
19+
private string _mediaPath;
1720

1821
/// <summary>
1922
/// Creates a new instance of <see cref="CdnMediaUrlProvider" />.
2023
/// </summary>
2124
/// <param name="options">The options.</param>
25+
/// <param name="globalSettings">The global settings.</param>
26+
/// <param name="hostingEnvironment">The hosting environment.</param>
2227
/// <param name="mediaPathGenerators">The media path generators.</param>
2328
/// <param name="uriUtility">The URI utility.</param>
2429
/// <exception cref="System.ArgumentNullException">options</exception>
25-
public CdnMediaUrlProvider(IOptionsMonitor<CdnMediaUrlProviderOptions> options, MediaUrlGeneratorCollection mediaPathGenerators, UriUtility uriUtility)
30+
public CdnMediaUrlProvider(IOptionsMonitor<CdnMediaUrlProviderOptions> options, IOptionsMonitor<GlobalSettings> globalSettings, IHostingEnvironment hostingEnvironment, MediaUrlGeneratorCollection mediaPathGenerators, UriUtility uriUtility)
2631
: base(mediaPathGenerators, uriUtility)
2732
{
28-
if (options == null) throw new ArgumentNullException(nameof(options));
33+
ArgumentNullException.ThrowIfNull(options);
34+
ArgumentNullException.ThrowIfNull(globalSettings);
35+
ArgumentNullException.ThrowIfNull(hostingEnvironment);
2936

3037
_cdnUrl = options.CurrentValue.Url;
3138
_removeMediaFromPath = options.CurrentValue.RemoveMediaFromPath;
39+
_mediaPath = hostingEnvironment.ToAbsolute(globalSettings.CurrentValue.UmbracoMediaPath).TrimEnd('/');
40+
41+
options.OnChange((options, name) =>
42+
{
43+
if (name == Options.DefaultName)
44+
{
45+
_removeMediaFromPath = options.RemoveMediaFromPath;
46+
_cdnUrl = options.Url;
47+
}
48+
});
3249

33-
options.OnChange(OptionsOnChange);
50+
globalSettings.OnChange((options, name) =>
51+
{
52+
if (name == Options.DefaultName)
53+
{
54+
_mediaPath = hostingEnvironment.ToAbsolute(options.UmbracoMediaPath).TrimEnd('/');
55+
}
56+
});
3457
}
3558

3659
/// <inheritdoc />
3760
public override UrlInfo? GetMediaUrl(IPublishedContent content, string propertyAlias, UrlMode mode, string culture, Uri current)
3861
{
3962
var mediaUrl = base.GetMediaUrl(content, propertyAlias, UrlMode.Relative, culture, current);
40-
if (mediaUrl == null) return null;
41-
42-
return mediaUrl.IsUrl switch
63+
if (mediaUrl?.IsUrl == true)
4364
{
44-
false => mediaUrl,
45-
_ => UrlInfo.Url($"{_cdnUrl}/{mediaUrl.Text[(_removeMediaFromPath ? "/media/" : "/").Length..]}", culture)
46-
};
47-
}
65+
string url = mediaUrl.Text;
66+
if (_removeMediaFromPath && url.StartsWith(_mediaPath, StringComparison.OrdinalIgnoreCase))
67+
{
68+
url = url[_mediaPath.Length..];
69+
}
4870

49-
private void OptionsOnChange(CdnMediaUrlProviderOptions options, string name)
50-
{
51-
if (name != Options.DefaultName) return;
71+
return UrlInfo.Url(_cdnUrl + url, culture);
72+
}
5273

53-
_removeMediaFromPath = options.RemoveMediaFromPath;
54-
_cdnUrl = options.Url;
74+
return mediaUrl;
5575
}
5676
}
5777
}

src/Umbraco.StorageProviders/CdnMediaUrlProviderOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Umbraco.StorageProviders
66
/// <summary>
77
/// The CDN media URL provider options.
88
/// </summary>
9-
public class CdnMediaUrlProviderOptions
9+
public sealed class CdnMediaUrlProviderOptions
1010
{
1111
/// <summary>
1212
/// Gets or sets the CDN media root URL.
@@ -18,10 +18,10 @@ public class CdnMediaUrlProviderOptions
1818
public Uri Url { get; set; } = null!;
1919

2020
/// <summary>
21-
/// Gets or sets a value indicating whether to remove <c>/media/</c> from the path, defaults to <c>true</c>.
21+
/// Gets or sets a value indicating whether to remove the <see cref="Umbraco.Cms.Core.Configuration.Models.GlobalSettings.UmbracoMediaPath"/> from the path, defaults to <c>true</c>.
2222
/// </summary>
2323
/// <value>
24-
/// <c>true</c> if <c>/media/</c> needs to be removed from the path; otherwise, <c>false</c>.
24+
/// <c>true</c> if the media path needs to be removed from the path; otherwise, <c>false</c>.
2525
/// </value>
2626
public bool RemoveMediaFromPath { get; set; } = true;
2727
}

0 commit comments

Comments
 (0)