StreamWriter.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using MediaBrowser.Model.Logging;
  2. using ServiceStack.Service;
  3. using ServiceStack.ServiceHost;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Threading.Tasks;
  8. namespace MediaBrowser.Server.Implementations.HttpServer
  9. {
  10. /// <summary>
  11. /// Class StreamWriter
  12. /// </summary>
  13. public class StreamWriter : IStreamWriter, IHasOptions
  14. {
  15. private ILogger Logger { get; set; }
  16. /// <summary>
  17. /// Gets or sets the source stream.
  18. /// </summary>
  19. /// <value>The source stream.</value>
  20. private Stream SourceStream { get; set; }
  21. /// <summary>
  22. /// The _options
  23. /// </summary>
  24. private readonly IDictionary<string, string> _options = new Dictionary<string, string>();
  25. /// <summary>
  26. /// Gets the options.
  27. /// </summary>
  28. /// <value>The options.</value>
  29. public IDictionary<string, string> Options
  30. {
  31. get { return _options; }
  32. }
  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, ILogger logger)
  40. {
  41. if (string.IsNullOrEmpty(contentType))
  42. {
  43. throw new ArgumentNullException("contentType");
  44. }
  45. SourceStream = source;
  46. Logger = logger;
  47. Options["Content-Type"] = contentType;
  48. }
  49. /// <summary>
  50. /// Initializes a new instance of the <see cref="StreamWriter"/> class.
  51. /// </summary>
  52. /// <param name="source">The source.</param>
  53. /// <param name="contentType">Type of the content.</param>
  54. /// <param name="logger">The logger.</param>
  55. public StreamWriter(byte[] source, string contentType, ILogger logger)
  56. : this(new MemoryStream(source), contentType, logger)
  57. {
  58. }
  59. /// <summary>
  60. /// Writes to.
  61. /// </summary>
  62. /// <param name="responseStream">The response stream.</param>
  63. public void WriteTo(Stream responseStream)
  64. {
  65. var task = WriteToAsync(responseStream);
  66. Task.WaitAll(task);
  67. }
  68. /// <summary>
  69. /// Writes to async.
  70. /// </summary>
  71. /// <param name="responseStream">The response stream.</param>
  72. /// <returns>Task.</returns>
  73. private async Task WriteToAsync(Stream responseStream)
  74. {
  75. try
  76. {
  77. using (var src = SourceStream)
  78. {
  79. await src.CopyToAsync(responseStream).ConfigureAwait(false);
  80. }
  81. }
  82. catch (Exception ex)
  83. {
  84. Logger.ErrorException("Error streaming media", ex);
  85. throw;
  86. }
  87. }
  88. }
  89. }