StreamWriter.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.Extensions.Logging;
  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 => _options;
  33. public Action OnComplete { get; set; }
  34. public Action OnError { get; set; }
  35. /// <summary>
  36. /// Initializes a new instance of the <see cref="StreamWriter" /> class.
  37. /// </summary>
  38. /// <param name="source">The source.</param>
  39. /// <param name="contentType">Type of the content.</param>
  40. /// <param name="logger">The logger.</param>
  41. public StreamWriter(Stream source, string contentType, ILogger logger)
  42. {
  43. if (string.IsNullOrEmpty(contentType))
  44. {
  45. throw new ArgumentNullException(nameof(contentType));
  46. }
  47. SourceStream = source;
  48. Logger = logger;
  49. Headers["Content-Type"] = contentType;
  50. if (source.CanSeek)
  51. {
  52. Headers["Content-Length"] = source.Length.ToString(UsCulture);
  53. }
  54. }
  55. /// <summary>
  56. /// Initializes a new instance of the <see cref="StreamWriter"/> class.
  57. /// </summary>
  58. /// <param name="source">The source.</param>
  59. /// <param name="contentType">Type of the content.</param>
  60. /// <param name="logger">The logger.</param>
  61. public StreamWriter(byte[] source, string contentType, int contentLength, ILogger logger)
  62. {
  63. if (string.IsNullOrEmpty(contentType))
  64. {
  65. throw new ArgumentNullException(nameof(contentType));
  66. }
  67. SourceBytes = source;
  68. Logger = logger;
  69. Headers["Content-Type"] = contentType;
  70. Headers["Content-Length"] = contentLength.ToString(UsCulture);
  71. }
  72. public async Task WriteToAsync(Stream responseStream, CancellationToken cancellationToken)
  73. {
  74. try
  75. {
  76. var bytes = SourceBytes;
  77. if (bytes != null)
  78. {
  79. await responseStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
  80. }
  81. else
  82. {
  83. using (var src = SourceStream)
  84. {
  85. await src.CopyToAsync(responseStream).ConfigureAwait(false);
  86. }
  87. }
  88. }
  89. catch
  90. {
  91. if (OnError != null)
  92. {
  93. OnError();
  94. }
  95. throw;
  96. }
  97. finally
  98. {
  99. if (OnComplete != null)
  100. {
  101. OnComplete();
  102. }
  103. }
  104. }
  105. }
  106. }