adds GetSupportedFileTypes to session api
This commit is contained in:
parent
172759462c
commit
efaa7442c8
@ -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"
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
@ -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)
|
||||
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);
|
||||
_chunkSizeInKiB = Math.Max(status.UploadFormChunkSize, 512);
|
||||
return _chunkSizeInKiB;
|
||||
return supportedFileTypes;
|
||||
}
|
||||
|
||||
private async ValueTask<SessionStatus> GetCachedSessionStatusAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (_currentStatus is not null)
|
||||
{
|
||||
return _currentStatus;
|
||||
}
|
||||
|
||||
_currentStatus = await GetStatusAsync(cancellationToken);
|
||||
return _currentStatus;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user