Skip to content

Commit 425c7b9

Browse files
Allow changing AzureBlobFileSystemImageCache container root path
1 parent 948f798 commit 425c7b9

File tree

2 files changed

+49
-25
lines changed

2 files changed

+49
-25
lines changed
Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using Azure.Storage.Blobs;
2+
using Azure.Storage.Blobs.Models;
23
using Microsoft.Extensions.Options;
34
using SixLabors.ImageSharp.Web;
45
using SixLabors.ImageSharp.Web.Caching;
56
using SixLabors.ImageSharp.Web.Resolvers;
67
using SixLabors.ImageSharp.Web.Resolvers.Azure;
8+
using Umbraco.Extensions;
79
using Umbraco.StorageProviders.AzureBlob.IO;
810

911
namespace Umbraco.StorageProviders.AzureBlob.ImageSharp;
@@ -13,29 +15,20 @@ namespace Umbraco.StorageProviders.AzureBlob.ImageSharp;
1315
/// </summary>
1416
public sealed class AzureBlobFileSystemImageCache : IImageCache
1517
{
16-
private const string CachePath = "cache/";
18+
private readonly string? _containerRootPath;
1719
private BlobContainerClient _container;
1820

1921
/// <summary>
2022
/// Initializes a new instance of the <see cref="AzureBlobFileSystemImageCache" /> class.
2123
/// </summary>
2224
/// <param name="options">The options.</param>
23-
/// <exception cref="ArgumentNullException"><paramref name="options" /> is <c>null</c>.</exception>
24-
public AzureBlobFileSystemImageCache(IOptionsMonitor<AzureBlobFileSystemOptions> options)
25-
: this(AzureBlobFileSystemOptions.MediaFileSystemName, options)
26-
{ }
27-
28-
/// <summary>
29-
/// Initializes a new instance of the <see cref="AzureBlobFileSystemImageCache"/> class.
30-
/// </summary>
3125
/// <param name="name">The name.</param>
32-
/// <param name="options">The options.</param>
33-
/// <exception cref="ArgumentNullException"><paramref name="name" /> is <c>null</c>.</exception>
26+
/// <param name="containerRootPath">The container root path.</param>
3427
/// <exception cref="ArgumentNullException"><paramref name="options" /> is <c>null</c>.</exception>
35-
public AzureBlobFileSystemImageCache(string name, IOptionsMonitor<AzureBlobFileSystemOptions> options)
28+
public AzureBlobFileSystemImageCache(IOptionsMonitor<AzureBlobFileSystemOptions> options, string name, string? containerRootPath)
3629
{
37-
ArgumentNullException.ThrowIfNull(name);
3830
ArgumentNullException.ThrowIfNull(options);
31+
ArgumentNullException.ThrowIfNull(name);
3932

4033
var fileSystemOptions = options.Get(name);
4134
_container = new BlobContainerClient(fileSystemOptions.ConnectionString, fileSystemOptions.ContainerName);
@@ -47,20 +40,33 @@ public AzureBlobFileSystemImageCache(string name, IOptionsMonitor<AzureBlobFileS
4740
_container = new BlobContainerClient(options.ConnectionString, options.ContainerName);
4841
}
4942
});
43+
44+
if (!string.IsNullOrEmpty(containerRootPath))
45+
{
46+
_containerRootPath = containerRootPath.EnsureEndsWith('/');
47+
}
5048
}
5149

5250
/// <summary>
5351
/// Initializes a new instance of the <see cref="AzureBlobFileSystemImageCache" /> class.
5452
/// </summary>
5553
/// <param name="blobContainerClient">The blob container client.</param>
54+
/// <param name="containerRootPath">The container root path.</param>
5655
/// <exception cref="ArgumentNullException"><paramref name="blobContainerClient" /> is <c>null</c>.</exception>
57-
public AzureBlobFileSystemImageCache(BlobContainerClient blobContainerClient)
58-
=> _container = blobContainerClient ?? throw new ArgumentNullException(nameof(blobContainerClient));
56+
public AzureBlobFileSystemImageCache(BlobContainerClient blobContainerClient, string? containerRootPath)
57+
{
58+
_container = blobContainerClient ?? throw new ArgumentNullException(nameof(blobContainerClient));
59+
60+
if (!string.IsNullOrEmpty(containerRootPath))
61+
{
62+
_containerRootPath = containerRootPath.EnsureEndsWith('/');
63+
}
64+
}
5965

