Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,11 @@ public async Task DeleteBucket_ShouldThrow_GivenNoPermission()
var act = () => this.PublicStorage.DeleteBucket(id);
await act.Should().ThrowAsync<SupabaseStorageException>();
}

[TestMethod]
public async Task PurgeProjectCacheAsync_ShouldThrow_GivenNoPermission()
{
var act = () => this.PublicStorage.PurgeProjectCacheAsync();
(await act.Should().ThrowAsync<SupabaseStorageException>()).Which.StatusCode.Should().Be(403);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,25 @@ public async Task PurgeBucketCache_ShouldEncodeDelimiters_GivenABucketIdWithUrlD
"a raw '?' in the id would start a query string and purge the wrong bucket");
}

[TestMethod]
public async Task PurgeProjectCacheAsync_ShouldDeleteTheCdnRoot()
{
this.Respond("/storage/v1/cdn/", "DELETE", 200, "{\"message\":\"success\"}");
await this.client.PurgeProjectCacheAsync();
var request = this.SingleRequest();
request.Method.Should().Be("DELETE");
request.Path.Should().Be("/storage/v1/cdn/");
request.Query.Should().BeNullOrEmpty("unset options must purge every cached version, not only the transformations");
}

[TestMethod]
public async Task PurgeProjectCacheAsync_ShouldRequestTransformationsOnly_GivenTheTransformationsOption()
{
this.Respond("/storage/v1/cdn/", "DELETE", 200, "{\"message\":\"success\"}");
await this.client.PurgeProjectCacheAsync(new PurgeCacheOptions { Transformations = true });
this.SingleRequest().Query.Should().Contain(pair => pair.Key == "transformations" && pair.Value.Contains("true"));
}

private void Respond(string path, string method, int statusCode, string body) =>
this.server.Given(Request.Create().WithPath(path).UsingMethod(method))
.RespondWith(Response.Create().WithStatusCode(statusCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ public async Task PurgeBucketCache_ShouldForwardTheTokenToTheRequest()
"the bucket purge must carry the caller's token so an in-flight request can be cancelled");
}

[TestMethod]
public async Task PurgeProjectCacheAsync_ShouldForwardTheTokenToTheRequest()
{
using var cts = new CancellationTokenSource();
var act = () => this.client.PurgeProjectCacheAsync(options: null, cts.Token);
(await this.TokenSeenBy(act, cts)).Should().BeTrue(
"the project purge must carry the caller's token so an in-flight request can be cancelled");
}

private async Task<bool> TokenSeenBy(Func<Task> act, CancellationTokenSource cts)
{
var requestSawLiveToken = false;
Expand Down
10 changes: 10 additions & 0 deletions packages/Storage/Storage/Interfaces/IStorageBucketApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,15 @@ public interface IStorageBucketApi<TBucket> : IGettableHeaders
/// </code>
/// </example>
Task<GenericResponse?> PurgeBucketCache(string id, PurgeCacheOptions? options = null, CancellationToken cancellationToken = default);

/// <summary>
/// Purges the CDN cache for every object in every bucket of the project. Requires a service-role key.
/// </summary>
/// <example>
/// <code>
/// await storage.PurgeProjectCacheAsync();
/// </code>
/// </example>
Task PurgeProjectCacheAsync(PurgeCacheOptions? options = null, CancellationToken cancellationToken = default);
}
}
2 changes: 2 additions & 0 deletions packages/Storage/Storage/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#nullable enable
Supabase.Storage.Interfaces.IStorageBucketApi<TBucket>.PurgeProjectCacheAsync(Supabase.Storage.PurgeCacheOptions? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task!
Supabase.Storage.StorageBucketApi.PurgeProjectCacheAsync(Supabase.Storage.PurgeCacheOptions? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task!
11 changes: 6 additions & 5 deletions packages/Storage/Storage/PurgeCacheOptions.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
namespace Supabase.Storage
{
/// <summary>
/// Options for <see cref="Interfaces.IStorageFileApi{TFileObject}.PurgeCache"/> and
/// <see cref="Interfaces.IStorageBucketApi{TBucket}.PurgeBucketCache"/>.
/// Options for <see cref="Interfaces.IStorageFileApi{TFileObject}.PurgeCache"/>,
/// <see cref="Interfaces.IStorageBucketApi{TBucket}.PurgeBucketCache"/> and
/// <see cref="Interfaces.IStorageBucketApi{TBucket}.PurgeProjectCacheAsync"/>.
/// </summary>
public class PurgeCacheOptions
{
/// <summary>
/// If <c>true</c>, purges only the transformations (resized/formatted variants) for the object
/// or bucket, leaving the original cached file intact. If left <c>null</c>, all cached versions
/// are purged.
/// If <c>true</c>, purges only the transformations (resized/formatted variants) for the object,
/// bucket or project, leaving the original cached file intact. If left <c>null</c>, all cached
/// versions are purged.
/// </summary>
public bool? Transformations { get; set; }
}
Expand Down
5 changes: 5 additions & 0 deletions packages/Storage/Storage/StorageBucketApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,9 @@ public async Task<string> CreateBucket(string id, BucketUpsertOptions? options =
var url = options.ToPurgeUrl($"{this.Url}/cdn/{Helpers.EncodePath(id)}");
return Helpers.MakeRequestAsync<GenericResponse>(this.requestClient, this.Options, HttpMethod.Delete, url, null, this.Headers, cancellationToken);
}

/// <inheritdoc />
[ImplementedCapability("storage.file_buckets.purge_project_cache")]
public Task PurgeProjectCacheAsync(PurgeCacheOptions? options = null, CancellationToken cancellationToken = default) =>
Helpers.MakeRequestAsync(this.requestClient, this.Options, HttpMethod.Delete, options.ToPurgeUrl($"{this.Url}/cdn/"), null, this.Headers, cancellationToken);
}