Fix gravatar proxy

This commit is contained in:
Zoe Roux 2024-02-03 16:49:41 +01:00
parent b43b6d6f75
commit c26a95ed60
4 changed files with 14 additions and 18 deletions

View File

@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>default</LangVersion>
<LangVersion>preview</LangVersion>
<Company>Kyoo</Company>
<Authors>Kyoo</Authors>
<Copyright>Copyright (c) Kyoo</Copyright>

View File

@ -30,7 +30,8 @@ namespace Kyoo.Abstractions.Models.Exceptions
/// <summary>
/// Create a default <see cref="ItemNotFoundException"/> with no message.
/// </summary>
public ItemNotFoundException() { }
public ItemNotFoundException()
: base("Item not found") { }
/// <summary>
/// Create a new <see cref="ItemNotFoundException"/> with a message

View File

@ -219,17 +219,18 @@ namespace Kyoo.Core.Controllers
try
{
await using FileStream img = File.Open(
$"/metadata/user/${userId}.webp",
$"/metadata/user/{userId}.webp",
FileMode.Open
);
return img;
}
catch (FileNotFoundException) { }
catch (DirectoryNotFoundException) { }
User user = await users.Value.Get(userId);
if (user.Email == null) throw new ItemNotFoundException();
using MD5 md5 = MD5.Create();
string hash = Convert.ToHexString(md5.ComputeHash(Encoding.ASCII.GetBytes(user.Email)));
string hash = Convert.ToHexString(md5.ComputeHash(Encoding.ASCII.GetBytes(user.Email))).ToLower();
try
{
HttpClient client = clientFactory.CreateClient();
@ -250,7 +251,8 @@ namespace Kyoo.Core.Controllers
info.ColorType = SKColorType.Rgba8888;
using SKBitmap original = SKBitmap.Decode(codec, info);
using SKBitmap ret = original.Resize(new SKSizeI(250, 250), SKFilterQuality.High);
await _WriteTo(ret, $"/metadata/user/${userId}.webp", 75);
Directory.CreateDirectory("/metadata/user");
await _WriteTo(ret, $"/metadata/user/{userId}.webp", 75);
}
}
}

View File

@ -30,19 +30,12 @@ namespace Kyoo.Core
/// <summary>
/// A middleware to handle errors globally.
/// </summary>
public class ExceptionFilter : IExceptionFilter
/// <remarks>
/// Initializes a new instance of the <see cref="ExceptionFilter"/> class.
/// </remarks>
/// <param name="logger">The logger used to log errors.</param>
public class ExceptionFilter(ILogger<ExceptionFilter> logger) : IExceptionFilter
{
private readonly ILogger _logger;
/// <summary>
/// Initializes a new instance of the <see cref="ExceptionFilter"/> class.
/// </summary>
/// <param name="logger">The logger used to log errors.</param>
public ExceptionFilter(ILogger<ExceptionFilter> logger)
{
_logger = logger;
}
/// <inheritdoc/>
public void OnException(ExceptionContext context)
{
@ -65,7 +58,7 @@ namespace Kyoo.Core
context.Result = new UnauthorizedObjectResult(new RequestError(ex.Message));
break;
case Exception ex:
_logger.LogError(ex, "Unhandled error");
logger.LogError(ex, "Unhandled error");
context.Result = new ServerErrorObjectResult(
new RequestError("Internal Server Error")
);