StreamWriter.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Model.Services;
  8. using Microsoft.Net.Http.Headers;
  9. namespace Emby.Server.Implementations.HttpServer
  10. {
  11. /// <summary>
  12. /// Class StreamWriter
  13. /// </summary>
  14. public class StreamWriter : IAsyncStreamWriter, IHasHeaders
  15. {
  16. /// <summary>
  17. /// Gets or sets the source stream.
  18. /// </summary>
  19. /// <value>The source stream.</value>
  20. private Stream SourceStream { get; set; }
  21. private byte[] SourceBytes { get; set; }
  22. /// <summary>
  23. /// The _options
  24. /// </summary>
  25. private readonly IDictionary<string, string> _options = new Dictionary<string, string>();
  26. /// <summary>
  27. /// Gets the options.
  28. /// </summary>
  29. /// <value>The options.</value>
  30. public IDictionary<string, string> Headers => _options;
  31. public Action OnComplete { get; set; }
  32. public Action OnError { get; set; }
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="StreamWriter" /> class.
  35. /// </summary>
  36. /// <param name="source">The source.</param>
  37. /// <param name="contentType">Type of the content.</param>
  38. /// <param name="logger">The logger.</param>
  39. public StreamWriter(Stream source, string contentType)
  40. {
  41. if (string.IsNullOrEmpty(contentType))
  42. {
  43. throw new ArgumentNullException(nameof(contentType));
  44. }
  45. SourceStream = source;
  46. Headers["Content-Type"] = contentType;
  47. if (source.CanSeek)
  48. {
  49. Headers[HeaderNames.ContentLength] = source.Length.ToString(CultureInfo.InvariantCulture);
  50. }
  51. Headers[HeaderNames.ContentType] = contentType;
  52. }
  53. /// <summary>
  54. /// Initializes a new instance of the <see cref="StreamWriter"/> class.
  55. /// </summary>
  56. /// <param name="source">The source.</param>
  57. /// <param name="contentType">Type of the content.</param>
  58. public StreamWriter(byte[] source, string contentType, int contentLength)
  59. {
  60. if (string.IsNullOrEmpty(contentType))
  61. {
  62. throw new ArgumentNullException(nameof(contentType));
  63. }
  64. SourceBytes = source;
  65. Headers[HeaderNames.ContentLength] = contentLength.ToString(CultureInfo.InvariantCulture);
  66. Headers[HeaderNames.ContentType] = contentType;
  67. }
  68. public async Task WriteToAsync(Stream responseStream, CancellationToken cancellationToken)
  69. {
  70. try
  71. {
  72. var bytes = SourceBytes;
  73. if (bytes != null)
  74. {
  75. await responseStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
  76. }
  77. else
  78. {
  79. using (var src = SourceStream)
  80. {
  81. await src.CopyToAsync(responseStream).ConfigureAwait(false);
  82. }
  83. }
  84. }
  85. catch
  86. {
  87. if (OnError != null)
  88. {
  89. OnError();
  90. }
  91. throw;
  92. }
  93. finally
  94. {
  95. if (OnComplete != null)
  96. {
  97. OnComplete();
  98. }
  99. }
  100. }
  101. }
  102. }