-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
AzureBlobStorageImageResolver.cs
48 lines (40 loc) · 1.72 KB
/
AzureBlobStorageImageResolver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Net.Http.Headers;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
namespace SixLabors.ImageSharp.Web.Resolvers.Azure;
/// <summary>
/// Provides means to manage image buffers within the Azure Blob file system.
/// </summary>
public class AzureBlobStorageImageResolver : IImageResolver
{
private readonly BlobClient blob;
/// <summary>
/// Initializes a new instance of the <see cref="AzureBlobStorageImageResolver"/> class.
/// </summary>
/// <param name="blob">The Azure blob.</param>
public AzureBlobStorageImageResolver(BlobClient blob)
=> this.blob = blob;
/// <inheritdoc/>
public async Task<ImageMetadata> GetMetaDataAsync()
{
// I've had a good read through the SDK source and I believe we cannot get
// a 304 here since 'If-Modified-Since' header is not set by default.
BlobProperties properties = (await this.blob.GetPropertiesAsync()).Value;
// 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))
{
// Weirdly passing null to TryParse returns true.
if (cacheControl?.MaxAge.HasValue == true)
{
maxAge = cacheControl.MaxAge.Value;
}
}
return new ImageMetadata(properties.LastModified.UtcDateTime, maxAge, properties.ContentLength);
}
/// <inheritdoc/>
public Task<Stream> OpenReadAsync() => this.blob.OpenReadAsync();
}