From ff2ba1fb1bd77153fb14c6559bd3430e748d7f30 Mon Sep 17 00:00:00 2001 From: sam Date: Thu, 26 Sep 2024 22:25:47 +0200 Subject: [PATCH] fix(backend): correctly hash images --- Foxnouns.Backend/Extensions/AvatarObjectExtensions.cs | 9 +++++++-- Foxnouns.Backend/Jobs/MemberAvatarUpdateInvocable.cs | 2 +- Foxnouns.Backend/Jobs/UserAvatarUpdateInvocable.cs | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Foxnouns.Backend/Extensions/AvatarObjectExtensions.cs b/Foxnouns.Backend/Extensions/AvatarObjectExtensions.cs index 7c39aa4..c3c3c04 100644 --- a/Foxnouns.Backend/Extensions/AvatarObjectExtensions.cs +++ b/Foxnouns.Backend/Extensions/AvatarObjectExtensions.cs @@ -23,7 +23,7 @@ public static class AvatarObjectExtensions CancellationToken ct = default) => await objectStorageService.RemoveObjectAsync(UserAvatarUpdateInvocable.Path(id, hash), ct); - public static async Task ConvertBase64UriToAvatar(this string uri) + public static async Task ConvertBase64UriToImage(this string uri, int size, bool crop) { if (!uri.StartsWith("data:image/")) throw new ArgumentException("Not a data URI", nameof(uri)); @@ -40,7 +40,12 @@ public static class AvatarObjectExtensions var image = Image.Load(rawImage); var processor = new ResizeProcessor( - new ResizeOptions { Size = new Size(512), Mode = ResizeMode.Crop, Position = AnchorPositionMode.Center }, + new ResizeOptions + { + Size = new Size(size), + Mode = crop ? ResizeMode.Crop : ResizeMode.Max, + Position = AnchorPositionMode.Center + }, image.Size ); diff --git a/Foxnouns.Backend/Jobs/MemberAvatarUpdateInvocable.cs b/Foxnouns.Backend/Jobs/MemberAvatarUpdateInvocable.cs index 3beff48..7ed801a 100644 --- a/Foxnouns.Backend/Jobs/MemberAvatarUpdateInvocable.cs +++ b/Foxnouns.Backend/Jobs/MemberAvatarUpdateInvocable.cs @@ -31,7 +31,7 @@ public class MemberAvatarUpdateInvocable(DatabaseContext db, ObjectStorageServic try { - var image = await newAvatar.ConvertBase64UriToAvatar(); + var image = await newAvatar.ConvertBase64UriToImage(size: 512, crop: true); var hash = Convert.ToHexString(await SHA256.HashDataAsync(image)).ToLower(); image.Seek(0, SeekOrigin.Begin); var prevHash = member.Avatar; diff --git a/Foxnouns.Backend/Jobs/UserAvatarUpdateInvocable.cs b/Foxnouns.Backend/Jobs/UserAvatarUpdateInvocable.cs index d1abd42..44dd312 100644 --- a/Foxnouns.Backend/Jobs/UserAvatarUpdateInvocable.cs +++ b/Foxnouns.Backend/Jobs/UserAvatarUpdateInvocable.cs @@ -31,7 +31,7 @@ public class UserAvatarUpdateInvocable(DatabaseContext db, ObjectStorageService try { - var image = await newAvatar.ConvertBase64UriToAvatar(); + var image = await newAvatar.ConvertBase64UriToImage(size: 512, crop: true); var hash = Convert.ToHexString(await SHA256.HashDataAsync(image)).ToLower(); image.Seek(0, SeekOrigin.Begin); var prevHash = user.Avatar;