ProgressiveFileCopier.cs 5.9 KB

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