Skip to content

Commit 350adf0

Browse files
Use same condition for composing
1 parent e523770 commit 350adf0

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed
Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics.CodeAnalysis;
12
using Microsoft.Extensions.Configuration;
23
using Microsoft.Extensions.Options;
34
using Umbraco.Cms.Core.Composing;
@@ -14,30 +15,43 @@ public sealed class AzureBlobComposer : IComposer
1415
/// <inheritdoc />
1516
public void Compose(IUmbracoBuilder builder)
1617
{
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+
}
1828

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()
2039
.AddEnvironmentVariables("Umbraco:Cloud:")
2140
.Build();
2241

2342
// 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)
2645
{
27-
return;
46+
return false;
2847
}
2948

30-
var validateResult = new DataAnnotationValidateOptions<AzureBlobOptions>(null).Validate(null, azureBlobOptions);
49+
ValidateOptionsResult validateResult = new DataAnnotationValidateOptions<AzureBlobOptions>(null).Validate(null, options);
3150
if (validateResult.Failed)
3251
{
33-
return;
52+
return false;
3453
}
3554

36-
// Configure Azure Blob Storage
37-
builder.AddAzureBlobMediaFileSystem(options =>
38-
{
39-
options.ConnectionString = azureBlobOptions.ConnectionString;
40-
options.ContainerName = azureBlobOptions.ContainerName;
41-
});
55+
return true;
4256
}
4357
}

src/Umbraco.Cloud.StorageProviders.AzureBlob.Core/Umbraco.Cloud.StorageProviders.AzureBlob.Core.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@
77
<ItemGroup>
88
<PackageReference Include="Umbraco.StorageProviders.AzureBlob" />
99
</ItemGroup>
10+
<ItemGroup>
11+
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
12+
<_Parameter1>Umbraco.Cloud.StorageProviders.AzureBlob.ImageSharp</_Parameter1>
13+
</AssemblyAttribute>
14+
</ItemGroup>
1015
</Project>

src/Umbraco.Cloud.StorageProviders.AzureBlob.ImageSharp/AzureBlobImageSharpComposer.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Umbraco.Cloud.StorageProviders.AzureBlob.Core;
22
using Umbraco.Cms.Core.Composing;
33
using Umbraco.Cms.Core.DependencyInjection;
4-
using Umbraco.StorageProviders.AzureBlob.IO;
54

65
namespace Umbraco.Cloud.StorageProviders.AzureBlob.ImageSharp;
76

@@ -15,11 +14,9 @@ public sealed class AzureBlobImageSharpComposer : IComposer
1514
/// <inheritdoc />
1615
public void Compose(IUmbracoBuilder builder)
1716
{
18-
ArgumentNullException.ThrowIfNull(builder);
19-
20-
if (builder.Services.Any(x => x.ServiceType == typeof(IAzureBlobFileSystemProvider)))
17+
// Configure ImageSharp support using Azure Blob Storage
18+
if (AzureBlobComposer.TryGetOptions(out _))
2119
{
22-
// Configure ImageSharp support using Azure Blob Storage
2320
builder.AddAzureBlobImageSharpCache();
2421
}
2522
}

0 commit comments

Comments
 (0)