6066
/// <inheritdoc />
6167
public async Task<IImageCacheResolver?> GetAsync(string key)
6268
{
63-
var blob = _container.GetBlobClient(CachePath + key);
69+
var blob = _container.GetBlobClient(_containerRootPath + key);
6470

6571
return !await blob.ExistsAsync().ConfigureAwait(false)
6672
? null
@@ -70,8 +76,11 @@ public AzureBlobFileSystemImageCache(BlobContainerClient blobContainerClient)
7076
/// <inheritdoc />
7177
public async Task SetAsync(string key, Stream stream, ImageCacheMetadata metadata)
7278
{
73-
var blob = _container.GetBlobClient(CachePath + key);
79+
var blob = _container.GetBlobClient(_containerRootPath + key);
7480

75-
await blob.UploadAsync(stream, metadata: metadata.ToDictionary()).ConfigureAwait(false);
81+
await blob.UploadAsync(stream, new BlobUploadOptions()
82+
{
83+
Metadata = metadata.ToDictionary()
84+
}).ConfigureAwait(false);
7685
}
7786
}

src/Umbraco.StorageProviders.AzureBlob.ImageSharp/DependencyInjection/AddAzureBlobImageSharpCacheExtensions.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,51 @@
88
namespace Umbraco.Cms.Core.DependencyInjection;
99

1010
/// <summary>
11-
/// Extension methods to help registering Azure Blob Storage image caches for ImageSharp.
11+
/// Extension methods to help register an Azure Blob Storage image cache for ImageSharp.
1212
/// </summary>
1313
public static class AddAzureBlobImageSharpCacheExtensions
1414
{
15+
private const string ContainerRootPath = "cache";
16+
17+
/// <summary>
18+
/// Registers an <see cref="IImageCache" /> configured using the <see cref="AzureBlobFileSystemOptions" /> for media and <see cref="ContainerRootPath" /> container root path.
19+
/// </summary>
20+
/// <param name="builder">The <see cref="IUmbracoBuilder" />.</param>
21+
/// <returns>
22+
/// The <see cref="IUmbracoBuilder" />.
23+
/// </returns>
24+
/// <exception cref="System.ArgumentNullException"><paramref name="builder" /> is <c>null</c>.</exception>
25+
public static IUmbracoBuilder AddAzureBlobImageSharpCache(this IUmbracoBuilder builder)
26+
=> builder.AddInternal(AzureBlobFileSystemOptions.MediaFileSystemName, ContainerRootPath);
27+
1528
/// <summary>
16-
/// Registers an <see cref="IImageCache" /> configured using the specified <see cref="IAzureBlobFileSystem" />.
29+
/// Registers an <see cref="IImageCache" /> configured using the specified <see cref="AzureBlobFileSystemOptions" />.
1730
/// </summary>
1831
/// <param name="builder">The <see cref="IUmbracoBuilder" />.</param>
1932
/// <param name="name">The name of the file system.</param>
33+
/// <param name="containerRootPath">The container root path.</param>
2034
/// <returns>
2135
/// The <see cref="IUmbracoBuilder" />.
2236
/// </returns>
2337
/// <exception cref="System.ArgumentNullException"><paramref name="builder" /> is <c>null</c>.</exception>
24-
public static IUmbracoBuilder AddAzureBlobImageSharpCache(this IUmbracoBuilder builder, string name = AzureBlobFileSystemOptions.MediaFileSystemName)
25-
=> builder.AddInternal(name);
38+
public static IUmbracoBuilder AddAzureBlobImageSharpCache(this IUmbracoBuilder builder, string name, string? containerRootPath = null)
39+
=> builder.AddInternal(name, containerRootPath);
2640

2741
/// <summary>
28-
/// Registers an <see cref="IImageCache" /> configured using the specified <see cref="IAzureBlobFileSystem" />.
42+
/// Registers an <see cref="IImageCache" /> configured using the specified <see cref="AzureBlobFileSystemOptions" />.
2943
/// </summary>
3044
/// <param name="builder">The <see cref="IUmbracoBuilder" />.</param>
3145
/// <param name="name">The name of the file system.</param>
46+
/// <param name="containerRootPath">The container root path.</param>
3247
/// <returns>
3348
/// The <see cref="IUmbracoBuilder" />.
3449
/// </returns>
3550
/// <exception cref="System.ArgumentNullException"><paramref name="builder" /> is <c>null</c>.</exception>
36-
internal static IUmbracoBuilder AddInternal(this IUmbracoBuilder builder, string name = AzureBlobFileSystemOptions.MediaFileSystemName)
51+
internal static IUmbracoBuilder AddInternal(this IUmbracoBuilder builder, string name, string? containerRootPath)
3752
{
3853
ArgumentNullException.ThrowIfNull(builder);
3954

40-
builder.Services.AddUnique<IImageCache>(provider => new AzureBlobFileSystemImageCache(name, provider.GetRequiredService<IOptionsMonitor<AzureBlobFileSystemOptions>>()));
55+
builder.Services.AddUnique<IImageCache>(provider => new AzureBlobFileSystemImageCache(provider.GetRequiredService<IOptionsMonitor<AzureBlobFileSystemOptions>>(), name, containerRootPath));
4156

4257
return builder;
4358
}

0 commit comments

Comments
 (0)