| | | 1 | | using Chronicis.Api.Data; |
| | | 2 | | using Chronicis.Api.Infrastructure; |
| | | 3 | | using Chronicis.Api.Services; |
| | | 4 | | using Microsoft.AspNetCore.Authorization; |
| | | 5 | | using Microsoft.AspNetCore.Mvc; |
| | | 6 | | using Microsoft.EntityFrameworkCore; |
| | | 7 | | |
| | | 8 | | namespace Chronicis.Api.Controllers; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Proxy endpoint for serving inline article images. |
| | | 12 | | /// Resolves document IDs to fresh SAS download URLs via 302 redirect. |
| | | 13 | | /// This avoids storing expiring SAS URLs in article HTML content. |
| | | 14 | | /// </summary> |
| | | 15 | | [Route("api/images")] |
| | | 16 | | public class ImagesController : ControllerBase |
| | | 17 | | { |
| | | 18 | | private readonly ChronicisDbContext _db; |
| | | 19 | | private readonly IBlobStorageService _blobStorage; |
| | | 20 | | private readonly ICurrentUserService _currentUserService; |
| | | 21 | | private readonly ILogger<ImagesController> _logger; |
| | | 22 | | |
| | 0 | 23 | | public ImagesController( |
| | 0 | 24 | | ChronicisDbContext db, |
| | 0 | 25 | | IBlobStorageService blobStorage, |
| | 0 | 26 | | ICurrentUserService currentUserService, |
| | 0 | 27 | | ILogger<ImagesController> logger) |
| | | 28 | | { |
| | 0 | 29 | | _db = db; |
| | 0 | 30 | | _blobStorage = blobStorage; |
| | 0 | 31 | | _currentUserService = currentUserService; |
| | 0 | 32 | | _logger = logger; |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// GET /api/images/{documentId} - Redirect to a fresh SAS download URL for the image. |
| | | 37 | | /// Authenticated users who are members of (or own) the world can access images. |
| | | 38 | | /// </summary> |
| | | 39 | | [HttpGet("{documentId:guid}")] |
| | | 40 | | [Authorize] |
| | | 41 | | public async Task<IActionResult> GetImage(Guid documentId) |
| | | 42 | | { |
| | 0 | 43 | | var user = await _currentUserService.GetRequiredUserAsync(); |
| | | 44 | | |
| | 0 | 45 | | var document = await _db.WorldDocuments |
| | 0 | 46 | | .AsNoTracking() |
| | 0 | 47 | | .Include(d => d.World) |
| | 0 | 48 | | .FirstOrDefaultAsync(d => d.Id == documentId); |
| | | 49 | | |
| | 0 | 50 | | if (document == null) |
| | | 51 | | { |
| | 0 | 52 | | return NotFound(); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | // Check access: user must be the world owner or a member |
| | 0 | 56 | | var hasAccess = document.World.OwnerId == user.Id |
| | 0 | 57 | | || await _db.WorldMembers.AnyAsync(m => m.WorldId == document.WorldId && m.UserId == user.Id); |
| | | 58 | | |
| | 0 | 59 | | if (!hasAccess) |
| | | 60 | | { |
| | 0 | 61 | | return Forbid(); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | // Verify it's an image content type |
| | 0 | 65 | | if (!document.ContentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase)) |
| | | 66 | | { |
| | 0 | 67 | | _logger.LogWarning("Non-image document {DocumentId} requested via image proxy", documentId); |
| | 0 | 68 | | return BadRequest(new { error = "Document is not an image" }); |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | var downloadUrl = await _blobStorage.GenerateDownloadSasUrlAsync(document.BlobPath); |
| | | 72 | | |
| | 0 | 73 | | return Redirect(downloadUrl); |
| | 0 | 74 | | } |
| | | 75 | | } |