diff --git a/Foxnouns.Backend/Extensions/AvatarObjectExtensions.cs b/Foxnouns.Backend/Extensions/AvatarObjectExtensions.cs index c3c3c04..063f835 100644 --- a/Foxnouns.Backend/Extensions/AvatarObjectExtensions.cs +++ b/Foxnouns.Backend/Extensions/AvatarObjectExtensions.cs @@ -1,3 +1,4 @@ +using System.Security.Cryptography; using Foxnouns.Backend.Database; using Foxnouns.Backend.Jobs; using Foxnouns.Backend.Services; @@ -23,7 +24,7 @@ public static class AvatarObjectExtensions CancellationToken ct = default) => await objectStorageService.RemoveObjectAsync(UserAvatarUpdateInvocable.Path(id, hash), ct); - public static async Task ConvertBase64UriToImage(this string uri, int size, bool crop) + public static async Task<(string Hash, Stream Image)> ConvertBase64UriToImage(this string uri, int size, bool crop) { if (!uri.StartsWith("data:image/")) throw new ArgumentException("Not a data URI", nameof(uri)); @@ -53,6 +54,11 @@ public static class AvatarObjectExtensions var stream = new MemoryStream(64 * 1024); await image.SaveAsync(stream, new WebpEncoder { Quality = 95, NearLossless = false }); - return stream; + + stream.Seek(0, SeekOrigin.Begin); + var hash = Convert.ToHexString(await SHA256.HashDataAsync(stream)).ToLower(); + stream.Seek(0, SeekOrigin.Begin); + + return (hash, stream); } } \ No newline at end of file diff --git a/Foxnouns.Backend/Jobs/CreateFlagInvocable.cs b/Foxnouns.Backend/Jobs/CreateFlagInvocable.cs index 6f69794..1acf054 100644 --- a/Foxnouns.Backend/Jobs/CreateFlagInvocable.cs +++ b/Foxnouns.Backend/Jobs/CreateFlagInvocable.cs @@ -20,11 +20,7 @@ public class CreateFlagInvocable(DatabaseContext db, ObjectStorageService object try { - var image = await Payload.ImageData.ConvertBase64UriToImage(size: 256, crop: false); - image.Seek(0, SeekOrigin.Begin); - var hash = Convert.ToHexString(await SHA256.HashDataAsync(image)).ToLower(); - image.Seek(0, SeekOrigin.Begin); - + var (hash, image) = await Payload.ImageData.ConvertBase64UriToImage(size: 256, crop: false); await objectStorageService.PutObjectAsync(Path(hash), image, "image/webp"); var flag = new PrideFlag diff --git a/Foxnouns.Backend/Jobs/MemberAvatarUpdateInvocable.cs b/Foxnouns.Backend/Jobs/MemberAvatarUpdateInvocable.cs index 7ed801a..baab54a 100644 --- a/Foxnouns.Backend/Jobs/MemberAvatarUpdateInvocable.cs +++ b/Foxnouns.Backend/Jobs/MemberAvatarUpdateInvocable.cs @@ -31,9 +31,7 @@ public class MemberAvatarUpdateInvocable(DatabaseContext db, ObjectStorageServic try { - var image = await newAvatar.ConvertBase64UriToImage(size: 512, crop: true); - var hash = Convert.ToHexString(await SHA256.HashDataAsync(image)).ToLower(); - image.Seek(0, SeekOrigin.Begin); + var (hash, image) = await newAvatar.ConvertBase64UriToImage(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 44dd312..aab0240 100644 --- a/Foxnouns.Backend/Jobs/UserAvatarUpdateInvocable.cs +++ b/Foxnouns.Backend/Jobs/UserAvatarUpdateInvocable.cs @@ -31,8 +31,7 @@ public class UserAvatarUpdateInvocable(DatabaseContext db, ObjectStorageService try { - var image = await newAvatar.ConvertBase64UriToImage(size: 512, crop: true); - var hash = Convert.ToHexString(await SHA256.HashDataAsync(image)).ToLower(); + var (hash, image) = await newAvatar.ConvertBase64UriToImage(size: 512, crop: true); image.Seek(0, SeekOrigin.Begin); var prevHash = user.Avatar;