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"); 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] [Test]
public async Task GetUploadChunkSizeAsync_should_only_get_size_from_session_once() public async Task GetUploadChunkSizeAsync_should_only_get_size_from_session_once()
{ {
@ -91,7 +105,8 @@ public class SessionApiTests : ApiTestsBase
{ {
Username = "admin", Username = "admin",
Version = "12.0.0", Version = "12.0.0",
UploadFormChunkSize = 512 UploadFormChunkSize = 512,
UploadFileTypes = "jpg,png"
} }
}; };
} }

View File

@ -32,4 +32,12 @@ public interface ISessionApi
/// <param name="cancellationToken"></param> /// <param name="cancellationToken"></param>
/// <returns></returns> /// <returns></returns>
ValueTask<int> GetUploadChunkSizeAsync(CancellationToken cancellationToken = default); 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 IPiwigoContext _context;
private readonly ILogger<SessionApi> _logger; private readonly ILogger<SessionApi> _logger;
private int _chunkSizeInKiB; private SessionStatus? _currentStatus;
public SessionApi(IPiwigoContext context, ILogger<SessionApi> logger) public SessionApi(IPiwigoContext context, ILogger<SessionApi> logger)
{ {
@ -17,7 +17,7 @@ public class SessionApi : ISessionApi
public async Task LogoutAsync(CancellationToken cancellationToken = default) public async Task LogoutAsync(CancellationToken cancellationToken = default)
{ {
await _context.LogoutAsync(cancellationToken); await _context.LogoutAsync(cancellationToken);
_chunkSizeInKiB = 0; _currentStatus = null;
} }
public Task LoginAsync(CancellationToken cancellationToken = default) public Task LoginAsync(CancellationToken cancellationToken = default)
@ -33,13 +33,31 @@ public class SessionApi : ISessionApi
public async ValueTask<int> GetUploadChunkSizeAsync(CancellationToken cancellationToken = default) public async ValueTask<int> GetUploadChunkSizeAsync(CancellationToken cancellationToken = default)
{ {
if (_chunkSizeInKiB > 0) const int minChunkSize = 512;
var status = await GetCachedSessionStatusAsync(cancellationToken);
return Math.Max(status.UploadFormChunkSize, minChunkSize);
}
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)
{ {
return _chunkSizeInKiB; throw new PiwigoException("Piwigo server seems to be misconfigured as the session does not provide any filetype to upload");
} }
var status = await GetStatusAsync(cancellationToken); return supportedFileTypes;
_chunkSizeInKiB = Math.Max(status.UploadFormChunkSize, 512); }
return _chunkSizeInKiB;
private async ValueTask<SessionStatus> GetCachedSessionStatusAsync(CancellationToken cancellationToken)
{
if (_currentStatus is not null)
{
return _currentStatus;
}
_currentStatus = await GetStatusAsync(cancellationToken);
return _currentStatus;
} }
} }