From b23ef3eebbefe8372cd999892453edfef499f01c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Thu, 17 Nov 2022 23:11:55 +0100 Subject: [PATCH] upload file extension methods to make usages easier --- src/Piwigo.Client/PiwigoClientExtensions.cs | 41 ++++++++++++++++++--- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/src/Piwigo.Client/PiwigoClientExtensions.cs b/src/Piwigo.Client/PiwigoClientExtensions.cs index f290a05..d0265c0 100644 --- a/src/Piwigo.Client/PiwigoClientExtensions.cs +++ b/src/Piwigo.Client/PiwigoClientExtensions.cs @@ -4,13 +4,47 @@ namespace Piwigo.Client; public static class PiwigoClientExtensions { - public static async Task UploadImageAsync(this IPiwigoClient client, FileInfo imageToUpload, string? md5Sum, CancellationToken cancellationToken = default) + public static async Task 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 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)