1
+ using System . Diagnostics . CodeAnalysis ;
1
2
using Microsoft . Extensions . Configuration ;
2
3
using Microsoft . Extensions . Options ;
3
4
using Umbraco . Cms . Core . Composing ;
@@ -14,30 +15,43 @@ public sealed class AzureBlobComposer : IComposer
14
15
/// <inheritdoc />
15
16
public void Compose ( IUmbracoBuilder builder )
16
17
{
17
- ArgumentNullException . ThrowIfNull ( builder ) ;
18
+ // Configure Azure Blob Storage
19
+ if ( TryGetOptions ( out AzureBlobOptions ? azureBlobOptions ) )
20
+ {
21
+ builder . AddAzureBlobMediaFileSystem ( options =>
22
+ {
23
+ options . ConnectionString = azureBlobOptions . ConnectionString ;
24
+ options . ContainerName = azureBlobOptions . ContainerName ;
25
+ } ) ;
26
+ }
27
+ }
18
28
19
- var configuration = new ConfigurationBuilder ( )
29
+ /// <summary>
30
+ /// Gets the validated Azure Blob Storage options from the environment variables.
31
+ /// </summary>
32
+ /// <param name="options">The options.</param>
33
+ /// <returns>
34
+ /// <c>true</c> if the environment variables contains valid Azure Blob Storage options; otherwise, <c>false</c>.
35
+ /// </returns>
36
+ internal static bool TryGetOptions ( [ MaybeNullWhen ( false ) ] out AzureBlobOptions options )
37
+ {
38
+ IConfigurationRoot configuration = new ConfigurationBuilder ( )
20
39
. AddEnvironmentVariables ( "Umbraco:Cloud:" )
21
40
. Build ( ) ;
22
41
23
42
// Get options and manually validate (no need to add them to the service collection)
24
- var azureBlobOptions = configuration . GetSection ( "Storage:AzureBlob" ) . Get < AzureBlobOptions > ( ) ;
25
- if ( azureBlobOptions == null )
43
+ options = configuration . GetSection ( "Storage:AzureBlob" ) . Get < AzureBlobOptions > ( ) ;
44
+ if ( options == null )
26
45
{
27
- return ;
46
+ return false ;
28
47
}
29
48
30
- var validateResult = new DataAnnotationValidateOptions < AzureBlobOptions > ( null ) . Validate ( null , azureBlobOptions ) ;
49
+ ValidateOptionsResult validateResult = new DataAnnotationValidateOptions < AzureBlobOptions > ( null ) . Validate ( null , options ) ;
31
50
if ( validateResult . Failed )
32
51
{
33
- return ;
52
+ return false ;
34
53
}
35
54
36
- // Configure Azure Blob Storage
37
- builder . AddAzureBlobMediaFileSystem ( options =>
38
- {
39
- options . ConnectionString = azureBlobOptions . ConnectionString ;
40
- options . ContainerName = azureBlobOptions . ContainerName ;
41
- } ) ;
55
+ return true ;
42
56
}
43
57
}
0 commit comments