From cd412e30560e1156bd686e408baf6af6e506effc Mon Sep 17 00:00:00 2001 From: pancakes Date: Wed, 18 Dec 2024 23:35:13 +1000 Subject: [PATCH] [backend/api] Return DriveFileResponse for avatar and banner --- .../Controllers/Web/ProfileController.cs | 49 +++++++++++++++++-- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/Iceshrimp.Backend/Controllers/Web/ProfileController.cs b/Iceshrimp.Backend/Controllers/Web/ProfileController.cs index 261b2f40..319e1a87 100644 --- a/Iceshrimp.Backend/Controllers/Web/ProfileController.cs +++ b/Iceshrimp.Backend/Controllers/Web/ProfileController.cs @@ -2,12 +2,14 @@ using System.Net; using System.Net.Mime; using Iceshrimp.Backend.Controllers.Shared.Attributes; using Iceshrimp.Backend.Core.Configuration; +using Iceshrimp.Backend.Core.Database; using Iceshrimp.Backend.Core.Database.Tables; using Iceshrimp.Backend.Core.Middleware; using Iceshrimp.Backend.Core.Services; using Iceshrimp.Shared.Schemas.Web; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.RateLimiting; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; namespace Iceshrimp.Backend.Controllers.Web; @@ -21,7 +23,8 @@ namespace Iceshrimp.Backend.Controllers.Web; public class ProfileController( IOptions instance, UserService userSvc, - DriveService driveSvc + DriveService driveSvc, + DatabaseContext db ) : ControllerBase { [HttpGet] @@ -89,10 +92,28 @@ public class ProfileController( [HttpGet("avatar")] [ProducesResults(HttpStatusCode.OK)] - public string GetAvatarUrl() + [ProducesErrors(HttpStatusCode.NotFound)] + public async Task GetAvatar() { var user = HttpContext.GetUserOrFail(); - return user.AvatarId != null ? user.GetAvatarUrl(instance.Value) : ""; + + var file = await db.Users + .IncludeCommonProperties() + .Where(p => p.Id == user.Id) + .Select(p => p.Avatar) + .FirstOrDefaultAsync() + ?? throw GracefulException.RecordNotFound(); + + return new DriveFileResponse + { + Id = file.Id, + Url = file.AccessUrl, + ThumbnailUrl = file.ThumbnailAccessUrl, + Filename = file.Name, + ContentType = file.Type, + Sensitive = file.IsSensitive, + Description = file.Comment + }; } [HttpPost("avatar")] @@ -144,10 +165,28 @@ public class ProfileController( [HttpGet("banner")] [ProducesResults(HttpStatusCode.OK)] - public string GetBannerUrl() + [ProducesErrors(HttpStatusCode.NotFound)] + public async Task GetBanner() { var user = HttpContext.GetUserOrFail(); - return user.GetBannerUrl(instance.Value) ?? ""; + + var file = await db.Users + .IncludeCommonProperties() + .Where(p => p.Id == user.Id) + .Select(p => p.Banner) + .FirstOrDefaultAsync() + ?? throw GracefulException.RecordNotFound(); + + return new DriveFileResponse + { + Id = file.Id, + Url = file.AccessUrl, + ThumbnailUrl = file.ThumbnailAccessUrl, + Filename = file.Name, + ContentType = file.Type, + Sensitive = file.IsSensitive, + Description = file.Comment + }; } [HttpPost("banner")]