From d87856bf2c77c662f8231030acc41e8002cfa358 Mon Sep 17 00:00:00 2001 From: sam Date: Sat, 23 Nov 2024 20:41:41 +0100 Subject: [PATCH] refactor: change ConvertBase64UriToImage from extension method to static method --- .../{AvatarObjectExtensions.cs => ImageObjectExtensions.cs} | 4 ++-- Foxnouns.Backend/Jobs/CreateFlagInvocable.cs | 3 ++- Foxnouns.Backend/Jobs/MemberAvatarUpdateInvocable.cs | 6 +++++- Foxnouns.Backend/Jobs/UserAvatarUpdateInvocable.cs | 6 +++++- 4 files changed, 14 insertions(+), 5 deletions(-) rename Foxnouns.Backend/Extensions/{AvatarObjectExtensions.cs => ImageObjectExtensions.cs} (97%) diff --git a/Foxnouns.Backend/Extensions/AvatarObjectExtensions.cs b/Foxnouns.Backend/Extensions/ImageObjectExtensions.cs similarity index 97% rename from Foxnouns.Backend/Extensions/AvatarObjectExtensions.cs rename to Foxnouns.Backend/Extensions/ImageObjectExtensions.cs index efa2d60..2126610 100644 --- a/Foxnouns.Backend/Extensions/AvatarObjectExtensions.cs +++ b/Foxnouns.Backend/Extensions/ImageObjectExtensions.cs @@ -10,7 +10,7 @@ using SixLabors.ImageSharp.Processing.Processors.Transforms; namespace Foxnouns.Backend.Extensions; -public static class AvatarObjectExtensions +public static class ImageObjectExtensions { private static readonly string[] ValidContentTypes = ["image/png", "image/webp", "image/jpeg"]; @@ -39,7 +39,7 @@ public static class AvatarObjectExtensions ) => await objectStorageService.RemoveObjectAsync(CreateFlagInvocable.Path(hash), ct); public static async Task<(string Hash, Stream Image)> ConvertBase64UriToImage( - this string uri, + string uri, int size, bool crop ) diff --git a/Foxnouns.Backend/Jobs/CreateFlagInvocable.cs b/Foxnouns.Backend/Jobs/CreateFlagInvocable.cs index cfe1ca0..e7ce0e3 100644 --- a/Foxnouns.Backend/Jobs/CreateFlagInvocable.cs +++ b/Foxnouns.Backend/Jobs/CreateFlagInvocable.cs @@ -26,7 +26,8 @@ public class CreateFlagInvocable( try { - var (hash, image) = await Payload.ImageData.ConvertBase64UriToImage( + var (hash, image) = await ImageObjectExtensions.ConvertBase64UriToImage( + Payload.ImageData, size: 256, crop: false ); diff --git a/Foxnouns.Backend/Jobs/MemberAvatarUpdateInvocable.cs b/Foxnouns.Backend/Jobs/MemberAvatarUpdateInvocable.cs index 91640cb..d97e1a7 100644 --- a/Foxnouns.Backend/Jobs/MemberAvatarUpdateInvocable.cs +++ b/Foxnouns.Backend/Jobs/MemberAvatarUpdateInvocable.cs @@ -39,7 +39,11 @@ public class MemberAvatarUpdateInvocable( try { - var (hash, image) = await newAvatar.ConvertBase64UriToImage(size: 512, crop: true); + var (hash, image) = await ImageObjectExtensions.ConvertBase64UriToImage( + newAvatar, + size: 512, + crop: true + ); var prevHash = member.Avatar; await objectStorageService.PutObjectAsync(Path(id, hash), image, "image/webp"); diff --git a/Foxnouns.Backend/Jobs/UserAvatarUpdateInvocable.cs b/Foxnouns.Backend/Jobs/UserAvatarUpdateInvocable.cs index 31433f9..8147424 100644 --- a/Foxnouns.Backend/Jobs/UserAvatarUpdateInvocable.cs +++ b/Foxnouns.Backend/Jobs/UserAvatarUpdateInvocable.cs @@ -39,7 +39,11 @@ public class UserAvatarUpdateInvocable( try { - var (hash, image) = await newAvatar.ConvertBase64UriToImage(size: 512, crop: true); + var (hash, image) = await ImageObjectExtensions.ConvertBase64UriToImage( + newAvatar, + size: 512, + crop: true + ); image.Seek(0, SeekOrigin.Begin); var prevHash = user.Avatar;