PictureHandler

This commit is contained in:
2026-03-18 01:24:16 +01:00
parent 306248b11c
commit a75f2c2087
3 changed files with 52 additions and 0 deletions
+1
View File
@@ -11,6 +11,7 @@
<PackageReference Include="AWSSDK.S3" Version="4.0.19" />
<PackageReference Include="Keycloak.Net.Core" Version="1.0.38" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.5" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.12" />
</ItemGroup>
<ItemGroup>
+42
View File
@@ -0,0 +1,42 @@
using Ardalis.Result;
using Microsoft.Extensions.Logging;
using Musify.Application.Contracts.Infrastructure;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
namespace Musify.Infrastructure.Pictures
{
public class PictureHandler(ILogger<PictureHandler> logger)
: IPictureHandler
{
public async Task<Result<Stream>> ResizePictureAsync(Stream pictureStream, int width, int height, CancellationToken cancellationToken)
{
try
{
if (pictureStream.CanSeek && pictureStream.Position > 0)
pictureStream.Position = 0;
using var picture = await Image.LoadAsync(pictureStream, cancellationToken);
picture.Mutate(options => options.Resize(new ResizeOptions
{
Size = new Size(width, height),
Mode = ResizeMode.Max
} ));
var memoryStream = new MemoryStream();
await picture.SaveAsWebpAsync(memoryStream, cancellationToken);
memoryStream.Position = 0;
return Result<Stream>.Success(memoryStream);
}
catch (Exception exception)
{
logger.LogError(exception, "An error occurred while resizing the picture.");
return Result.Error("An error occurred while resizing the picture.");
}
}
}
}