adds GetSupportedFileTypes to session api

This commit is contained in:
Philipp Häfelfinger 2022-11-16 00:22:33 +01:00
parent 172759462c
commit efaa7442c8
3 changed files with 49 additions and 8 deletions

View File

@ -46,6 +46,20 @@ public class SessionApiTests : ApiTestsBase
CorrectMethodShouldGetCalled("pwg.session.getStatus");
}
[Test]
public async Task GetSupportedFileTypes_should_get_allowed_types_from_session()
{
await LoginAsync();
SetJsonResult(GetStatusResponse());
var fileTypes = await _sessionApi.GetSupportedFileTypes();
fileTypes.Should().HaveCount(2);
fileTypes.Should().Contain("jpg");
fileTypes.Should().Contain("png");
CorrectMethodShouldGetCalled("pwg.session.getStatus");
}
[Test]
public async Task GetUploadChunkSizeAsync_should_only_get_size_from_session_once()
{
@ -91,7 +105,8 @@ public class SessionApiTests : ApiTestsBase
{
Username = "admin",
Version = "12.0.0",
UploadFormChunkSize = 512
UploadFormChunkSize = 512,
UploadFileTypes = "jpg,png"
}
};
}

View File

@ -32,4 +32,12 @@ public interface ISessionApi
/// <param name="cancellationToken"></param>
/// <returns></returns>
ValueTask<int> GetUploadChunkSizeAsync(CancellationToken cancellationToken = default);
/// <summary>
/// Gets the file types supported by the piwigo instance.
/// The chunk size is only get once and cleared on logout.
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
ValueTask<IReadOnlySet<string>> GetSupportedFileTypes(CancellationToken cancellationToken = default);
}

View File

@ -6,7 +6,7 @@ public class SessionApi : ISessionApi
{
private readonly IPiwigoContext _context;
private readonly ILogger<SessionApi> _logger;
private int _chunkSizeInKiB;
private SessionStatus? _currentStatus;
public SessionApi(IPiwigoContext context, ILogger<SessionApi> logger)
{
@ -17,7 +17,7 @@ public class SessionApi : ISessionApi
public async Task LogoutAsync(CancellationToken cancellationToken = default)
{
await _context.LogoutAsync(cancellationToken);
_chunkSizeInKiB = 0;
_currentStatus = null;
}
public Task LoginAsync(CancellationToken cancellationToken = default)
@ -33,13 +33,31 @@ public class SessionApi : ISessionApi
public async ValueTask<int> GetUploadChunkSizeAsync(CancellationToken cancellationToken = default)
{
if (_chunkSizeInKiB > 0)
{
return _chunkSizeInKiB;
const int minChunkSize = 512;
var status = await GetCachedSessionStatusAsync(cancellationToken);
return Math.Max(status.UploadFormChunkSize, minChunkSize);
}
var status = await GetStatusAsync(cancellationToken);
_chunkSizeInKiB = Math.Max(status.UploadFormChunkSize, 512);
return _chunkSizeInKiB;
public async ValueTask<IReadOnlySet<string>> GetSupportedFileTypes(CancellationToken cancellationToken = default)
{
var status = await GetCachedSessionStatusAsync(cancellationToken);
var supportedFileTypes = status.UploadFileTypes?.Split(",").Select(s => s.ToLower()).ToHashSet(StringComparer.OrdinalIgnoreCase);
if (supportedFileTypes is null)
{
throw new PiwigoException("Piwigo server seems to be misconfigured as the session does not provide any filetype to upload");
}
return supportedFileTypes;
}
private async ValueTask<SessionStatus> GetCachedSessionStatusAsync(CancellationToken cancellationToken)
{
if (_currentStatus is not null)
{
return _currentStatus;
}
_currentStatus = await GetStatusAsync(cancellationToken);
return _currentStatus;
}
}