WebResources.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using GenHTTP.Api.Content.IO;
  5. using GenHTTP.Api.Protocol;
  6. namespace PRSServer
  7. {
  8. public class ByteArrayResource : IResource
  9. {
  10. public ByteArrayResource(byte[] content, string? name, FlexibleContentType? contentType, DateTime? modified)
  11. {
  12. Content = content;
  13. Checksum = (ulong)content.GetHashCode();
  14. Name = name;
  15. ContentType = contentType ?? FlexibleContentType.Get(GenHTTP.Api.Protocol.ContentType.TextPlain);
  16. Modified = modified;
  17. }
  18. public byte[] Content { get; }
  19. private ulong Checksum { get; }
  20. public string? Name { get; }
  21. public DateTime? Modified { get; }
  22. public FlexibleContentType? ContentType { get; }
  23. public ulong? Length => (ulong)Content.Length;
  24. public ValueTask<Stream> GetContentAsync()
  25. {
  26. return new ValueTask<Stream>(new MemoryStream(Content, false));
  27. }
  28. public ValueTask<ulong> CalculateChecksumAsync()
  29. {
  30. return new ValueTask<ulong>(Checksum);
  31. }
  32. public ValueTask WriteAsync(Stream target, uint bufferSize)
  33. {
  34. return target.WriteAsync(Content.AsMemory());
  35. }
  36. }
  37. }