upload file extension methods to make usages easier

This commit is contained in:
Philipp Häfelfinger 2022-11-17 23:11:55 +01:00
parent 326b76b54b
commit b23ef3eebb
1 changed files with 35 additions and 6 deletions

View File

@ -4,13 +4,47 @@ namespace Piwigo.Client;
public static class PiwigoClientExtensions
{
public static async Task<ImageUploaded> UploadImageAsync(this IPiwigoClient client, FileInfo imageToUpload, string? md5Sum, CancellationToken cancellationToken = default)
public static async Task<ImageUploaded> UploadImageAsync(this IPiwigoClient client, FileInfo imageToUpload, ImageUpload metaData, CancellationToken cancellationToken = default)
{
if (client == null)
{
throw new ArgumentNullException(nameof(client));
}
if (imageToUpload == null)
{
throw new ArgumentNullException(nameof(imageToUpload));
}
if (metaData == null)
{
throw new ArgumentNullException(nameof(metaData));
}
await client.CheckSupportedFileTypeAsync(imageToUpload, cancellationToken);
await UploadChunksAsync(client, imageToUpload, metaData.OriginalSum, cancellationToken);
return await client.Image.AddAsync(metaData, cancellationToken);
}
public static async Task<ImageUploaded> UploadImageAsync(this IPiwigoClient client, FileInfo imageToUpload, CancellationToken cancellationToken = default)
{
if (client == null)
{
throw new ArgumentNullException(nameof(client));
}
if (imageToUpload == null)
{
throw new ArgumentNullException(nameof(imageToUpload));
}
await client.CheckSupportedFileTypeAsync(imageToUpload, cancellationToken);
var uploadMd5 = await imageToUpload.CalculateMd5SumAsync(cancellationToken);
return await client.UploadImageAsync(imageToUpload, new ImageUpload(uploadMd5), cancellationToken);
}
private static async Task CheckSupportedFileTypeAsync(this IPiwigoClient client, FileSystemInfo imageToUpload, CancellationToken cancellationToken)
{
if (!imageToUpload.Exists)
{
throw new PiwigoException($"Could not find file {imageToUpload.FullName}");
@ -22,11 +56,6 @@ public static class PiwigoClientExtensions
throw new PiwigoException(
$"The file {imageToUpload.Name} has extension {imageToUpload.Extension} which is not supported by the connected piwigo server. Please use one of the following formats {string.Join(",", supportedFileTypes)}");
}
var uploadMd5 = md5Sum ?? await imageToUpload.CalculateMd5SumAsync(cancellationToken);
await UploadChunksAsync(client, imageToUpload, uploadMd5, cancellationToken);
return await client.Image.AddAsync(new ImageUpload(uploadMd5), cancellationToken);
}
private static async Task UploadChunksAsync(IPiwigoClient client, FileInfo imageToUpload, string uploadMd5, CancellationToken cancellationToken)