diff --git a/src/CarpaNet/Blob/ATProtoClientBlobExtensions.cs b/src/CarpaNet/Blob/ATProtoClientBlobExtensions.cs index fbec484..a7082d8 100644 --- a/src/CarpaNet/Blob/ATProtoClientBlobExtensions.cs +++ b/src/CarpaNet/Blob/ATProtoClientBlobExtensions.cs @@ -5,6 +5,7 @@ using System.Net.Http.Headers; using System.Text.Json; using System.Threading; using System.Threading.Tasks; +using CarpaNet; using CarpaNet.Auth; namespace CarpaNet.Blob; @@ -86,6 +87,51 @@ public static class ATProtoClientBlobExtensions return await client.UploadBlobAsync(stream, mimeType, cancellationToken).ConfigureAwait(false); } + /// + /// Downloads a blob from a PDS. + /// + /// The ATProto client. + /// The DID of the repo that owns the blob. + /// The CID of the blob to download. + /// Cancellation token. + /// The blob data as a byte array. + public static async Task DownloadBlobAsync( + this ATProtoClient client, + ATDid did, + ATCid cid, + CancellationToken cancellationToken = default) + { + var url = new Uri(client.BaseUrl, $"/xrpc/com.atproto.sync.getBlob?did={did}&cid={cid}"); + + using var httpClient = new HttpClient(); + using var request = new HttpRequestMessage(HttpMethod.Get, url); + + if (client.IsAuthenticated && client.TokenProvider != null) + { + var accessToken = await client.TokenProvider.GetAccessTokenAsync(cancellationToken).ConfigureAwait(false); + if (!string.IsNullOrEmpty(accessToken)) + { + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); + } + } + + var response = await httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false); + + if (!response.IsSuccessStatusCode) + { + var errorContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false); + throw new ATProtoException( + $"Blob download failed: {errorContent}", + statusCode: response.StatusCode); + } + +#if NET8_0_OR_GREATER + return await response.Content.ReadAsByteArrayAsync(cancellationToken).ConfigureAwait(false); +#else + return await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false); +#endif + } + private static async Task UploadBlobInternalAsync( Uri baseUrl, Stream content, diff --git a/tests/CarpaNet.UnitTests/Blob/BlobTests.cs b/tests/CarpaNet.UnitTests/Blob/BlobTests.cs index 97d610b..52c4f6d 100644 --- a/tests/CarpaNet.UnitTests/Blob/BlobTests.cs +++ b/tests/CarpaNet.UnitTests/Blob/BlobTests.cs @@ -1,6 +1,8 @@ using System; using System.IO; using System.Text.Json; +using System.Threading.Tasks; +using CarpaNet; using CarpaNet.Blob; using Xunit; @@ -67,6 +69,33 @@ public class BlobRefTests } } +public class BlobDownloadTests +{ + private static ATProtoClientOptions CreateDefaultOptions() + { + return new ATProtoClientOptions + { + JsonOptions = TestHelpers.CreateJsonOptions(), + CborContext = CarpaNet.Cbor.CborSerializerContext.Default, + BaseUrl = new Uri(BlueskyServices.Entryway) + }; + } + + [Fact] + public async Task DownloadBlobAsync_ReturnsNonEmptyBytes() + { + using var client = ATProtoClient.Create(CreateDefaultOptions()); + + ATDid did = "did:plc:okblbaji7rz243bluudjlgxt"; + ATCid cid = "bafkreig2cadsvr24lz4n2jd65v4j7k5z36is6d4mmspmkci3mxta2caw2i"; + + var data = await client.DownloadBlobAsync(did, cid); + + Assert.NotNull(data); + Assert.True(data.Length > 0, "Downloaded blob should not be empty."); + } +} + public class BlobExtensionTests { [Fact]