[backend/api] Return DriveFileResponse for avatar and banner
This commit is contained in:
parent
95c0a3e9ff
commit
cd412e3056
1 changed files with 44 additions and 5 deletions
|
@ -2,12 +2,14 @@ using System.Net;
|
||||||
using System.Net.Mime;
|
using System.Net.Mime;
|
||||||
using Iceshrimp.Backend.Controllers.Shared.Attributes;
|
using Iceshrimp.Backend.Controllers.Shared.Attributes;
|
||||||
using Iceshrimp.Backend.Core.Configuration;
|
using Iceshrimp.Backend.Core.Configuration;
|
||||||
|
using Iceshrimp.Backend.Core.Database;
|
||||||
using Iceshrimp.Backend.Core.Database.Tables;
|
using Iceshrimp.Backend.Core.Database.Tables;
|
||||||
using Iceshrimp.Backend.Core.Middleware;
|
using Iceshrimp.Backend.Core.Middleware;
|
||||||
using Iceshrimp.Backend.Core.Services;
|
using Iceshrimp.Backend.Core.Services;
|
||||||
using Iceshrimp.Shared.Schemas.Web;
|
using Iceshrimp.Shared.Schemas.Web;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.RateLimiting;
|
using Microsoft.AspNetCore.RateLimiting;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
namespace Iceshrimp.Backend.Controllers.Web;
|
namespace Iceshrimp.Backend.Controllers.Web;
|
||||||
|
@ -21,7 +23,8 @@ namespace Iceshrimp.Backend.Controllers.Web;
|
||||||
public class ProfileController(
|
public class ProfileController(
|
||||||
IOptions<Config.InstanceSection> instance,
|
IOptions<Config.InstanceSection> instance,
|
||||||
UserService userSvc,
|
UserService userSvc,
|
||||||
DriveService driveSvc
|
DriveService driveSvc,
|
||||||
|
DatabaseContext db
|
||||||
) : ControllerBase
|
) : ControllerBase
|
||||||
{
|
{
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
|
@ -89,10 +92,28 @@ public class ProfileController(
|
||||||
|
|
||||||
[HttpGet("avatar")]
|
[HttpGet("avatar")]
|
||||||
[ProducesResults(HttpStatusCode.OK)]
|
[ProducesResults(HttpStatusCode.OK)]
|
||||||
public string GetAvatarUrl()
|
[ProducesErrors(HttpStatusCode.NotFound)]
|
||||||
|
public async Task<DriveFileResponse> GetAvatar()
|
||||||
{
|
{
|
||||||
var user = HttpContext.GetUserOrFail();
|
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")]
|
[HttpPost("avatar")]
|
||||||
|
@ -144,10 +165,28 @@ public class ProfileController(
|
||||||
|
|
||||||
[HttpGet("banner")]
|
[HttpGet("banner")]
|
||||||
[ProducesResults(HttpStatusCode.OK)]
|
[ProducesResults(HttpStatusCode.OK)]
|
||||||
public string GetBannerUrl()
|
[ProducesErrors(HttpStatusCode.NotFound)]
|
||||||
|
public async Task<DriveFileResponse> GetBanner()
|
||||||
{
|
{
|
||||||
var user = HttpContext.GetUserOrFail();
|
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")]
|
[HttpPost("banner")]
|
||||||
|
|
Loading…
Add table
Reference in a new issue