Skip to content

Commit 85e76ac

Browse files
committed
Remove nullable disable from azure
* Removed all Nullable disable from the azure providers * Removed IAzureBlobContainerClientOptions because it wasn't used just for declaration * Added convenience DI methods for Adding the cache or image provider. Leaving out the initialization was on purpose to not run that code on startup because it could throw an exception so the app won't start..
1 parent e852621 commit 85e76ac

8 files changed

Lines changed: 60 additions & 52 deletions

src/ImageSharp.Web.Providers.Azure/Caching/AzureBlobStorageCache.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
using Azure;
65
using Azure.Storage.Blobs;
@@ -31,7 +30,7 @@ public AzureBlobStorageCache(IOptions<AzureBlobStorageCacheOptions> cacheOptions
3130
}
3231

3332
/// <inheritdoc/>
34-
public async Task<IImageCacheResolver> GetAsync(string key)
33+
public async Task<IImageCacheResolver?> GetAsync(string key)
3534
{
3635
BlobClient blob = this.container.GetBlobClient(key);
3736

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
namespace SixLabors.ImageSharp.Web.Caching.Azure;
65

76
/// <summary>
87
/// Configuration options for the <see cref="AzureBlobStorageCache"/>.
98
/// </summary>
10-
public class AzureBlobStorageCacheOptions : IAzureBlobContainerClientOptions
9+
public class AzureBlobStorageCacheOptions
1110
{
1211
/// <inheritdoc/>
13-
public string ConnectionString { get; set; }
12+
public string ConnectionString { get; set; } = null!;
1413

1514
/// <inheritdoc/>
16-
public string ContainerName { get; set; }
15+
public string ContainerName { get; set; } = null!;
1716
}

src/ImageSharp.Web.Providers.Azure/IAzureBlobContainerClientOptions.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/ImageSharp.Web.Providers.Azure/Providers/AzureBlobStorageImageProvider.cs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
using Azure.Storage.Blobs;
65
using Microsoft.AspNetCore.Http;
@@ -27,11 +26,6 @@ public class AzureBlobStorageImageProvider : IImageProvider
2726
private readonly Dictionary<string, BlobContainerClient> containers
2827
= new();
2928

30-
/// <summary>
31-
/// The blob storage options.
32-
/// </summary>
33-
private readonly AzureBlobStorageImageProviderOptions storageOptions;
34-
3529
/// <summary>
3630
/// Contains various helper methods based on the current configuration.
3731
/// </summary>
@@ -40,7 +34,7 @@ private readonly Dictionary<string, BlobContainerClient> containers
4034
/// <summary>
4135
/// A match function used by the resolver to identify itself as the correct resolver to use.
4236
/// </summary>
43-
private Func<HttpContext, bool> match;
37+
private Func<HttpContext, bool>? match;
4438

4539
/// <summary>
4640
/// Initializes a new instance of the <see cref="AzureBlobStorageImageProvider"/> class.
@@ -53,13 +47,12 @@ public AzureBlobStorageImageProvider(
5347
{
5448
Guard.NotNull(storageOptions, nameof(storageOptions));
5549

56-
this.storageOptions = storageOptions.Value;
5750
this.formatUtilities = formatUtilities;
5851

59-
foreach (AzureBlobContainerClientOptions container in this.storageOptions.BlobContainers)
52+
foreach (AzureBlobContainerClientOptions container in storageOptions.Value.BlobContainers)
6053
{
6154
this.containers.Add(
62-
container.ContainerName,
55+
container.ContainerName!,
6356
new BlobContainerClient(container.ConnectionString, container.ContainerName));
6457
}
6558
}
@@ -75,19 +68,25 @@ public Func<HttpContext, bool> Match
7568
}
7669

7770
/// <inheritdoc/>
78-
public async Task<IImageResolver> GetAsync(HttpContext context)
71+
public async Task<IImageResolver?> GetAsync(HttpContext context)
7972
{
8073
// Strip the leading slash and container name from the HTTP request path and treat
8174
// the remaining path string as the blob name.
8275
// Path has already been correctly parsed before here.
8376
string containerName = string.Empty;
84-
BlobContainerClient container = null;
77+
BlobContainerClient? container = null;
8578

8679
// We want an exact match here to ensure that container names starting with
8780
// the same prefix are not mixed up.
88-
string path = context.Request.Path.Value.TrimStart(SlashChars);
81+
string? path = context.Request.Path.Value?.TrimStart(SlashChars);
82+
83+
if (path is null)
84+
{
85+
return null;
86+
}
87+
8988
int index = path.IndexOfAny(SlashChars);
90-
string nameToMatch = index != -1 ? path.Substring(0, index) : path;
89+
string nameToMatch = index != -1 ? path[..index] : path;
9190

9291
foreach (string key in this.containers.Keys)
9392
{
@@ -131,7 +130,13 @@ private bool IsMatch(HttpContext context)
131130
{
132131
// Only match loosly here for performance.
133132
// Path matching conflicts should be dealt with by configuration.
134-
string path = context.Request.Path.Value.TrimStart(SlashChars);
133+
string? path = context.Request.Path.Value?.TrimStart(SlashChars);
134+
135+
if (path is null)
136+
{
137+
return false;
138+
}
139+
135140
foreach (string container in this.containers.Keys)
136141
{
137142
if (path.StartsWith(container, StringComparison.OrdinalIgnoreCase))

src/ImageSharp.Web.Providers.Azure/Providers/AzureBlobStorageImageProviderOptions.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
namespace SixLabors.ImageSharp.Web.Providers.Azure;
65

@@ -18,11 +17,11 @@ public class AzureBlobStorageImageProviderOptions
1817
/// <summary>
1918
/// Represents a single Azure Blob Storage connection and container.
2019
/// </summary>
21-
public class AzureBlobContainerClientOptions : IAzureBlobContainerClientOptions
20+
public class AzureBlobContainerClientOptions
2221
{
2322
/// <inheritdoc/>
24-
public string ConnectionString { get; set; }
23+
public string? ConnectionString { get; set; }
2524

2625
/// <inheritdoc/>
27-
public string ContainerName { get; set; }
26+
public string? ContainerName { get; set; }
2827
}

src/ImageSharp.Web.Providers.Azure/Resolvers/AzureBlobStorageCacheResolver.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
using Azure.Storage.Blobs;
65
using Azure.Storage.Blobs.Models;

src/ImageSharp.Web.Providers.Azure/Resolvers/AzureBlobStorageImageResolver.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
using System.Net.Http.Headers;
65
using Azure.Storage.Blobs;
@@ -32,7 +31,7 @@ public async Task<ImageMetadata> GetMetaDataAsync()
3231
// Try to parse the max age from the source. If it's not zero then we pass it along
3332
// to set the cache control headers for the response.
3433
TimeSpan maxAge = TimeSpan.MinValue;
35-
if (CacheControlHeaderValue.TryParse(properties.CacheControl, out CacheControlHeaderValue cacheControl))
34+
if (CacheControlHeaderValue.TryParse(properties.CacheControl, out CacheControlHeaderValue? cacheControl))
3635
{
3736
// Weirdly passing null to TryParse returns true.
3837
if (cacheControl?.MaxAge.HasValue == true)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using Microsoft.Extensions.DependencyInjection;
5+
using SixLabors.ImageSharp.Web.Caching.Azure;
6+
using SixLabors.ImageSharp.Web.DependencyInjection;
7+
using SixLabors.ImageSharp.Web.Providers.Azure;
8+
9+
namespace SixLabors.ImageSharp.Web;
10+
11+
public static class SeviceCollectionExtensions
12+
{
13+
/// <summary>
14+
/// Adds AzureBlobStorage as Cache provider.
15+
/// </summary>
16+
/// <param name="imageSharpBuilder">The builder</param>
17+
/// <param name="setupAction">The setup action</param>
18+
/// <returns>An <see cref="IImageSharpBuilder"/> that can be used to further configure the ImageSharp services.</returns>
19+
public static IImageSharpBuilder AddAzureBlobCache(this IImageSharpBuilder imageSharpBuilder, Action<AzureBlobStorageCacheOptions> setupAction) =>
20+
imageSharpBuilder.Configure(setupAction)
21+
.SetCache<AzureBlobStorageCache>();
22+
23+
/// <summary>
24+
/// Adds the AzureBlobStorageImageProvider.
25+
/// </summary>
26+
/// <param name="imageSharpBuilder">The builder</param>
27+
/// <param name="setupAction">The setup action</param>
28+
/// <returns>An <see cref="IImageSharpBuilder"/> that can be used to further configure the ImageSharp services.</returns>
29+
public static IImageSharpBuilder AddAzureBlobImageProvider(this IImageSharpBuilder imageSharpBuilder, Action<AzureBlobStorageImageProviderOptions> setupAction) =>
30+
imageSharpBuilder.Configure(setupAction)
31+
.AddProvider<AzureBlobStorageImageProvider>();
32+
}

0 commit comments

Comments
 (0)