Skip to content

Commit 9fe0483

Browse files
committed
feature: experimental Cloudflare R2 support
1 parent 20de03f commit 9fe0483

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed
Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,62 @@
1-
using Microsoft.Extensions.Options;
1+
using Amazon.S3;
2+
using Amazon.S3.Model;
3+
using Microsoft.Extensions.Options;
4+
using Our.Umbraco.StorageProviders.AWSS3.IO;
25
using SixLabors.ImageSharp.Web;
36
using SixLabors.ImageSharp.Web.Caching;
47
using SixLabors.ImageSharp.Web.Caching.AWS;
58
using SixLabors.ImageSharp.Web.Resolvers;
9+
using SixLabors.ImageSharp.Web.Resolvers.AWS;
10+
using Umbraco.Community.FileSystemProviders.B2.Models;
611

712
namespace Umbraco.Community.FileSystemProviders.B2;
813

9-
public class B2FileSystemImageCache(IOptions<AWSS3StorageCacheOptions> options) : IImageCache
14+
public class B2FileSystemImageCache(
15+
IOptions<AWSS3StorageCacheOptions> options,
16+
IOptions<B2Options> b2Options,
17+
IAWSS3FileSystemProvider fileSystemProvider)
18+
: IImageCache
1019
{
11-
private readonly AWSS3StorageCache _baseCache = new(options);
20+
private readonly IAmazonS3 _client = fileSystemProvider.GetFileSystem(Constants.Aliases.MediaFileSystem).GetS3Client("");
1221

13-
public async Task<IImageCacheResolver?> GetAsync(string key) =>
14-
await _baseCache.GetAsync(Path.Combine("cache/", key));
22+
public virtual async Task<IImageCacheResolver?> GetAsync(string key)
23+
{
24+
var request = new GetObjectMetadataRequest
25+
{
26+
BucketName = b2Options.Value.BucketName,
27+
Key = key
28+
};
1529

16-
public async Task SetAsync(string key, Stream stream, ImageCacheMetadata metadata) =>
17-
await _baseCache.SetAsync(Path.Combine("cache/", key), stream, metadata);
30+
try
31+
{
32+
// HEAD request throws a 404 if not found.
33+
var metadata = (await _client.GetObjectMetadataAsync(request)).Metadata;
34+
return new AWSS3StorageCacheResolver(_client, b2Options.Value.BucketName, key, metadata);
35+
}
36+
catch
37+
{
38+
return null;
39+
}
40+
}
41+
42+
43+
public virtual async Task SetAsync(string key, Stream stream, ImageCacheMetadata metadata)
44+
{
45+
var request = new PutObjectRequest
46+
{
47+
BucketName = options.Value.BucketName,
48+
Key = key,
49+
ContentType = metadata.ContentType,
50+
InputStream = stream,
51+
AutoCloseStream = false,
52+
DisablePayloadSigning = b2Options.Value.Experimental.DisablePayloadSigning
53+
};
54+
55+
foreach (KeyValuePair<string, string> d in metadata.ToDictionary())
56+
{
57+
request.Metadata.Add(d.Key, d.Value);
58+
}
59+
60+
await _client.PutObjectAsync(request);
61+
}
1862
}

src/Umbraco.Community.FileSystemProviders.B2/Composing/UmbracoBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static void AddB2MediaFileSystem(this IUmbracoBuilder builder)
8181
builder.SetMediaFileSystem(x =>
8282
{
8383
var provider = x.GetRequiredService<B2FileSystemProvider>();
84-
return provider.GetFileSystem("Media");
84+
return provider.GetFileSystem(Constants.Aliases.MediaFileSystem);
8585
});
8686

8787
builder.Services.Configure<UmbracoPipelineOptions>(options =>

src/Umbraco.Community.FileSystemProviders.B2/Constants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,9 @@ public class Groups
5959
public const string Configuration = "B2 Configuration";
6060
}
6161
}
62+
63+
public static class Aliases
64+
{
65+
public const string MediaFileSystem = "Media";
66+
}
6267
}

src/Umbraco.Community.FileSystemProviders.B2/HealthChecks/ApiHealthCheck.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private bool TryGetFileSystem(out IFileSystem? fs)
7171
fs = null;
7272
try
7373
{
74-
if (mediaFileManager.GetFileSystem("Media") is IFileSystem fss)
74+
if (mediaFileManager.GetFileSystem(Constants.Aliases.MediaFileSystem) is IFileSystem fss)
7575
{
7676
fs = fss;
7777
return true;

0 commit comments

Comments
 (0)