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 @@ -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();
Expand Down
15 changes: 15 additions & 0 deletions packages/Storage/Storage.Tests/Files/StorageFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]> UploadThreeNumbered()
{
var names = new[]
Expand Down
324 changes: 180 additions & 144 deletions packages/Storage/Storage/Interfaces/IStorageFileApi.cs
Original file line number Diff line number Diff line change
@@ -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<TFileObject>
where TFileObject : FileObject
{
public interface IStorageFileApi<TFileObject>
where TFileObject : FileObject
{
ClientOptions Options { get; }
Task<string> CreateSignedUrl(
string path,
int expiresIn,
TransformOptions? transformOptions = null,
DownloadOptions? options = null
);
Task<List<CreateSignedUrlsResponse>?> CreateSignedUrls(
List<string> paths,
int expiresIn,
DownloadOptions? options = null
);
Task<byte[]> Download(string supabasePath, EventHandler<float>? onProgress = null, CancellationToken cancellationToken = default, string? cacheNonce = null);
Task<byte[]> Download(
string supabasePath,
TransformOptions? transformOptions = null,
EventHandler<float>? onProgress = null,
CancellationToken cancellationToken = default,
string? cacheNonce = null
);
Task<string> Download(
string supabasePath,
string localPath,
EventHandler<float>? onProgress = null,
CancellationToken cancellationToken = default,
string? cacheNonce = null
);
Task<string> Download(
string supabasePath,
string localPath,
TransformOptions? transformOptions = null,
EventHandler<float>? onProgress = null,
CancellationToken cancellationToken = default,
string? cacheNonce = null
);
Task<byte[]> DownloadPublicFile(
string supabasePath,
TransformOptions? transformOptions = null,
EventHandler<float>? onProgress = null,
CancellationToken cancellationToken = default,
string? cacheNonce = null
);
Task<string> DownloadPublicFile(
string supabasePath,
string localPath,
TransformOptions? transformOptions = null,
EventHandler<float>? onProgress = null,
CancellationToken cancellationToken = default,
string? cacheNonce = null
);
string GetPublicUrl(
string path,
TransformOptions? transformOptions = null,
DownloadOptions? options = null
);
Task<List<TFileObject>?> List(string path = "", SearchOptions? options = null);
/// <summary>Lists the objects under a prefix, one page at a time.</summary>
Task<SearchV2Result?> ListV2Async(SearchV2Options? options = null, CancellationToken cancellationToken = default);
Task<FileObjectV2?> Info(string path);
Task<bool> Move(string fromPath, string toPath, DestinationOptions? options = null);
Task<bool> Copy(string fromPath, string toPath, DestinationOptions? options = null);
Task<TFileObject?> Remove(string path);
Task<List<TFileObject>?> Remove(List<string> paths);
Task<string> Update(
byte[] data,
string supabasePath,
FileOptions? options = null,
EventHandler<float>? onProgress = null
);
Task<string> Update(
string localFilePath,
string supabasePath,
FileOptions? options = null,
EventHandler<float>? onProgress = null
);
Task<string> Upload(
byte[] data,
string supabasePath,
FileOptions? options = null,
EventHandler<float>? onProgress = null,
bool inferContentType = true,
CancellationToken cancellationToken = default
);
Task<string> Upload(
string localFilePath,
string supabasePath,
FileOptions? options = null,
EventHandler<float>? onProgress = null,
bool inferContentType = true,
CancellationToken cancellationToken = default
);
Task UploadOrResume(
string localPath,
string fileName,
FileOptions options,
EventHandler<float>? onProgress = null,
CancellationToken cancellationToken = default
);
Task UploadOrResume(
byte[] data,
string fileName,
FileOptions options,
EventHandler<float>? onProgress = null,
CancellationToken cancellationToken = default
);
Task<string> UploadToSignedUrl(
byte[] data,
UploadSignedUrl url,
FileOptions? options = null,
EventHandler<float>? onProgress = null,
bool inferContentType = true
);
Task<string> UploadToSignedUrl(
string localFilePath,
UploadSignedUrl url,
FileOptions? options = null,
EventHandler<float>? onProgress = null,
bool inferContentType = true
);
Task<UploadSignedUrl> CreateUploadSignedUrl(string supabasePath);

/// <summary>
/// Purges the CDN cache for a single object. Requires a service-role key.
/// </summary>
/// <param name="path">The object path within the bucket to purge.</param>
/// <param name="options">
/// When <see cref="PurgeCacheOptions.Transformations"/> is <c>true</c>, only the transformed
/// variants are purged; otherwise every cached version is purged.
/// </param>
/// <param name="cancellationToken">Token used to cancel the request.</param>
/// <returns>The service acknowledgement of the purge.</returns>
/// <example>
/// <code>
/// await storage.From("avatars").PurgeCache("folder/avatar.png");
/// </code>
/// </example>
Task<GenericResponse?> PurgeCache(string path, PurgeCacheOptions? options = null, CancellationToken cancellationToken = default);
}
}
ClientOptions Options { get; }

Task<string> CreateSignedUrl(
string path,
int expiresIn,
TransformOptions? transformOptions = null,
DownloadOptions? options = null
);

Task<List<CreateSignedUrlsResponse>?> CreateSignedUrls(
List<string> paths,
int expiresIn,
DownloadOptions? options = null
);

Task<byte[]> Download(string supabasePath, EventHandler<float>? onProgress = null,
CancellationToken cancellationToken = default, string? cacheNonce = null);

Task<byte[]> Download(
string supabasePath,
TransformOptions? transformOptions = null,
EventHandler<float>? onProgress = null,
CancellationToken cancellationToken = default,
string? cacheNonce = null
);

Task<string> Download(
string supabasePath,
string localPath,
EventHandler<float>? onProgress = null,
CancellationToken cancellationToken = default,
string? cacheNonce = null
);

Task<string> Download(
string supabasePath,
string localPath,
TransformOptions? transformOptions = null,
EventHandler<float>? onProgress = null,
CancellationToken cancellationToken = default,
string? cacheNonce = null
);

Task<byte[]> DownloadPublicFile(
string supabasePath,
TransformOptions? transformOptions = null,
EventHandler<float>? onProgress = null,
CancellationToken cancellationToken = default,
string? cacheNonce = null
);

Task<string> DownloadPublicFile(
string supabasePath,
string localPath,
TransformOptions? transformOptions = null,
EventHandler<float>? onProgress = null,
CancellationToken cancellationToken = default,
string? cacheNonce = null
);

string GetPublicUrl(
string path,
TransformOptions? transformOptions = null,
DownloadOptions? options = null
);

Task<List<TFileObject>?> List(string path = "", SearchOptions? options = null);

/// <summary>Lists the objects under a prefix, one page at a time.</summary>
Task<SearchV2Result?> ListV2Async(SearchV2Options? options = null, CancellationToken cancellationToken = default);
Task<FileObjectV2?> Info(string path);
Task<bool> Move(string fromPath, string toPath, DestinationOptions? options = null);
Task<bool> Copy(string fromPath, string toPath, DestinationOptions? options = null);
Task<TFileObject?> Remove(string path);
Task<List<TFileObject>?> Remove(List<string> paths);

Task<string> Update(
byte[] data,
string supabasePath,
FileOptions? options = null,
EventHandler<float>? onProgress = null
);

Task<string> Update(
string localFilePath,
string supabasePath,
FileOptions? options = null,
EventHandler<float>? onProgress = null
);

Task<string> Upload(
byte[] data,
string supabasePath,
FileOptions? options = null,
EventHandler<float>? onProgress = null,
bool inferContentType = true,
CancellationToken cancellationToken = default
);

Task<string> Upload(
string localFilePath,
string supabasePath,
FileOptions? options = null,
EventHandler<float>? onProgress = null,
bool inferContentType = true,
CancellationToken cancellationToken = default
);

Task UploadOrResume(
string localPath,
string fileName,
FileOptions options,
EventHandler<float>? onProgress = null,
CancellationToken cancellationToken = default
);

Task UploadOrResume(
byte[] data,
string fileName,
FileOptions options,
EventHandler<float>? onProgress = null,
CancellationToken cancellationToken = default
);

Task<string> UploadToSignedUrl(
byte[] data,
UploadSignedUrl url,
FileOptions? options = null,
EventHandler<float>? onProgress = null,
bool inferContentType = true
);

Task<string> UploadToSignedUrl(
string localFilePath,
UploadSignedUrl url,
FileOptions? options = null,
EventHandler<float>? onProgress = null,
bool inferContentType = true
);

Task<UploadSignedUrl> CreateUploadSignedUrl(string supabasePath);

/// <summary>
/// Purges the CDN cache for a single object. Requires a service-role key.
/// </summary>
/// <param name="path">The object path within the bucket to purge.</param>
/// <param name="options">
/// When <see cref="PurgeCacheOptions.Transformations" /> is <c>true</c>, only the transformed
/// variants are purged; otherwise every cached version is purged.
/// </param>
/// <param name="cancellationToken">Token used to cancel the request.</param>
/// <returns>The service acknowledgement of the purge.</returns>
/// <example>
/// <code>
/// await storage.From("avatars").PurgeCache("folder/avatar.png");
/// </code>
/// </example>
Task<GenericResponse?> PurgeCache(string path, PurgeCacheOptions? options = null,
CancellationToken cancellationToken = default);

/// <summary>Checks whether a file exists by reading its metadata.</summary>
/// <remarks>
/// Returns <c>false</c> only for the <c>NoSuchKey</c> error code or an HTTP 404 without
/// an error code. Other failures, including <c>InvalidJWT</c>, <c>NoSuchBucket</c>, and
/// network errors, throw an exception.
/// </remarks>
/// <param name="path">The file path within the bucket, including the file name (for example, <c>folder/image.png</c>).</param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>A task that resolves to <c>true</c> if the file exists or <c>false</c> if the key does not exist.</returns>
/// <example>
/// <code>
/// var exists = await storage.From("avatars").Exists("folder/avatar1.png");
/// </code>
/// </example>
Task<bool> Exists(string path, 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.IStorageFileApi<TFileObject>.Exists(string! path, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<bool>!
Supabase.Storage.StorageFileApi.Exists(string! path, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<bool>!
Loading