From a8ad6ca9ffd3ecde891e0ef0e6d90531dbaebc37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Sat, 22 Oct 2022 22:42:09 +0200 Subject: [PATCH] adds an add overload to update an image --- .../Piwigo.Client.Tests/ImageApiTests.cs | 81 +++++++++++++------ PiwigoDotnet/Piwigo.Client/IImageApi.cs | 3 +- PiwigoDotnet/Piwigo.Client/ImageApi.cs | 55 ++++++++----- 3 files changed, 93 insertions(+), 46 deletions(-) diff --git a/PiwigoDotnet/Piwigo.Client.Tests/ImageApiTests.cs b/PiwigoDotnet/Piwigo.Client.Tests/ImageApiTests.cs index 09f02dc..f27896b 100644 --- a/PiwigoDotnet/Piwigo.Client.Tests/ImageApiTests.cs +++ b/PiwigoDotnet/Piwigo.Client.Tests/ImageApiTests.cs @@ -16,7 +16,7 @@ public class ImageApiTests : ApiTestsBase } [Test] - public async Task Add() + public async Task Update_should_pass_data_to_piwigo() { SetJsonResult(@"{stat: ""ok"", result: { @@ -24,26 +24,38 @@ public class ImageApiTests : ApiTestsBase url: ""https://localhost/image.jpg"" }}"); - var albums = new List<(int AlbumId, int? Rank)> - { - new ValueTuple(3, 10), - new ValueTuple(5, null), - new ValueTuple(7, 11) - }; + var imageUpload = GetImageUpload(); - var imageUpload = new ImageUpload("md5Sum") - { - Author = "unit test", - Comment = "perfect image", - Level = 42, - Name = "Image001.jpg", - CreatedAt = new DateTime(2022, 10, 22, 21, 50, 42), - OriginalFileName = "RAW-Image001.jpg", - Albums = albums, - TagIds = new List { 2, 4, 8 } - }; + var uploaded = await _imageApi.UpdateAsync(1234, imageUpload); - var uploaded = await _imageApi.Add(imageUpload); + uploaded.ImageId.Should().Be(1042); + uploaded.Url.Should().Be("https://localhost/image.jpg"); + + CorrectMethodShouldGetCalled("pwg.images.add"); + + CorrectParamShouldGetSent("original_filename", imageUpload.OriginalFileName!); + CorrectParamShouldGetSent("name", imageUpload.Name!); + CorrectParamShouldGetSent("author", imageUpload.Author!); + CorrectParamShouldGetSent("date_creation", "2022-10-22 21:50:42"); + CorrectParamShouldGetSent("comment", imageUpload.Comment!); + CorrectParamShouldGetSent("level", imageUpload.Level.ToString()!); + CorrectParamShouldGetSent("categories", "3,10;5;7,11"); + CorrectParamShouldGetSent("tag_ids", "2,4,8"); + CorrectParamShouldGetSent("image_id", "1234"); + } + + [Test] + public async Task Add_should_pass_data_to_piwigo() + { + SetJsonResult(@"{stat: ""ok"", + result: { + image_id: 1042, + url: ""https://localhost/image.jpg"" + }}"); + + var imageUpload = GetImageUpload(); + + var uploaded = await _imageApi.AddAsync(imageUpload); uploaded.ImageId.Should().Be(1042); uploaded.Url.Should().Be("https://localhost/image.jpg"); @@ -54,11 +66,11 @@ public class ImageApiTests : ApiTestsBase // Piwigo uses the same request for add or update depending on this parameter ParamShouldNotGetSent("image_id"); - CorrectParamShouldGetSent("original_filename", imageUpload.OriginalFileName); - CorrectParamShouldGetSent("name", imageUpload.Name); - CorrectParamShouldGetSent("author", imageUpload.Author); + CorrectParamShouldGetSent("original_filename", imageUpload.OriginalFileName!); + CorrectParamShouldGetSent("name", imageUpload.Name!); + CorrectParamShouldGetSent("author", imageUpload.Author!); CorrectParamShouldGetSent("date_creation", "2022-10-22 21:50:42"); - CorrectParamShouldGetSent("comment", imageUpload.Comment); + CorrectParamShouldGetSent("comment", imageUpload.Comment!); CorrectParamShouldGetSent("level", imageUpload.Level.ToString()!); CorrectParamShouldGetSent("categories", "3,10;5;7,11"); CorrectParamShouldGetSent("tag_ids", "2,4,8"); @@ -113,4 +125,27 @@ public class ImageApiTests : ApiTestsBase await Verify(result); } + + private static ImageUpload GetImageUpload() + { + var albums = new List<(int AlbumId, int? Rank)> + { + new ValueTuple(3, 10), + new ValueTuple(5, null), + new ValueTuple(7, 11) + }; + + var imageUpload = new ImageUpload("md5Sum") + { + Author = "unit test", + Comment = "perfect image", + Level = 42, + Name = "Image001.jpg", + CreatedAt = new DateTime(2022, 10, 22, 21, 50, 42), + OriginalFileName = "RAW-Image001.jpg", + Albums = albums, + TagIds = new List { 2, 4, 8 } + }; + return imageUpload; + } } \ No newline at end of file diff --git a/PiwigoDotnet/Piwigo.Client/IImageApi.cs b/PiwigoDotnet/Piwigo.Client/IImageApi.cs index c0dd145..c221963 100644 --- a/PiwigoDotnet/Piwigo.Client/IImageApi.cs +++ b/PiwigoDotnet/Piwigo.Client/IImageApi.cs @@ -4,7 +4,8 @@ namespace Piwigo.Client; public interface IImageApi { - Task Add(ImageUpload imageUpload, CancellationToken cancellationToken = default); + Task AddAsync(ImageUpload imageUpload, CancellationToken cancellationToken = default); + Task UpdateAsync(int imageId, ImageUpload imageUpload, CancellationToken cancellationToken = default); Task AddChunkAsync(byte[] data, string originalSum, int position, CancellationToken cancellationToken = default); Task ReadyForUploadAsync(CancellationToken cancellationToken = default); diff --git a/PiwigoDotnet/Piwigo.Client/ImageApi.cs b/PiwigoDotnet/Piwigo.Client/ImageApi.cs index ebc0bf3..b5b09b9 100644 --- a/PiwigoDotnet/Piwigo.Client/ImageApi.cs +++ b/PiwigoDotnet/Piwigo.Client/ImageApi.cs @@ -34,30 +34,14 @@ public class ImageApi : IImageApi await _context.PostAsync(_logger, "pwg.images.addChunk", formParams, cancellationToken); } - public async Task Add(ImageUpload imageUpload, CancellationToken cancellationToken = default) + public Task AddAsync(ImageUpload imageUpload, CancellationToken cancellationToken = default) { - var formParams = new Dictionary - { - { "original_sum", imageUpload.OriginalSum }, - { "check_uniqueness", "true" } - }; + return AddOrUpdateAsync(null, imageUpload, cancellationToken); + } - formParams.AddIfValueNotNull("original_filename", imageUpload.OriginalFileName); - formParams.AddIfValueNotNull("name", imageUpload.Name); - formParams.AddIfValueNotNull("author", imageUpload.Author); - formParams.AddIfValueNotNull("date_creation", imageUpload.CreatedAt?.ToString(DateTimeFormat)); - formParams.AddIfValueNotNull("comment", imageUpload.Comment); - formParams.AddIfValueNotNull("level", imageUpload.Level?.ToString()); - - var albums = imageUpload.Albums != null ? string.Join(";", imageUpload.Albums.Select(a => a.Rank.HasValue ? $"{a.AlbumId},{a.Rank}" : $"{a.AlbumId}")) : null; - formParams.AddIfValueNotNull("categories", albums); - - var tags = imageUpload.TagIds != null ? string.Join(",", imageUpload.TagIds.Select(t => t.ToString())) : null; - formParams.AddIfValueNotNull("tag_ids", tags); - - var response = await _context.PostAsync>(_logger, "pwg.images.add", formParams, cancellationToken); - - return response.Result; + public Task UpdateAsync(int imageId, ImageUpload imageUpload, CancellationToken cancellationToken = default) + { + return AddOrUpdateAsync(imageId, imageUpload, cancellationToken); } public async Task ReadyForUploadAsync(CancellationToken cancellationToken = default) @@ -149,4 +133,31 @@ public class ImageApi : IImageApi var response = await _context.PostAsync>(_logger, "pwg.categories.getImages", formParams, cancellationToken); return response.Result; } + + private async Task AddOrUpdateAsync(int? imageId, ImageUpload imageUpload, CancellationToken cancellationToken) + { + var formParams = new Dictionary + { + { "original_sum", imageUpload.OriginalSum }, + { "check_uniqueness", "true" } + }; + + formParams.AddIfValueNotNull("original_filename", imageUpload.OriginalFileName); + formParams.AddIfValueNotNull("name", imageUpload.Name); + formParams.AddIfValueNotNull("author", imageUpload.Author); + formParams.AddIfValueNotNull("date_creation", imageUpload.CreatedAt?.ToString(DateTimeFormat)); + formParams.AddIfValueNotNull("comment", imageUpload.Comment); + formParams.AddIfValueNotNull("level", imageUpload.Level?.ToString()); + + var albums = imageUpload.Albums != null ? string.Join(";", imageUpload.Albums.Select(a => a.Rank.HasValue ? $"{a.AlbumId},{a.Rank}" : $"{a.AlbumId}")) : null; + formParams.AddIfValueNotNull("categories", albums); + + var tags = imageUpload.TagIds != null ? string.Join(",", imageUpload.TagIds.Select(t => t.ToString())) : null; + formParams.AddIfValueNotNull("tag_ids", tags); + formParams.AddIfValueNotNull("image_id", imageId?.ToString()); + + var response = await _context.PostAsync>(_logger, "pwg.images.add", formParams, cancellationToken); + + return response.Result; + } } \ No newline at end of file