2
0

StreamWriter.cs 3.7 KB

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