ProgressiveFileCopier.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Model.IO;
  8. using MediaBrowser.Model.Logging;
  9. using MediaBrowser.Model.Services;
  10. using MediaBrowser.Model.System;
  11. namespace MediaBrowser.Api.LiveTv
  12. {
  13. public class ProgressiveFileCopier : IAsyncStreamWriter, IHasHeaders
  14. {
  15. private readonly IFileSystem _fileSystem;
  16. private readonly ILogger _logger;
  17. private readonly string _path;
  18. private readonly Dictionary<string, string> _outputHeaders;
  19. const int StreamCopyToBufferSize = 81920;
  20. private long _bytesWritten = 0;
  21. public long StartPosition { get; set; }
  22. public bool AllowEndOfFile = true;
  23. private readonly IDirectStreamProvider _directStreamProvider;
  24. private readonly IEnvironmentInfo _environment;
  25. public ProgressiveFileCopier(IFileSystem fileSystem, string path, Dictionary<string, string> outputHeaders, ILogger logger, IEnvironmentInfo environment)
  26. {
  27. _fileSystem = fileSystem;
  28. _path = path;
  29. _outputHeaders = outputHeaders;
  30. _logger = logger;
  31. _environment = environment;
  32. }
  33. public ProgressiveFileCopier(IDirectStreamProvider directStreamProvider, Dictionary<string, string> outputHeaders, ILogger logger, IEnvironmentInfo environment)
  34. {
  35. _directStreamProvider = directStreamProvider;
  36. _outputHeaders = outputHeaders;
  37. _logger = logger;
  38. _environment = environment;
  39. }
  40. public IDictionary<string, string> Headers
  41. {
  42. get
  43. {
  44. return _outputHeaders;
  45. }
  46. }
  47. private Stream GetInputStream(bool allowAsyncFileRead)
  48. {
  49. var fileOpenOptions = FileOpenOptions.SequentialScan;
  50. if (allowAsyncFileRead)
  51. {
  52. fileOpenOptions |= FileOpenOptions.Asynchronous;
  53. }
  54. return _fileSystem.GetFileStream(_path, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.ReadWrite, fileOpenOptions);
  55. }
  56. public async Task WriteToAsync(Stream outputStream, CancellationToken cancellationToken)
  57. {
  58. if (_directStreamProvider != null)
  59. {
  60. await _directStreamProvider.CopyToAsync(outputStream, cancellationToken).ConfigureAwait(false);
  61. return;
  62. }
  63. var eofCount = 0;
  64. // use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039
  65. var allowAsyncFileRead = _environment.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.Windows;
  66. using (var inputStream = GetInputStream(allowAsyncFileRead))
  67. {
  68. if (StartPosition > 0)
  69. {
  70. inputStream.Position = StartPosition;
  71. }
  72. var emptyReadLimit = AllowEndOfFile ? 20 : 100;
  73. while (eofCount < emptyReadLimit)
  74. {
  75. int bytesRead;
  76. if (allowAsyncFileRead)
  77. {
  78. bytesRead = await CopyToInternalAsync(inputStream, outputStream, cancellationToken).ConfigureAwait(false);
  79. }
  80. else
  81. {
  82. bytesRead = await CopyToInternalAsyncWithSyncRead(inputStream, outputStream, cancellationToken).ConfigureAwait(false);
  83. }
  84. //var position = fs.Position;
  85. //_logger.Debug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);
  86. if (bytesRead == 0)
  87. {
  88. eofCount++;
  89. await Task.Delay(100, cancellationToken).ConfigureAwait(false);
  90. }
  91. else
  92. {
  93. eofCount = 0;
  94. }
  95. }
  96. }
  97. }
  98. private async Task<int> CopyToInternalAsyncWithSyncRead(Stream source, Stream destination, CancellationToken cancellationToken)
  99. {
  100. var array = new byte[StreamCopyToBufferSize];
  101. int bytesRead;
  102. int totalBytesRead = 0;
  103. while ((bytesRead = source.Read(array, 0, array.Length)) != 0)
  104. {
  105. var bytesToWrite = bytesRead;
  106. if (bytesToWrite > 0)
  107. {
  108. await destination.WriteAsync(array, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
  109. _bytesWritten += bytesRead;
  110. totalBytesRead += bytesRead;
  111. }
  112. }
  113. return totalBytesRead;
  114. }
  115. private async Task<int> CopyToInternalAsync(Stream source, Stream destination, CancellationToken cancellationToken)
  116. {
  117. var array = new byte[StreamCopyToBufferSize];
  118. int bytesRead;
  119. int totalBytesRead = 0;
  120. while ((bytesRead = await source.ReadAsync(array, 0, array.Length, cancellationToken).ConfigureAwait(false)) != 0)
  121. {
  122. var bytesToWrite = bytesRead;
  123. if (bytesToWrite > 0)
  124. {
  125. await destination.WriteAsync(array, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
  126. _bytesWritten += bytesRead;
  127. totalBytesRead += bytesRead;
  128. }
  129. }
  130. return totalBytesRead;
  131. }
  132. }
  133. }