add caching headers

This commit is contained in:
john 2025-05-18 13:51:49 +02:00
parent 322dd01ee0
commit a98a61e040

View file

@ -31,13 +31,18 @@ public class MediaController(IMediaModule mediaModule) : ControllerBase
}
[HttpGet("{id}")]
[Authorize]
public async Task GetMedia(Guid id, CancellationToken cancellationToken)
{
var res = await mediaModule.PostQuery(new LoadFileQuery(id), cancellationToken);
// Set cache headers
HttpContext.Response.Headers.CacheControl = "public,max-age=31536000"; // Cache for 1 year
HttpContext.Response.Headers.Expires = DateTime.UtcNow.AddYears(1).ToString("R");
// If you want to support cache validation
HttpContext.Response.Headers.ETag = $"\"{id}\""; // Using the file ID as ETag
HttpContext.Response.ContentType = res.Type;
HttpContext.Response.ContentLength = res.Size;
await res.Data.CopyToAsync(HttpContext.Response.Body, cancellationToken);
}
}
}}