| | | 1 | | using Chronicis.Api.Infrastructure; |
| | | 2 | | using Chronicis.Api.Models; |
| | | 3 | | using Chronicis.Api.Services; |
| | | 4 | | using Microsoft.AspNetCore.Authorization; |
| | | 5 | | using Microsoft.AspNetCore.Mvc; |
| | | 6 | | |
| | | 7 | | namespace Chronicis.Api.Controllers; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Proxy endpoint for serving inline article images. |
| | | 11 | | /// Resolves document IDs to fresh SAS download URLs via 302 redirect. |
| | | 12 | | /// This avoids storing expiring SAS URLs in article HTML content. |
| | | 13 | | /// </summary> |
| | | 14 | | [Route("api/images")] |
| | | 15 | | public class ImagesController : ControllerBase |
| | | 16 | | { |
| | | 17 | | private readonly IImageAccessService _imageAccessService; |
| | | 18 | | private readonly ICurrentUserService _currentUserService; |
| | | 19 | | |
| | 1 | 20 | | public ImagesController( |
| | 1 | 21 | | IImageAccessService imageAccessService, |
| | 1 | 22 | | ICurrentUserService currentUserService) |
| | | 23 | | { |
| | 1 | 24 | | _imageAccessService = imageAccessService; |
| | 1 | 25 | | _currentUserService = currentUserService; |
| | 1 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// GET /api/images/{documentId} - Redirect to a fresh SAS download URL for the image. |
| | | 30 | | /// Authenticated users who are members of (or own) the world can access images. |
| | | 31 | | /// </summary> |
| | | 32 | | [HttpGet("{documentId:guid}")] |
| | | 33 | | [Authorize] |
| | | 34 | | public async Task<IActionResult> GetImage(Guid documentId) |
| | | 35 | | { |
| | | 36 | | var user = await _currentUserService.GetRequiredUserAsync(); |
| | | 37 | | var result = await _imageAccessService.GetImageDownloadUrlAsync(documentId, user.Id); |
| | | 38 | | return result.Status switch |
| | | 39 | | { |
| | | 40 | | ServiceStatus.NotFound => NotFound(), |
| | | 41 | | ServiceStatus.Forbidden => Forbid(), |
| | | 42 | | ServiceStatus.ValidationError => BadRequest(new { error = result.ErrorMessage }), |
| | | 43 | | _ => Redirect(result.Value!) |
| | | 44 | | }; |
| | | 45 | | } |
| | | 46 | | } |