using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UniVerse.Application.Interfaces;
namespace UniVerse.Api.Controllers;
[ApiController]
[Authorize]
[Route("api/v1/users/me/photo")]
public sealed class ProfilePhotoController : ControllerBase
{
private readonly IAuthService _auth;
private readonly IMicrosoftAuthClient _microsoft;
public ProfilePhotoController(IAuthService auth, IMicrosoftAuthClient microsoft)
{
_auth = auth;
_microsoft = microsoft;
}
/// Получить фото текущего пользователя из Microsoft Entra.
[HttpGet]
[Produces("image/jpeg", "image/png", "image/gif", "image/webp")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task Get(CancellationToken cancellationToken)
{
var userId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier) ?? User.FindFirstValue("sub") ?? "0");
await _auth.GetCurrentUserAsync(userId);
Response.Headers.CacheControl = "private, no-store";
var photo = await _microsoft.GetPhotoAsync(userId, cancellationToken);
if (photo is null) return NotFound();
return File(photo.Content, photo.ContentType);
}
}