|
6 | 6 | using SixLabors.ImageSharp.Web.Resolvers.Azure;
|
7 | 7 | using Umbraco.StorageProviders.AzureBlob.IO;
|
8 | 8 |
|
9 |
| -namespace Umbraco.StorageProviders.AzureBlob.ImageSharp |
| 9 | +namespace Umbraco.StorageProviders.AzureBlob.ImageSharp; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Implements an Azure Blob Storage based cache storing files in a <c>cache</c> subfolder. |
| 13 | +/// </summary> |
| 14 | +public sealed class AzureBlobFileSystemImageCache : IImageCache |
10 | 15 | {
|
| 16 | + private const string CachePath = "cache/"; |
| 17 | + private BlobContainerClient _container; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Initializes a new instance of the <see cref="AzureBlobFileSystemImageCache" /> class. |
| 21 | + /// </summary> |
| 22 | + /// <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 | + |
11 | 28 | /// <summary>
|
12 |
| - /// Implements an Azure Blob Storage based cache storing files in a <c>cache</c> subfolder. |
| 29 | + /// Initializes a new instance of the <see cref="AzureBlobFileSystemImageCache"/> class. |
13 | 30 | /// </summary>
|
14 |
| - public sealed class AzureBlobFileSystemImageCache : IImageCache |
| 31 | + /// <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> |
| 34 | + /// <exception cref="ArgumentNullException"><paramref name="options" /> is <c>null</c>.</exception> |
| 35 | + public AzureBlobFileSystemImageCache(string name, IOptionsMonitor<AzureBlobFileSystemOptions> options) |
15 | 36 | {
|
16 |
| - private const string CachePath = "cache/"; |
17 |
| - private BlobContainerClient _container; |
| 37 | + ArgumentNullException.ThrowIfNull(name); |
| 38 | + ArgumentNullException.ThrowIfNull(options); |
18 | 39 |
|
19 |
| - /// <summary> |
20 |
| - /// Initializes a new instance of the <see cref="AzureBlobFileSystemImageCache" /> class. |
21 |
| - /// </summary> |
22 |
| - /// <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 |
| - { } |
| 40 | + var fileSystemOptions = options.Get(name); |
| 41 | + _container = new BlobContainerClient(fileSystemOptions.ConnectionString, fileSystemOptions.ContainerName); |
27 | 42 |
|
28 |
| - /// <summary> |
29 |
| - /// Initializes a new instance of the <see cref="AzureBlobFileSystemImageCache"/> class. |
30 |
| - /// </summary> |
31 |
| - /// <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> |
34 |
| - /// <exception cref="ArgumentNullException"><paramref name="options" /> is <c>null</c>.</exception> |
35 |
| - public AzureBlobFileSystemImageCache(string name, IOptionsMonitor<AzureBlobFileSystemOptions> options) |
| 43 | + options.OnChange((options, changedName) => |
36 | 44 | {
|
37 |
| - ArgumentNullException.ThrowIfNull(name); |
38 |
| - ArgumentNullException.ThrowIfNull(options); |
39 |
| - |
40 |
| - var fileSystemOptions = options.Get(name); |
41 |
| - _container = new BlobContainerClient(fileSystemOptions.ConnectionString, fileSystemOptions.ContainerName); |
42 |
| - |
43 |
| - options.OnChange((options, changedName) => |
| 45 | + if (changedName == name) |
44 | 46 | {
|
45 |
| - if (changedName == name) |
46 |
| - { |
47 |
| - _container = new BlobContainerClient(options.ConnectionString, options.ContainerName); |
48 |
| - } |
49 |
| - }); |
50 |
| - } |
| 47 | + _container = new BlobContainerClient(options.ConnectionString, options.ContainerName); |
| 48 | + } |
| 49 | + }); |
| 50 | + } |
51 | 51 |
|
52 |
| - /// <summary> |
53 |
| - /// Initializes a new instance of the <see cref="AzureBlobFileSystemImageCache" /> class. |
54 |
| - /// </summary> |
55 |
| - /// <param name="blobContainerClient">The blob container client.</param> |
56 |
| - /// <exception cref="ArgumentNullException"><paramref name="blobContainerClient" /> is <c>null</c>.</exception> |
57 |
| - public AzureBlobFileSystemImageCache(BlobContainerClient blobContainerClient) |
58 |
| - => _container = blobContainerClient ?? throw new ArgumentNullException(nameof(blobContainerClient)); |
| 52 | + /// <summary> |
| 53 | + /// Initializes a new instance of the <see cref="AzureBlobFileSystemImageCache" /> class. |
| 54 | + /// </summary> |
| 55 | + /// <param name="blobContainerClient">The blob container client.</param> |
| 56 | + /// <exception cref="ArgumentNullException"><paramref name="blobContainerClient" /> is <c>null</c>.</exception> |
| 57 | + public AzureBlobFileSystemImageCache(BlobContainerClient blobContainerClient) |
| 58 | + => _container = blobContainerClient ?? throw new ArgumentNullException(nameof(blobContainerClient)); |
59 | 59 |
|
60 |
| - /// <inheritdoc /> |
61 |
| - public async Task<IImageCacheResolver?> GetAsync(string key) |
62 |
| - { |
63 |
| - var blob = _container.GetBlobClient(CachePath + key); |
| 60 | + /// <inheritdoc /> |
| 61 | + public async Task<IImageCacheResolver?> GetAsync(string key) |
| 62 | + { |
| 63 | + var blob = _container.GetBlobClient(CachePath + key); |
64 | 64 |
|
65 |
| - return !await blob.ExistsAsync().ConfigureAwait(false) |
66 |
| - ? null |
67 |
| - : new AzureBlobStorageCacheResolver(blob); |
68 |
| - } |
| 65 | + return !await blob.ExistsAsync().ConfigureAwait(false) |
| 66 | + ? null |
| 67 | + : new AzureBlobStorageCacheResolver(blob); |
| 68 | + } |
69 | 69 |
|
70 |
| - /// <inheritdoc /> |
71 |
| - public async Task SetAsync(string key, Stream stream, ImageCacheMetadata metadata) |
72 |
| - { |
73 |
| - var blob = _container.GetBlobClient(CachePath + key); |
| 70 | + /// <inheritdoc /> |
| 71 | + public async Task SetAsync(string key, Stream stream, ImageCacheMetadata metadata) |
| 72 | + { |
| 73 | + var blob = _container.GetBlobClient(CachePath + key); |
74 | 74 |
|
75 |
| - await blob.UploadAsync(stream, metadata: metadata.ToDictionary()).ConfigureAwait(false); |
76 |
| - } |
| 75 | + await blob.UploadAsync(stream, metadata: metadata.ToDictionary()).ConfigureAwait(false); |
77 | 76 | }
|
78 | 77 | }
|
0 commit comments