Skip to content

Commit

Permalink
Merge pull request #309 from stefannikolei/stefannikolei/nullable/azure
Browse files Browse the repository at this point in the history
Remove nullable disable from azure
  • Loading branch information
JimBobSquarePants authored Mar 6, 2023
2 parents e852621 + bae419e commit 135c8b1
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable disable

using Azure;
using Azure.Storage.Blobs;
Expand Down Expand Up @@ -31,7 +30,7 @@ public AzureBlobStorageCache(IOptions<AzureBlobStorageCacheOptions> cacheOptions
}

/// <inheritdoc/>
public async Task<IImageCacheResolver> GetAsync(string key)
public async Task<IImageCacheResolver?> GetAsync(string key)
{
BlobClient blob = this.container.GetBlobClient(key);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable disable

namespace SixLabors.ImageSharp.Web.Caching.Azure;

/// <summary>
/// Configuration options for the <see cref="AzureBlobStorageCache"/>.
/// </summary>
public class AzureBlobStorageCacheOptions : IAzureBlobContainerClientOptions
public class AzureBlobStorageCacheOptions
{
/// <inheritdoc/>
public string ConnectionString { get; set; }
/// <summary>
/// Gets or sets the Azure Blob Storage connection string.
/// <see href="https://docs.microsoft.com/en-us/azure/storage/common/storage-configure-connection-string."/>
/// </summary>
public string ConnectionString { get; set; } = null!;

/// <inheritdoc/>
public string ContainerName { get; set; }
/// <summary>
/// Gets or sets the Azure Blob Storage container name.
/// Must conform to Azure Blob Storage container naming guidlines.
/// <see href="https://docs.microsoft.com/en-us/rest/api/storageservices/naming-and-referencing-containers--blobs--and-metadata#container-names"/>
/// </summary>
public string ContainerName { get; set; } = null!;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable disable

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

/// <summary>
/// The blob storage options.
/// </summary>
private readonly AzureBlobStorageImageProviderOptions storageOptions;

/// <summary>
/// Contains various helper methods based on the current configuration.
/// </summary>
Expand All @@ -40,7 +34,7 @@ private readonly Dictionary<string, BlobContainerClient> containers
/// <summary>
/// A match function used by the resolver to identify itself as the correct resolver to use.
/// </summary>
private Func<HttpContext, bool> match;
private Func<HttpContext, bool>? match;

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

this.storageOptions = storageOptions.Value;
this.formatUtilities = formatUtilities;

foreach (AzureBlobContainerClientOptions container in this.storageOptions.BlobContainers)
foreach (AzureBlobContainerClientOptions container in storageOptions.Value.BlobContainers)
{
this.containers.Add(
container.ContainerName,
container.ContainerName!,
new BlobContainerClient(container.ConnectionString, container.ContainerName));
}
}
Expand All @@ -75,19 +68,25 @@ public Func<HttpContext, bool> Match
}

/// <inheritdoc/>
public async Task<IImageResolver> GetAsync(HttpContext context)
public async Task<IImageResolver?> GetAsync(HttpContext context)
{
// Strip the leading slash and container name from the HTTP request path and treat
// the remaining path string as the blob name.
// Path has already been correctly parsed before here.
string containerName = string.Empty;
BlobContainerClient container = null;
BlobContainerClient? container = null;

// We want an exact match here to ensure that container names starting with
// the same prefix are not mixed up.
string path = context.Request.Path.Value.TrimStart(SlashChars);
string? path = context.Request.Path.Value?.TrimStart(SlashChars);

if (path is null)
{
return null;
}

int index = path.IndexOfAny(SlashChars);
string nameToMatch = index != -1 ? path.Substring(0, index) : path;
string nameToMatch = index != -1 ? path[..index] : path;

foreach (string key in this.containers.Keys)
{
Expand Down Expand Up @@ -131,7 +130,13 @@ private bool IsMatch(HttpContext context)
{
// Only match loosly here for performance.
// Path matching conflicts should be dealt with by configuration.
string path = context.Request.Path.Value.TrimStart(SlashChars);
string? path = context.Request.Path.Value?.TrimStart(SlashChars);

if (path is null)
{
return false;
}

foreach (string container in this.containers.Keys)
{
if (path.StartsWith(container, StringComparison.OrdinalIgnoreCase))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable disable

namespace SixLabors.ImageSharp.Web.Providers.Azure;

Expand All @@ -18,11 +17,18 @@ public class AzureBlobStorageImageProviderOptions
/// <summary>
/// Represents a single Azure Blob Storage connection and container.
/// </summary>
public class AzureBlobContainerClientOptions : IAzureBlobContainerClientOptions
public class AzureBlobContainerClientOptions
{
/// <inheritdoc/>
public string ConnectionString { get; set; }
/// <summary>
/// Gets or sets the Azure Blob Storage connection string.
/// <see href="https://docs.microsoft.com/en-us/azure/storage/common/storage-configure-connection-string."/>
/// </summary>
public string? ConnectionString { get; set; }

/// <inheritdoc/>
public string ContainerName { get; set; }
/// <summary>
/// Gets or sets the Azure Blob Storage container name.
/// Must conform to Azure Blob Storage container naming guidlines.
/// <see href="https://docs.microsoft.com/en-us/rest/api/storageservices/naming-and-referencing-containers--blobs--and-metadata#container-names"/>
/// </summary>
public string? ContainerName { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable disable

using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable disable

using System.Net.Http.Headers;
using Azure.Storage.Blobs;
Expand Down Expand Up @@ -32,7 +31,7 @@ public async Task<ImageMetadata> GetMetaDataAsync()
// Try to parse the max age from the source. If it's not zero then we pass it along
// to set the cache control headers for the response.
TimeSpan maxAge = TimeSpan.MinValue;
if (CacheControlHeaderValue.TryParse(properties.CacheControl, out CacheControlHeaderValue cacheControl))
if (CacheControlHeaderValue.TryParse(properties.CacheControl, out CacheControlHeaderValue? cacheControl))
{
// Weirdly passing null to TryParse returns true.
if (cacheControl?.MaxAge.HasValue == true)
Expand Down

0 comments on commit 135c8b1

Please sign in to comment.