adds add to image api
This commit is contained in:
parent
002734f692
commit
ef0d127b61
@ -39,12 +39,12 @@ public class ApiTestsBase
|
|||||||
HttpTest?.Dispose();
|
HttpTest?.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void SetOkResult()
|
protected void SetOkResult()
|
||||||
{
|
{
|
||||||
SetJsonResult(@"{stat: ""ok"", result: null}");
|
SetJsonResult(@"{stat: ""ok"", result: null}");
|
||||||
}
|
}
|
||||||
|
|
||||||
internal async Task SetJsonResultFromFileAsync(string fileName)
|
protected async Task SetJsonResultFromFileAsync(string fileName)
|
||||||
{
|
{
|
||||||
var assemblyLocation = Assembly.GetExecutingAssembly().Location;
|
var assemblyLocation = Assembly.GetExecutingAssembly().Location;
|
||||||
var directory = Path.GetDirectoryName(assemblyLocation) ?? Environment.CurrentDirectory;
|
var directory = Path.GetDirectoryName(assemblyLocation) ?? Environment.CurrentDirectory;
|
||||||
@ -57,7 +57,7 @@ public class ApiTestsBase
|
|||||||
SetJsonResult(fileContent);
|
SetJsonResult(fileContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void SetJsonResult(string json)
|
protected void SetJsonResult(string json)
|
||||||
{
|
{
|
||||||
HttpTest?.RespondWith(json);
|
HttpTest?.RespondWith(json);
|
||||||
}
|
}
|
||||||
@ -67,11 +67,21 @@ public class ApiTestsBase
|
|||||||
CorrectParamShouldGetSent("method", methodName);
|
CorrectParamShouldGetSent("method", methodName);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void CorrectParamShouldGetSent(string paramName, string methodName)
|
protected void ParamShouldNotGetSent(string paramName)
|
||||||
|
{
|
||||||
|
HttpTest?.ShouldHaveMadeACall().With(c =>
|
||||||
|
{
|
||||||
|
return !c.HttpRequestMessage.Content.As<CapturedMultipartContent>().Parts.OfType<CapturedStringContent>()
|
||||||
|
.Select(p => new { p.Headers.ContentDisposition?.Name, p.Content })
|
||||||
|
.Any(s => s.Name?.Equals(paramName) ?? false);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void CorrectParamShouldGetSent(string paramName, string expectedValue)
|
||||||
{
|
{
|
||||||
HttpTest?.ShouldHaveMadeACall().With(c =>
|
HttpTest?.ShouldHaveMadeACall().With(c =>
|
||||||
c.HttpRequestMessage.Content.As<CapturedMultipartContent>().Parts.OfType<CapturedStringContent>().Select(p => new { p.Headers.ContentDisposition?.Name, p.Content })
|
c.HttpRequestMessage.Content.As<CapturedMultipartContent>().Parts.OfType<CapturedStringContent>().Select(p => new { p.Headers.ContentDisposition?.Name, p.Content })
|
||||||
.Where(s => s.Name?.Equals(paramName) ?? false).Any(s => s.Content.Equals(methodName, StringComparison.OrdinalIgnoreCase)));
|
.Where(s => s.Name?.Equals(paramName) ?? false).Any(s => s.Content.Equals(expectedValue, StringComparison.OrdinalIgnoreCase)));
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void SetJsonResult<T>(PiwigoResponse<T> serverResponse)
|
internal void SetJsonResult<T>(PiwigoResponse<T> serverResponse)
|
||||||
|
@ -15,6 +15,55 @@ public class ImageApiTests : ApiTestsBase
|
|||||||
_imageApi = new ImageApi(Context, new NullLogger<ImageApi>());
|
_imageApi = new ImageApi(Context, new NullLogger<ImageApi>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Add()
|
||||||
|
{
|
||||||
|
SetJsonResult(@"{stat: ""ok"",
|
||||||
|
result: {
|
||||||
|
image_id: 1042,
|
||||||
|
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 = 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.Add(imageUpload);
|
||||||
|
|
||||||
|
uploaded.ImageId.Should().Be(1042);
|
||||||
|
uploaded.Url.Should().Be("https://localhost/image.jpg");
|
||||||
|
|
||||||
|
CorrectMethodShouldGetCalled("pwg.images.add");
|
||||||
|
|
||||||
|
// must not be sent or it will be used as an update.
|
||||||
|
// 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("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");
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public async Task ReadyForUpload_should_return_correct_value()
|
public async Task ReadyForUpload_should_return_correct_value()
|
||||||
{
|
{
|
||||||
|
13
PiwigoDotnet/Piwigo.Client/Contract/ImageUpload.cs
Normal file
13
PiwigoDotnet/Piwigo.Client/Contract/ImageUpload.cs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
namespace Piwigo.Client.Contract;
|
||||||
|
|
||||||
|
public record ImageUpload(string OriginalSum)
|
||||||
|
{
|
||||||
|
public string? OriginalFileName { get; init; }
|
||||||
|
public string? Name { get; init; }
|
||||||
|
public string? Author { get; init; }
|
||||||
|
public DateTime? CreatedAt { get; init; }
|
||||||
|
public string? Comment { get; init; }
|
||||||
|
public int? Level { get; init; }
|
||||||
|
public IReadOnlyCollection<(int AlbumId, int? Rank)>? Albums { get; init; }
|
||||||
|
public IReadOnlyCollection<int>? TagIds { get; init; }
|
||||||
|
}
|
12
PiwigoDotnet/Piwigo.Client/Contract/ImageUploaded.cs
Normal file
12
PiwigoDotnet/Piwigo.Client/Contract/ImageUploaded.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace Piwigo.Client.Contract;
|
||||||
|
|
||||||
|
public class ImageUploaded
|
||||||
|
{
|
||||||
|
[JsonProperty("image_id")]
|
||||||
|
public int? ImageId { get; init; }
|
||||||
|
|
||||||
|
[JsonProperty("url")]
|
||||||
|
public string? Url { get; init; }
|
||||||
|
}
|
@ -4,18 +4,17 @@ namespace Piwigo.Client;
|
|||||||
|
|
||||||
public interface IImageApi
|
public interface IImageApi
|
||||||
{
|
{
|
||||||
|
Task<ImageUploaded> Add(ImageUpload imageUpload, CancellationToken cancellationToken = default);
|
||||||
Task AddChunkAsync(byte[] data, string originalSum, int position, CancellationToken cancellationToken = default);
|
Task AddChunkAsync(byte[] data, string originalSum, int position, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
Task<bool> ReadyForUploadAsync(CancellationToken cancellationToken = default);
|
Task<bool> ReadyForUploadAsync(CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
Task<PagedImages> GetImages(int albumId, bool recursive, PagingInfo page, ImageFilter filter, ImageOrder order = ImageOrder.Name,
|
Task<PagedImages> GetImages(int albumId, bool recursive, PagingInfo page, ImageFilter filter, ImageOrder order = ImageOrder.Name,
|
||||||
CancellationToken cancellationToken = default);
|
CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
add
|
|
||||||
addComment
|
addComment
|
||||||
addFile
|
addFile
|
||||||
addSimple
|
|
||||||
checkFiles
|
checkFiles
|
||||||
delete
|
delete
|
||||||
deleteOrphans
|
deleteOrphans
|
||||||
|
@ -6,6 +6,9 @@ namespace Piwigo.Client;
|
|||||||
|
|
||||||
public class ImageApi : IImageApi
|
public class ImageApi : IImageApi
|
||||||
{
|
{
|
||||||
|
private const string DateFormat = "yyyy-MM-dd";
|
||||||
|
private const string DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
|
||||||
|
|
||||||
private readonly IPiwigoContext _context;
|
private readonly IPiwigoContext _context;
|
||||||
private readonly ILogger<ImageApi> _logger;
|
private readonly ILogger<ImageApi> _logger;
|
||||||
|
|
||||||
@ -31,6 +34,32 @@ public class ImageApi : IImageApi
|
|||||||
await _context.PostAsync<PiwigoResponse>(_logger, "pwg.images.addChunk", formParams, cancellationToken);
|
await _context.PostAsync<PiwigoResponse>(_logger, "pwg.images.addChunk", formParams, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<ImageUploaded> Add(ImageUpload imageUpload, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
|
||||||
|
var response = await _context.PostAsync<PiwigoResponse<ImageUploaded>>(_logger, "pwg.images.add", formParams, cancellationToken);
|
||||||
|
|
||||||
|
return response.Result;
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<bool> ReadyForUploadAsync(CancellationToken cancellationToken = default)
|
public async Task<bool> ReadyForUploadAsync(CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var response = await _context.PostAsync<PiwigoResponse<CheckUpload>>(_logger, "pwg.images.checkUpload", new Dictionary<string, string>(), cancellationToken);
|
var response = await _context.PostAsync<PiwigoResponse<CheckUpload>>(_logger, "pwg.images.checkUpload", new Dictionary<string, string>(), cancellationToken);
|
||||||
@ -99,22 +128,22 @@ public class ImageApi : IImageApi
|
|||||||
|
|
||||||
if (filter.MinDateAvailable.HasValue)
|
if (filter.MinDateAvailable.HasValue)
|
||||||
{
|
{
|
||||||
formParams.Add("f_min_date_available", filter.MinDateAvailable.Value.ToString("yyyy-MM-dd"));
|
formParams.Add("f_min_date_available", filter.MinDateAvailable.Value.ToString(DateFormat));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filter.MaxDataAvailable.HasValue)
|
if (filter.MaxDataAvailable.HasValue)
|
||||||
{
|
{
|
||||||
formParams.Add("f_max_date_available", filter.MaxDataAvailable.Value.ToString("yyyy-MM-dd"));
|
formParams.Add("f_max_date_available", filter.MaxDataAvailable.Value.ToString(DateFormat));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filter.MinDateCreated.HasValue)
|
if (filter.MinDateCreated.HasValue)
|
||||||
{
|
{
|
||||||
formParams.Add("f_min_date_created", filter.MinDateCreated.Value.ToString("yyyy-MM-dd"));
|
formParams.Add("f_min_date_created", filter.MinDateCreated.Value.ToString(DateFormat));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filter.MaxDateCreated.HasValue)
|
if (filter.MaxDateCreated.HasValue)
|
||||||
{
|
{
|
||||||
formParams.Add("f_max_date_created", filter.MaxDateCreated.Value.ToString("yyyy-MM-dd"));
|
formParams.Add("f_max_date_created", filter.MaxDateCreated.Value.ToString(DateFormat));
|
||||||
}
|
}
|
||||||
|
|
||||||
var response = await _context.PostAsync<PiwigoResponse<PagedImages>>(_logger, "pwg.categories.getImages", formParams, cancellationToken);
|
var response = await _context.PostAsync<PiwigoResponse<PagedImages>>(_logger, "pwg.categories.getImages", formParams, cancellationToken);
|
||||||
|
Loading…
Reference in New Issue
Block a user