1
1
using System ;
2
2
using Microsoft . Extensions . Options ;
3
+ using Umbraco . Cms . Core . Configuration . Models ;
4
+ using Umbraco . Cms . Core . Hosting ;
3
5
using Umbraco . Cms . Core . Models . PublishedContent ;
4
6
using Umbraco . Cms . Core . PropertyEditors ;
5
7
using Umbraco . Cms . Core . Routing ;
8
+ using Umbraco . Extensions ;
6
9
7
10
namespace Umbraco . StorageProviders . AzureBlob
8
11
{
@@ -14,45 +17,89 @@ public class CdnMediaUrlProvider : DefaultMediaUrlProvider
14
17
{
15
18
private bool _removeMediaFromPath ;
16
19
private Uri _cdnUrl ;
20
+ private string _mediaPath ;
17
21
18
22
/// <summary>
19
- /// Creates a new instance of <see cref="CdnMediaUrlProvider" /> .
23
+ /// Initializes a new instance of the <see cref="CdnMediaUrlProvider"/> class .
20
24
/// </summary>
21
25
/// <param name="options">The options.</param>
26
+ /// <param name="globalSettings">The global settings.</param>
27
+ /// <param name="hostingEnvironment">The hosting environment.</param>
22
28
/// <param name="mediaPathGenerators">The media path generators.</param>
23
29
/// <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 )
27
63
: base ( mediaPathGenerators , uriUtility )
28
64
{
29
65
if ( options == null ) throw new ArgumentNullException ( nameof ( options ) ) ;
30
66
31
67
_cdnUrl = options . CurrentValue . Url ;
32
68
_removeMediaFromPath = options . CurrentValue . RemoveMediaFromPath ;
69
+ _mediaPath = mediaPath ;
33
70
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
+ } ) ;
35
79
}
36
80
37
81
/// <inheritdoc />
38
82
public override UrlInfo ? GetMediaUrl ( IPublishedContent content , string propertyAlias , UrlMode mode , string culture , Uri current )
39
83
{
40
84
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 )
44
86
{
45
- false => mediaUrl ,
46
- _ => UrlInfo . Url ( $ "{ _cdnUrl } /{ mediaUrl . Text [ ( _removeMediaFromPath ? "/media/" : "/" ) . Length ..] } ", culture )
47
- } ;
48
- }
87
+ string url = mediaUrl . Text ;
49
88
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
+ }
53
101
54
- _removeMediaFromPath = options . RemoveMediaFromPath ;
55
- _cdnUrl = options . Url ;
102
+ return mediaUrl ;
56
103
}
57
104
}
58
105
}
0 commit comments