From efaa7442c8feb58373b644e88f256c924b271360 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Wed, 16 Nov 2022 00:22:33 +0100 Subject: [PATCH] adds GetSupportedFileTypes to session api --- src/Piwigo.Client.Tests/SessionApiTests.cs | 17 +++++++++++- src/Piwigo.Client/Session/ISessionApi.cs | 8 ++++++ src/Piwigo.Client/Session/SessionApi.cs | 32 +++++++++++++++++----- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/Piwigo.Client.Tests/SessionApiTests.cs b/src/Piwigo.Client.Tests/SessionApiTests.cs index a5ac65e..4599354 100644 --- a/src/Piwigo.Client.Tests/SessionApiTests.cs +++ b/src/Piwigo.Client.Tests/SessionApiTests.cs @@ -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" } }; } diff --git a/src/Piwigo.Client/Session/ISessionApi.cs b/src/Piwigo.Client/Session/ISessionApi.cs index 8833e01..a6c4c38 100644 --- a/src/Piwigo.Client/Session/ISessionApi.cs +++ b/src/Piwigo.Client/Session/ISessionApi.cs @@ -32,4 +32,12 @@ public interface ISessionApi /// /// ValueTask GetUploadChunkSizeAsync(CancellationToken cancellationToken = default); + + /// + /// Gets the file types supported by the piwigo instance. + /// The chunk size is only get once and cleared on logout. + /// + /// + /// + ValueTask> GetSupportedFileTypes(CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/src/Piwigo.Client/Session/SessionApi.cs b/src/Piwigo.Client/Session/SessionApi.cs index c6dd37a..d3de198 100644 --- a/src/Piwigo.Client/Session/SessionApi.cs +++ b/src/Piwigo.Client/Session/SessionApi.cs @@ -6,7 +6,7 @@ public class SessionApi : ISessionApi { private readonly IPiwigoContext _context; private readonly ILogger _logger; - private int _chunkSizeInKiB; + private SessionStatus? _currentStatus; public SessionApi(IPiwigoContext context, ILogger 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 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> 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); - _chunkSizeInKiB = Math.Max(status.UploadFormChunkSize, 512); - return _chunkSizeInKiB; + return supportedFileTypes; + } + + private async ValueTask GetCachedSessionStatusAsync(CancellationToken cancellationToken) + { + if (_currentStatus is not null) + { + return _currentStatus; + } + + _currentStatus = await GetStatusAsync(cancellationToken); + return _currentStatus; } } \ No newline at end of file