1
1
using Azure . Storage . Blobs ;
2
+ using Azure . Storage . Blobs . Models ;
2
3
using Microsoft . Extensions . Options ;
3
4
using SixLabors . ImageSharp . Web ;
4
5
using SixLabors . ImageSharp . Web . Caching ;
5
6
using SixLabors . ImageSharp . Web . Resolvers ;
6
7
using SixLabors . ImageSharp . Web . Resolvers . Azure ;
8
+ using Umbraco . Extensions ;
7
9
using Umbraco . StorageProviders . AzureBlob . IO ;
8
10
9
11
namespace Umbraco . StorageProviders . AzureBlob . ImageSharp ;
@@ -13,29 +15,20 @@ namespace Umbraco.StorageProviders.AzureBlob.ImageSharp;
13
15
/// </summary>
14
16
public sealed class AzureBlobFileSystemImageCache : IImageCache
15
17
{
16
- private const string CachePath = "cache/" ;
18
+ private readonly string ? _containerRootPath ;
17
19
private BlobContainerClient _container ;
18
20
19
21
/// <summary>
20
22
/// Initializes a new instance of the <see cref="AzureBlobFileSystemImageCache" /> class.
21
23
/// </summary>
22
24
/// <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>
31
25
/// <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>
34
27
/// <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 )
36
29
{
37
- ArgumentNullException . ThrowIfNull ( name ) ;
38
30
ArgumentNullException . ThrowIfNull ( options ) ;
31
+ ArgumentNullException . ThrowIfNull ( name ) ;
39
32
40
33
var fileSystemOptions = options . Get ( name ) ;
41
34
_container = new BlobContainerClient ( fileSystemOptions . ConnectionString , fileSystemOptions . ContainerName ) ;
@@ -47,20 +40,33 @@ public AzureBlobFileSystemImageCache(string name, IOptionsMonitor<AzureBlobFileS
47
40
_container = new BlobContainerClient ( options . ConnectionString , options . ContainerName ) ;
48
41
}
49
42
} ) ;
43
+
44
+ if ( ! string . IsNullOrEmpty ( containerRootPath ) )
45
+ {
46
+ _containerRootPath = containerRootPath . EnsureEndsWith ( '/' ) ;
47
+ }
50
48
}
51
49
52
50
/// <summary>
53
51
/// Initializes a new instance of the <see cref="AzureBlobFileSystemImageCache" /> class.
54
52
/// </summary>
55
53
/// <param name="blobContainerClient">The blob container client.</param>
54
+ /// <param name="containerRootPath">The container root path.</param>
56
55
/// <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
+ }
59
65
60
66
/// <inheritdoc />
61
67
public async Task < IImageCacheResolver ? > GetAsync ( string key )
62
68
{
63
- var blob = _container . GetBlobClient ( CachePath + key ) ;
69
+ var blob = _container . GetBlobClient ( _containerRootPath + key ) ;
64
70
65
71
return ! await blob . ExistsAsync ( ) . ConfigureAwait ( false )
66
72
? null
@@ -70,8 +76,11 @@ public AzureBlobFileSystemImageCache(BlobContainerClient blobContainerClient)
70
76
/// <inheritdoc />
71
77
public async Task SetAsync ( string key , Stream stream , ImageCacheMetadata metadata )
72
78
{
73
- var blob = _container . GetBlobClient ( CachePath + key ) ;
79
+ var blob = _container . GetBlobClient ( _containerRootPath + key ) ;
74
80
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 ) ;
76
85
}
77
86
}
0 commit comments