From e65bea618ac79cee297c0528ec318b721e9c34f9 Mon Sep 17 00:00:00 2001 From: Jonathan Potts Date: Thu, 28 Mar 2024 17:32:50 -0500 Subject: [PATCH] Remove unnecessary code --- src/Application/Services/RecipeService.cs | 48 +++++++++++------------ 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/src/Application/Services/RecipeService.cs b/src/Application/Services/RecipeService.cs index 32cc7f5..3d3ff00 100644 --- a/src/Application/Services/RecipeService.cs +++ b/src/Application/Services/RecipeService.cs @@ -142,38 +142,34 @@ public async Task UpdateCoverImageAsync( $"User is unauthorized to perform {nameof(Operations.Update)} operation on resource."); } - await Task.Run(async () => - { - SKBitmap bitmap = SKBitmap.Decode(imageData) - ?? throw new ArgumentException("The image could not be decoded.", nameof(imageData)); + SKBitmap bitmap = SKBitmap.Decode(imageData) + ?? throw new ArgumentException("The image could not be decoded.", nameof(imageData)); - try + try + { + if (bitmap.Width > MaxImageDimension || bitmap.Height > MaxImageDimension) { - if (bitmap.Width > MaxImageDimension || bitmap.Height > MaxImageDimension) - { - var scaleFactor = (double)MaxImageDimension / Math.Max(bitmap.Width, bitmap.Height); - var width = (int)Math.Floor(bitmap.Width * scaleFactor); - var height = (int)Math.Floor(bitmap.Height * scaleFactor); + var scaleFactor = (double)MaxImageDimension / Math.Max(bitmap.Width, bitmap.Height); + var width = (int)Math.Floor(bitmap.Width * scaleFactor); + var height = (int)Math.Floor(bitmap.Height * scaleFactor); - var resizedBitmap = bitmap.Resize(new SKImageInfo(width, height), ImageFilterQuality); + var resizedBitmap = bitmap.Resize(new SKImageInfo(width, height), ImageFilterQuality); - bitmap.Dispose(); - bitmap = resizedBitmap; - } - - using var data = bitmap.Encode(SKEncodedImageFormat.Webp, ImageQuality); - - await File.WriteAllBytesAsync( - Path.Combine(s_imagesDirectory, $"{id}.webp"), - data.AsSpan().ToArray(), - cancellationToken); - } - finally - { bitmap.Dispose(); + bitmap = resizedBitmap; } - }, - cancellationToken); + + using var data = bitmap.Encode(SKEncodedImageFormat.Webp, ImageQuality); + + await File.WriteAllBytesAsync( + Path.Combine(s_imagesDirectory, $"{id}.webp"), + data.AsSpan().ToArray(), + cancellationToken); + } + finally + { + bitmap.Dispose(); + } recipe.CoverImage ??= new(); recipe.CoverImage.Url = $"{id}.webp";