diff --git a/PiwigoDotnet/Piwigo.Client.Tests/ImageApiTests.cs b/PiwigoDotnet/Piwigo.Client.Tests/ImageApiTests.cs index 68d8656..135a09f 100644 --- a/PiwigoDotnet/Piwigo.Client.Tests/ImageApiTests.cs +++ b/PiwigoDotnet/Piwigo.Client.Tests/ImageApiTests.cs @@ -1,3 +1,4 @@ +using System.Text; using Microsoft.Extensions.Logging.Abstractions; using Piwigo.Client.Contract; @@ -14,6 +15,24 @@ public class ImageApiTests : ApiTestsBase _imageApi = new ImageApi(Context, new NullLogger()); } + [Test] + public async Task AddChunk_should_post_base64_encoded_data() + { + SetOkResult(); + + var bytes = Encoding.UTF8.GetBytes("PiwigoTestString"); + var expectedBase64 = Convert.ToBase64String(bytes); + + await _imageApi.AddChunkAsync(bytes, "origSum", 0); + + CorrectMethodShouldGetCalled("pwg.images.addChunk"); + + CorrectParamShouldGetSent("data", expectedBase64); + CorrectParamShouldGetSent("original_sum", "origSum"); + CorrectParamShouldGetSent("type", "file"); + CorrectParamShouldGetSent("position", "0"); + } + [Test] public async Task GetImages_should_return_expected_images() { diff --git a/PiwigoDotnet/Piwigo.Client/IImageApi.cs b/PiwigoDotnet/Piwigo.Client/IImageApi.cs index 60236aa..b1bdb4e 100644 --- a/PiwigoDotnet/Piwigo.Client/IImageApi.cs +++ b/PiwigoDotnet/Piwigo.Client/IImageApi.cs @@ -4,6 +4,8 @@ namespace Piwigo.Client; public interface IImageApi { + Task AddChunkAsync(byte[] data, string originalSum, int position, CancellationToken cancellationToken = default); + Task GetImages(int albumId, bool recursive, PagingInfo page, ImageFilter filter, ImageOrder order = ImageOrder.Name, CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/PiwigoDotnet/Piwigo.Client/ImageApi.cs b/PiwigoDotnet/Piwigo.Client/ImageApi.cs index 0299592..2f026db 100644 --- a/PiwigoDotnet/Piwigo.Client/ImageApi.cs +++ b/PiwigoDotnet/Piwigo.Client/ImageApi.cs @@ -15,6 +15,22 @@ public class ImageApi : IImageApi _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } + public async Task AddChunkAsync(byte[] data, string originalSum, int position, CancellationToken cancellationToken = default) + { + var base64Data = Convert.ToBase64String(data); + + var formParams = new Dictionary + { + { "data", base64Data }, + { "original_sum", originalSum }, + // type = file is required for compatibility reasons. + // There is no other value allowed + { "type", "file" }, + { "position", position.ToString() } + }; + await _context.PostAsync(_logger, "pwg.images.addChunk", formParams, cancellationToken); + } + public async Task GetImages(int albumId, bool recursive, PagingInfo page, ImageFilter filter, ImageOrder order = ImageOrder.Name, CancellationToken cancellationToken = default) {