From feb56ebdd6db658975a26091a48a0ff2ca80e917 Mon Sep 17 00:00:00 2001 From: Evann Regnault Date: Sun, 4 Aug 2024 13:41:22 +0200 Subject: [PATCH] [AvatarUrlFormat] Better Handling - Mimetype is now inferred by the Response header. - Extension is now derived from the Mimetype --- SSO-Auth/Api/SSOController.cs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/SSO-Auth/Api/SSOController.cs b/SSO-Auth/Api/SSOController.cs index f5c53b7..85d674e 100644 --- a/SSO-Auth/Api/SSOController.cs +++ b/SSO-Auth/Api/SSOController.cs @@ -1062,8 +1062,22 @@ public class SSOController : ControllerBase try { using var client = new HttpClient(); - var extension = avatarUrl.Split(".").Last(); - var stream = await client.GetStreamAsync(avatarUrl); + var avatarResponse = await client.GetAsync(avatarUrl); + + if (!avatarResponse.Content.Headers.TryGetValues("content-type", out var contentTypeList)) + { + throw new Exception("Cannot get Content-Type of image : " + avatarUrl); + } + + var contentType = contentTypeList.First(); + if (!contentType.StartsWith("image")) + { + throw new Exception("Content type of avatar URL is not an image, got : " + contentType); + } + + var extension = contentType.Split("/").Last(); + var stream = await avatarResponse.Content.ReadAsStreamAsync(); + if (user != null) { var userDataPath = @@ -1077,7 +1091,7 @@ public class SSOController : ControllerBase user.ProfileImage = new ImageInfo(Path.Combine(userDataPath, "profile" + extension)); - await _providerManager.SaveImage(stream, "image/" + extension, user.ProfileImage.Path) + await _providerManager.SaveImage(stream, contentType, user.ProfileImage.Path) .ConfigureAwait(false); } } @@ -1252,9 +1266,9 @@ public class TimedAuthorizeState /// Gets or sets a value indicating whether the user is allowed to manage live TV. /// public bool EnableLiveTvManagement { get; set; } - + /// - /// Gets or set the user avatar url. + /// Gets or sets the user avatar url. /// public string AvatarURL { get; set; } }