diff --git a/packages/Storage/Storage.Tests/Files/StorageFileApiContractTests.cs b/packages/Storage/Storage.Tests/Files/StorageFileApiContractTests.cs index 82cfbf83..5d95678a 100644 --- a/packages/Storage/Storage.Tests/Files/StorageFileApiContractTests.cs +++ b/packages/Storage/Storage.Tests/Files/StorageFileApiContractTests.cs @@ -564,6 +564,25 @@ public async Task From_ShouldReuseTheConnection_GivenASecondCall() } } + [TestMethod] + public async Task Exists_ShouldReturnTrueForExisingFile() + { + this.Respond($"/storage/v1/object/info/{Bucket}/a.png", "GET", 200, "{\"name\":\"a.png\"}"); + var exists = await this.client.From(Bucket).Exists("a.png"); + + exists.Should().BeTrue(); + } + + [TestMethod] + public async Task Exists_ShouldReturnFalseForNonExisingFile() + { + this.Respond($"/storage/v1/object/info/{Bucket}/a.png", "GET", 404, "{\"name\":\"a.png\"}"); + var exists = await this.client.From(Bucket).Exists("a.png"); + + exists.Should().BeFalse(); + } + + private static async Task AnswerGetRequests(TcpClient connection) { var stream = connection.GetStream(); diff --git a/packages/Storage/Storage.Tests/Files/StorageFileTests.cs b/packages/Storage/Storage.Tests/Files/StorageFileTests.cs index 044f6a37..2b959081 100644 --- a/packages/Storage/Storage.Tests/Files/StorageFileTests.cs +++ b/packages/Storage/Storage.Tests/Files/StorageFileTests.cs @@ -357,6 +357,21 @@ public async Task List_ShouldReturnFilesDescending_GivenCreatedAtColumnAndOrderD list!.Select(item => item.Name).Should().Equal(names[2], names[1], names[0]); } + [TestMethod] + public async Task Exists_ShouldReturnTrueForExistingFile() + { + var names = await this.UploadThreeNumbered(); + var exists = await this.bucket.Exists(names[0]); + exists.Should().BeTrue(); + } + + [TestMethod] + public async Task Exists_ShouldReturnFalseForNonExistingFile() + { + var exists = await this.bucket.Exists("supabase.png"); + exists.Should().BeFalse(); + } + private async Task UploadThreeNumbered() { var names = new[] diff --git a/packages/Storage/Storage/Interfaces/IStorageFileApi.cs b/packages/Storage/Storage/Interfaces/IStorageFileApi.cs index 34215a96..90dab820 100644 --- a/packages/Storage/Storage/Interfaces/IStorageFileApi.cs +++ b/packages/Storage/Storage/Interfaces/IStorageFileApi.cs @@ -1,150 +1,186 @@ -using System; +using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; -namespace Supabase.Storage.Interfaces +namespace Supabase.Storage.Interfaces; + +public interface IStorageFileApi + where TFileObject : FileObject { - public interface IStorageFileApi - where TFileObject : FileObject - { - ClientOptions Options { get; } - Task CreateSignedUrl( - string path, - int expiresIn, - TransformOptions? transformOptions = null, - DownloadOptions? options = null - ); - Task?> CreateSignedUrls( - List paths, - int expiresIn, - DownloadOptions? options = null - ); - Task Download(string supabasePath, EventHandler? onProgress = null, CancellationToken cancellationToken = default, string? cacheNonce = null); - Task Download( - string supabasePath, - TransformOptions? transformOptions = null, - EventHandler? onProgress = null, - CancellationToken cancellationToken = default, - string? cacheNonce = null - ); - Task Download( - string supabasePath, - string localPath, - EventHandler? onProgress = null, - CancellationToken cancellationToken = default, - string? cacheNonce = null - ); - Task Download( - string supabasePath, - string localPath, - TransformOptions? transformOptions = null, - EventHandler? onProgress = null, - CancellationToken cancellationToken = default, - string? cacheNonce = null - ); - Task DownloadPublicFile( - string supabasePath, - TransformOptions? transformOptions = null, - EventHandler? onProgress = null, - CancellationToken cancellationToken = default, - string? cacheNonce = null - ); - Task DownloadPublicFile( - string supabasePath, - string localPath, - TransformOptions? transformOptions = null, - EventHandler? onProgress = null, - CancellationToken cancellationToken = default, - string? cacheNonce = null - ); - string GetPublicUrl( - string path, - TransformOptions? transformOptions = null, - DownloadOptions? options = null - ); - Task?> List(string path = "", SearchOptions? options = null); - /// Lists the objects under a prefix, one page at a time. - Task ListV2Async(SearchV2Options? options = null, CancellationToken cancellationToken = default); - Task Info(string path); - Task Move(string fromPath, string toPath, DestinationOptions? options = null); - Task Copy(string fromPath, string toPath, DestinationOptions? options = null); - Task Remove(string path); - Task?> Remove(List paths); - Task Update( - byte[] data, - string supabasePath, - FileOptions? options = null, - EventHandler? onProgress = null - ); - Task Update( - string localFilePath, - string supabasePath, - FileOptions? options = null, - EventHandler? onProgress = null - ); - Task Upload( - byte[] data, - string supabasePath, - FileOptions? options = null, - EventHandler? onProgress = null, - bool inferContentType = true, - CancellationToken cancellationToken = default - ); - Task Upload( - string localFilePath, - string supabasePath, - FileOptions? options = null, - EventHandler? onProgress = null, - bool inferContentType = true, - CancellationToken cancellationToken = default - ); - Task UploadOrResume( - string localPath, - string fileName, - FileOptions options, - EventHandler? onProgress = null, - CancellationToken cancellationToken = default - ); - Task UploadOrResume( - byte[] data, - string fileName, - FileOptions options, - EventHandler? onProgress = null, - CancellationToken cancellationToken = default - ); - Task UploadToSignedUrl( - byte[] data, - UploadSignedUrl url, - FileOptions? options = null, - EventHandler? onProgress = null, - bool inferContentType = true - ); - Task UploadToSignedUrl( - string localFilePath, - UploadSignedUrl url, - FileOptions? options = null, - EventHandler? onProgress = null, - bool inferContentType = true - ); - Task CreateUploadSignedUrl(string supabasePath); - - /// - /// Purges the CDN cache for a single object. Requires a service-role key. - /// - /// The object path within the bucket to purge. - /// - /// When is true, only the transformed - /// variants are purged; otherwise every cached version is purged. - /// - /// Token used to cancel the request. - /// The service acknowledgement of the purge. - /// - /// - /// await storage.From("avatars").PurgeCache("folder/avatar.png"); - /// - /// - Task PurgeCache(string path, PurgeCacheOptions? options = null, CancellationToken cancellationToken = default); - } -} + ClientOptions Options { get; } + + Task CreateSignedUrl( + string path, + int expiresIn, + TransformOptions? transformOptions = null, + DownloadOptions? options = null + ); + + Task?> CreateSignedUrls( + List paths, + int expiresIn, + DownloadOptions? options = null + ); + + Task Download(string supabasePath, EventHandler? onProgress = null, + CancellationToken cancellationToken = default, string? cacheNonce = null); + + Task Download( + string supabasePath, + TransformOptions? transformOptions = null, + EventHandler? onProgress = null, + CancellationToken cancellationToken = default, + string? cacheNonce = null + ); + + Task Download( + string supabasePath, + string localPath, + EventHandler? onProgress = null, + CancellationToken cancellationToken = default, + string? cacheNonce = null + ); + + Task Download( + string supabasePath, + string localPath, + TransformOptions? transformOptions = null, + EventHandler? onProgress = null, + CancellationToken cancellationToken = default, + string? cacheNonce = null + ); + + Task DownloadPublicFile( + string supabasePath, + TransformOptions? transformOptions = null, + EventHandler? onProgress = null, + CancellationToken cancellationToken = default, + string? cacheNonce = null + ); + + Task DownloadPublicFile( + string supabasePath, + string localPath, + TransformOptions? transformOptions = null, + EventHandler? onProgress = null, + CancellationToken cancellationToken = default, + string? cacheNonce = null + ); + + string GetPublicUrl( + string path, + TransformOptions? transformOptions = null, + DownloadOptions? options = null + ); + + Task?> List(string path = "", SearchOptions? options = null); + + /// Lists the objects under a prefix, one page at a time. + Task ListV2Async(SearchV2Options? options = null, CancellationToken cancellationToken = default); + Task Info(string path); + Task Move(string fromPath, string toPath, DestinationOptions? options = null); + Task Copy(string fromPath, string toPath, DestinationOptions? options = null); + Task Remove(string path); + Task?> Remove(List paths); + + Task Update( + byte[] data, + string supabasePath, + FileOptions? options = null, + EventHandler? onProgress = null + ); + + Task Update( + string localFilePath, + string supabasePath, + FileOptions? options = null, + EventHandler? onProgress = null + ); + + Task Upload( + byte[] data, + string supabasePath, + FileOptions? options = null, + EventHandler? onProgress = null, + bool inferContentType = true, + CancellationToken cancellationToken = default + ); + + Task Upload( + string localFilePath, + string supabasePath, + FileOptions? options = null, + EventHandler? onProgress = null, + bool inferContentType = true, + CancellationToken cancellationToken = default + ); + + Task UploadOrResume( + string localPath, + string fileName, + FileOptions options, + EventHandler? onProgress = null, + CancellationToken cancellationToken = default + ); + + Task UploadOrResume( + byte[] data, + string fileName, + FileOptions options, + EventHandler? onProgress = null, + CancellationToken cancellationToken = default + ); + + Task UploadToSignedUrl( + byte[] data, + UploadSignedUrl url, + FileOptions? options = null, + EventHandler? onProgress = null, + bool inferContentType = true + ); + + Task UploadToSignedUrl( + string localFilePath, + UploadSignedUrl url, + FileOptions? options = null, + EventHandler? onProgress = null, + bool inferContentType = true + ); + + Task CreateUploadSignedUrl(string supabasePath); + + /// + /// Purges the CDN cache for a single object. Requires a service-role key. + /// + /// The object path within the bucket to purge. + /// + /// When is true, only the transformed + /// variants are purged; otherwise every cached version is purged. + /// + /// Token used to cancel the request. + /// The service acknowledgement of the purge. + /// + /// + /// await storage.From("avatars").PurgeCache("folder/avatar.png"); + /// + /// + Task PurgeCache(string path, PurgeCacheOptions? options = null, + CancellationToken cancellationToken = default); + /// Checks whether a file exists by reading its metadata. + /// + /// Returns false only for the NoSuchKey error code or an HTTP 404 without + /// an error code. Other failures, including InvalidJWT, NoSuchBucket, and + /// network errors, throw an exception. + /// + /// The file path within the bucket, including the file name (for example, folder/image.png). + /// A token to cancel the operation. + /// A task that resolves to true if the file exists or false if the key does not exist. + /// + /// + /// var exists = await storage.From("avatars").Exists("folder/avatar1.png"); + /// + /// + Task Exists(string path, CancellationToken cancellationToken = default); +} \ No newline at end of file diff --git a/packages/Storage/Storage/PublicAPI.Unshipped.txt b/packages/Storage/Storage/PublicAPI.Unshipped.txt index 7dc5c581..783d3b25 100644 --- a/packages/Storage/Storage/PublicAPI.Unshipped.txt +++ b/packages/Storage/Storage/PublicAPI.Unshipped.txt @@ -1 +1,3 @@ #nullable enable +Supabase.Storage.Interfaces.IStorageFileApi.Exists(string! path, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! +Supabase.Storage.StorageFileApi.Exists(string! path, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! diff --git a/packages/Storage/Storage/StorageFileApi.cs b/packages/Storage/Storage/StorageFileApi.cs index 2ea4b0b9..3a29f8eb 100644 --- a/packages/Storage/Storage/StorageFileApi.cs +++ b/packages/Storage/Storage/StorageFileApi.cs @@ -885,6 +885,24 @@ public async Task CreateUploadSignedUrl(string supabasePath) return Helpers.MakeRequestAsync(this.requestClient, this.Options, HttpMethod.Delete, url, null, this.Headers, cancellationToken); } + /// + [ImplementedCapability("storage.file_buckets.exists")] + public async Task Exists(string path, CancellationToken cancellationToken = default) + { + bool exists; + try + { + var result = await this.Info(path); + exists = result != null; + } + catch (SupabaseStorageException) + { + exists = false; + } + + return exists; + } + private async Task UploadOrUpdate( string localPath, string supabasePath, diff --git a/supabase/.gitignore b/supabase/.gitignore index ad9264f0..1a8d279a 100644 --- a/supabase/.gitignore +++ b/supabase/.gitignore @@ -6,3 +6,7 @@ .env.keys .env.local .env.*.local +.link.json +..link.json.*.tmp +.supabase.lock.*.tmp +.supabase.lock.lease