Remove ImageSharp

This commit is contained in:
Loporrit
2025-07-21 16:52:08 +00:00
parent f882fd8b9d
commit 73d9124d46
3 changed files with 52 additions and 11 deletions

View File

@@ -23,7 +23,6 @@
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="9.0.3" /> <PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="9.0.3" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="9.0.3" /> <PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="9.0.3" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.3" /> <PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.3" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.7" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="10.7.0.110445"> <PackageReference Include="SonarAnalyzer.CSharp" Version="10.7.0.110445">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View File

@@ -9,10 +9,9 @@ using MareSynchronos.API.Dto.User;
using MareSynchronos.Services; using MareSynchronos.Services;
using MareSynchronos.Services.Mediator; using MareSynchronos.Services.Mediator;
using MareSynchronos.Services.ServerConfiguration; using MareSynchronos.Services.ServerConfiguration;
using MareSynchronos.Utils;
using MareSynchronos.WebAPI; using MareSynchronos.WebAPI;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
namespace MareSynchronos.UI; namespace MareSynchronos.UI;
@@ -149,15 +148,9 @@ public class EditProfileUi : WindowMediatorSubscriberBase
{ {
var fileContent = File.ReadAllBytes(file); var fileContent = File.ReadAllBytes(file);
using MemoryStream ms = new(fileContent); using MemoryStream ms = new(fileContent);
var format = await Image.DetectFormatAsync(ms).ConfigureAwait(false); var format = PngHdr.TryExtractDimensions(ms);
if (!format.FileExtensions.Contains("png", StringComparer.OrdinalIgnoreCase))
{
_showFileDialogError = true;
return;
}
using var image = Image.Load<Rgba32>(fileContent);
if (image.Width > 256 || image.Height > 256 || (fileContent.Length > 250 * 1024)) if (format.Width > 256 || format.Height > 256 || (fileContent.Length > 250 * 1024))
{ {
_showFileDialogError = true; _showFileDialogError = true;
return; return;

View File

@@ -0,0 +1,49 @@
namespace MareSynchronos.Utils;
public class PngHdr
{
private static readonly byte[] _magicSignature = [137, 80, 78, 71, 13, 10, 26, 10];
private static readonly byte[] _IHDR = [(byte)'I', (byte)'H', (byte)'D', (byte)'R'];
public static readonly (int Width, int Height) InvalidSize = (0, 0);
public static (int Width, int Height) TryExtractDimensions(Stream stream)
{
Span<byte> buffer = stackalloc byte[8];
try
{
stream.ReadExactly(buffer[..8]);
// All PNG files start with the same 8 bytes
if (!buffer.SequenceEqual(_magicSignature))
return InvalidSize;
stream.ReadExactly(buffer[..8]);
uint ihdrLength = BitConverter.ToUInt32(buffer);
// The next four bytes will be the length of the IHDR section (it should be 13 bytes but we only need 8)
if (ihdrLength < 8)
return InvalidSize;
// followed by ASCII "IHDR"
if (!buffer[4..].SequenceEqual(_IHDR))
return InvalidSize;
stream.ReadExactly(buffer[..8]);
uint width = BitConverter.ToUInt32(buffer);
uint height = BitConverter.ToUInt32(buffer[4..]);
// Validate the width/height are non-negative and... that's all we care about!
if (width > int.MaxValue || height > int.MaxValue)
return InvalidSize;
return ((int)width, (int)height);
}
catch (EndOfStreamException)
{
return InvalidSize;
}
}
}