From 326b76b54ba6e5529395c82a285e7f437dc58799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Wed, 16 Nov 2022 00:22:55 +0100 Subject: [PATCH] WIP: upload file extension methods to make usages easier --- src/Piwigo.Client/FileInfoExtensions.cs | 16 +++++++ src/Piwigo.Client/PiwigoClientExtensions.cs | 49 +++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 src/Piwigo.Client/FileInfoExtensions.cs create mode 100644 src/Piwigo.Client/PiwigoClientExtensions.cs diff --git a/src/Piwigo.Client/FileInfoExtensions.cs b/src/Piwigo.Client/FileInfoExtensions.cs new file mode 100644 index 0000000..685204b --- /dev/null +++ b/src/Piwigo.Client/FileInfoExtensions.cs @@ -0,0 +1,16 @@ +using System.Security.Cryptography; + +namespace Piwigo.Client; + +public static class FileInfoExtensions +{ + public static async Task CalculateMd5SumAsync(this FileInfo file, CancellationToken cancellationToken) + { + using var md5 = MD5.Create(); + await using var stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.Read); + + var hash = await md5.ComputeHashAsync(stream, cancellationToken); + + return BitConverter.ToString(hash).Replace("-", string.Empty).ToLower(); + } +} \ No newline at end of file diff --git a/src/Piwigo.Client/PiwigoClientExtensions.cs b/src/Piwigo.Client/PiwigoClientExtensions.cs new file mode 100644 index 0000000..f290a05 --- /dev/null +++ b/src/Piwigo.Client/PiwigoClientExtensions.cs @@ -0,0 +1,49 @@ +using Piwigo.Client.Images; + +namespace Piwigo.Client; + +public static class PiwigoClientExtensions +{ + public static async Task UploadImageAsync(this IPiwigoClient client, FileInfo imageToUpload, string? md5Sum, CancellationToken cancellationToken = default) + { + if (client == null) + { + throw new ArgumentNullException(nameof(client)); + } + + if (!imageToUpload.Exists) + { + throw new PiwigoException($"Could not find file {imageToUpload.FullName}"); + } + + var supportedFileTypes = await client.Session.GetSupportedFileTypes(cancellationToken); + if (!supportedFileTypes.Contains(imageToUpload.Extension.ToLower())) + { + 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) + { + var chunkSizeInKiB = await client.Session.GetUploadChunkSizeAsync(cancellationToken); + + await using var file = imageToUpload.Open(FileMode.Open, FileAccess.Read, FileShare.Read); + + var buffer = new byte[chunkSizeInKiB * 1024]; + var totalChunks = (int)imageToUpload.Length / buffer.Length + 1; + var currentChunk = 0; + + while (currentChunk < totalChunks) + { + var readBytes = await file.ReadAsync(buffer, cancellationToken); + await client.Image.AddChunkAsync(uploadMd5, currentChunk, buffer[..readBytes], cancellationToken); + currentChunk++; + } + } +} \ No newline at end of file