CommunicationService.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 renderedPages = JsonSerializer.Deserialize<ICollection<RenderedPageSnapshot>>(request.Form["metadata"], JsonSerializerOptions);
  62. foreach (var renderedPage in renderedPages)
  63. {
  64. using var memoryStream = new MemoryStream();
  65. await request.Form.Files.GetFile(renderedPage.ToString()).CopyToAsync(memoryStream);
  66. renderedPage.Image = SKImage.FromEncodedData(memoryStream.ToArray());
  67. }
  68. Task.Run(() => OnPageSnapshotsProvided(renderedPages));
  69. }
  70. }