ResourcesController.cs 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using FastReport.Web.Infrastructure;
  2. using FastReport.Web.Services;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.AspNetCore.Mvc;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace FastReport.Web.Controllers
  8. {
  9. static partial class Controllers
  10. {
  11. internal sealed class GetResourceParams
  12. {
  13. public string ResourceName { get; set; }
  14. public string ContentType { get; set; }
  15. }
  16. [HttpGet("/resources.getResource")]
  17. public static async Task<IResult> GetResource([FromQuery] GetResourceParams query,
  18. IResourceLoader resourceLoader,
  19. CancellationToken cancellationToken)
  20. {
  21. var resource = await resourceLoader.GetBytesAsync(query.ResourceName, cancellationToken);
  22. if (resource == null)
  23. return Results.NotFound();
  24. if (query.ContentType.IsNullOrWhiteSpace())
  25. query.ContentType = "application/octet-stream";
  26. return Results.File(resource, query.ContentType);
  27. }
  28. }
  29. }