piwigodotnet/PiwigoDotnet/Piwigo.Client/ImageApi.cs

117 lines
4.0 KiB
C#

using System.Globalization;
using Microsoft.Extensions.Logging;
using Piwigo.Client.Contract;
namespace Piwigo.Client;
public class ImageApi : IImageApi
{
private readonly IPiwigoContext _context;
private readonly ILogger<ImageApi> _logger;
public ImageApi(IPiwigoContext context, ILogger<ImageApi> logger)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_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<string, string>
{
{ "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<PiwigoResponse>(_logger, "pwg.images.addChunk", formParams, cancellationToken);
}
public async Task<PagedImages> GetImages(int albumId, bool recursive, PagingInfo page, ImageFilter filter, ImageOrder order = ImageOrder.Name,
CancellationToken cancellationToken = default)
{
var orderValue = order switch
{
ImageOrder.Id => "id",
ImageOrder.File => "file",
ImageOrder.Name => "name",
ImageOrder.Hit => "hit",
ImageOrder.RatingScore => "rating_score",
ImageOrder.DateCreation => "date_creation",
ImageOrder.DateAvailable => "date_available",
ImageOrder.Random => "random",
_ => throw new ArgumentOutOfRangeException(nameof(order), order, null)
};
var formParams = new Dictionary<string, string>
{
{ "cat_id", albumId.ToString() },
{ "recursive", recursive.ToString() },
{ "per_page", page.PageSize.ToString() },
{ "page", page.Page.ToString() },
{ "order", orderValue }
};
if (filter.MinRate.HasValue)
{
formParams.Add("f_min_rate", filter.MinRate.Value.ToString(CultureInfo.InvariantCulture));
}
if (filter.MaxRate.HasValue)
{
formParams.Add("f_max_rate", filter.MaxRate.Value.ToString(CultureInfo.InvariantCulture));
}
if (filter.MinHit.HasValue)
{
formParams.Add("f_min_hit", filter.MinHit.Value.ToString());
}
if (filter.MaxHit.HasValue)
{
formParams.Add("f_max_hit", filter.MaxHit.Value.ToString());
}
if (filter.MinRatio.HasValue)
{
formParams.Add("f_min_ratio", filter.MinRatio.Value.ToString(CultureInfo.InvariantCulture));
}
if (filter.MaxRatio.HasValue)
{
formParams.Add("f_max_ratio", filter.MaxRatio.Value.ToString(CultureInfo.InvariantCulture));
}
if (filter.MaxLevel.HasValue)
{
formParams.Add("f_max_level", filter.MaxLevel.Value.ToString());
}
if (filter.MinDateAvailable.HasValue)
{
formParams.Add("f_min_date_available", filter.MinDateAvailable.Value.ToString("yyyy-MM-dd"));
}
if (filter.MaxDataAvailable.HasValue)
{
formParams.Add("f_max_date_available", filter.MaxDataAvailable.Value.ToString("yyyy-MM-dd"));
}
if (filter.MinDateCreated.HasValue)
{
formParams.Add("f_min_date_created", filter.MinDateCreated.Value.ToString("yyyy-MM-dd"));
}
if (filter.MaxDateCreated.HasValue)
{
formParams.Add("f_max_date_created", filter.MaxDateCreated.Value.ToString("yyyy-MM-dd"));
}
var response = await _context.PostAsync<PiwigoResponse<PagedImages>>(_logger, "pwg.categories.getImages", formParams, cancellationToken);
return response.Result;
}
}