adds an add overload to update an image
This commit is contained in:
parent
ef0d127b61
commit
a8ad6ca9ff
@ -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<int, int?>(3, 10),
|
||||
new ValueTuple<int, int?>(5, null),
|
||||
new ValueTuple<int, int?>(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<int> { 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<int, int?>(3, 10),
|
||||
new ValueTuple<int, int?>(5, null),
|
||||
new ValueTuple<int, int?>(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<int> { 2, 4, 8 }
|
||||
};
|
||||
return imageUpload;
|
||||
}
|
||||
}
|
@ -4,7 +4,8 @@ namespace Piwigo.Client;
|
||||
|
||||
public interface IImageApi
|
||||
{
|
||||
Task<ImageUploaded> Add(ImageUpload imageUpload, CancellationToken cancellationToken = default);
|
||||
Task<ImageUploaded> AddAsync(ImageUpload imageUpload, CancellationToken cancellationToken = default);
|
||||
Task<ImageUploaded> UpdateAsync(int imageId, ImageUpload imageUpload, CancellationToken cancellationToken = default);
|
||||
Task AddChunkAsync(byte[] data, string originalSum, int position, CancellationToken cancellationToken = default);
|
||||
|
||||
Task<bool> ReadyForUploadAsync(CancellationToken cancellationToken = default);
|
||||
|
@ -34,30 +34,14 @@ public class ImageApi : IImageApi
|
||||
await _context.PostAsync<PiwigoResponse>(_logger, "pwg.images.addChunk", formParams, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<ImageUploaded> Add(ImageUpload imageUpload, CancellationToken cancellationToken = default)
|
||||
public Task<ImageUploaded> AddAsync(ImageUpload imageUpload, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var formParams = new Dictionary<string, string>
|
||||
{
|
||||
{ "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<PiwigoResponse<ImageUploaded>>(_logger, "pwg.images.add", formParams, cancellationToken);
|
||||
|
||||
return response.Result;
|
||||
public Task<ImageUploaded> UpdateAsync(int imageId, ImageUpload imageUpload, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return AddOrUpdateAsync(imageId, imageUpload, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<bool> ReadyForUploadAsync(CancellationToken cancellationToken = default)
|
||||
@ -149,4 +133,31 @@ public class ImageApi : IImageApi
|
||||
var response = await _context.PostAsync<PiwigoResponse<PagedImages>>(_logger, "pwg.categories.getImages", formParams, cancellationToken);
|
||||
return response.Result;
|
||||
}
|
||||
|
||||
private async Task<ImageUploaded> AddOrUpdateAsync(int? imageId, ImageUpload imageUpload, CancellationToken cancellationToken)
|
||||
{
|
||||
var formParams = new Dictionary<string, string>
|
||||
{
|
||||
{ "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<PiwigoResponse<ImageUploaded>>(_logger, "pwg.images.add", formParams, cancellationToken);
|
||||
|
||||
return response.Result;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user