CommunicationService.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.Text.Json;
  2. using Microsoft.AspNetCore.Builder;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Microsoft.Extensions.Logging;
  7. using SkiaSharp;
  8. namespace QuestPDF.Previewer;
  9. class CommunicationService
  10. {
  11. public static CommunicationService Instance { get; } = new ();
  12. public event Action<DocumentStructure>? OnDocumentUpdated;
  13. public Func<ICollection<PageSnapshotIndex>>? OnPageSnapshotsRequested { get; set; }
  14. public Action<ICollection<RenderedPageSnapshot>> OnPageSnapshotsProvided { get; set; }
  15. private WebApplication? Application { get; set; }
  16. private readonly JsonSerializerOptions JsonSerializerOptions = new()
  17. {
  18. PropertyNameCaseInsensitive = true
  19. };
  20. private CommunicationService()
  21. {
  22. }
  23. public Task Start(int port)
  24. {
  25. var builder = WebApplication.CreateBuilder();
  26. builder.Services.AddLogging(x => x.ClearProviders());
  27. builder.WebHost.UseKestrel(options => options.Limits.MaxRequestBodySize = null);
  28. Application = builder.Build();
  29. Application.MapGet("ping", HandlePing);
  30. Application.MapGet("version", HandleVersion);
  31. Application.MapPost("preview/update", HandlePreviewRefresh);
  32. Application.MapGet("preview/getRenderingRequests", HandleGetRequests);
  33. Application.MapPost("preview/provideRenderedImages", HandleProvidedSnapshotImages);
  34. return Application.RunAsync($"http://localhost:{port}/");
  35. }
  36. public async Task Stop()
  37. {
  38. await Application.StopAsync();
  39. await Application.DisposeAsync();
  40. }
  41. private async Task<IResult> HandlePing()
  42. {
  43. return OnDocumentUpdated == null
  44. ? Results.StatusCode(StatusCodes.Status503ServiceUnavailable)
  45. : Results.Ok();
  46. }
  47. private async Task<IResult> HandleVersion()
  48. {
  49. return Results.Json(GetType().Assembly.GetName().Version);
  50. }
  51. private async Task HandlePreviewRefresh(DocumentStructure documentStructure)
  52. {
  53. Task.Run(() => OnDocumentUpdated(documentStructure));
  54. }
  55. private async Task<ICollection<PageSnapshotIndex>> HandleGetRequests()
  56. {
  57. return OnPageSnapshotsRequested();
  58. }
  59. private async Task HandleProvidedSnapshotImages(HttpRequest request)
  60. {
  61. var renderedPageIndexes = JsonSerializer.Deserialize<ICollection<PageSnapshotIndex>>(request.Form["metadata"], JsonSerializerOptions);
  62. var renderedPages = new List<RenderedPageSnapshot>();
  63. foreach (var index in renderedPageIndexes)
  64. {
  65. using var memoryStream = new MemoryStream();
  66. await request.Form.Files.GetFile(index.ToString()).CopyToAsync(memoryStream);
  67. var image = SKImage.FromEncodedData(memoryStream.ToArray()).ToRasterImage(true);
  68. var renderedPage = new RenderedPageSnapshot
  69. {
  70. ZoomLevel = index.ZoomLevel, PageIndex = index.PageIndex, Image = image
  71. };
  72. renderedPages.Add(renderedPage);
  73. }
  74. Task.Run(() => OnPageSnapshotsProvided(renderedPages));
  75. }
  76. }