ProgressiveFileCopier.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Model.IO;
  7. using MediaBrowser.Model.Services;
  8. using MediaBrowser.Model.System;
  9. using Microsoft.Extensions.Logging;
  10. namespace MediaBrowser.Api.LiveTv
  11. {
  12. public class ProgressiveFileCopier : IAsyncStreamWriter, IHasHeaders
  13. {
  14. private readonly IFileSystem _fileSystem;
  15. private readonly ILogger _logger;
  16. private readonly string _path;
  17. private readonly Dictionary<string, string> _outputHeaders;
  18. const int StreamCopyToBufferSize = 81920;
  19. public long StartPosition { get; set; }
  20. public bool AllowEndOfFile = true;
  21. private readonly IDirectStreamProvider _directStreamProvider;
  22. private readonly IEnvironmentInfo _environment;
  23. private IStreamHelper _streamHelper;
  24. public ProgressiveFileCopier(IFileSystem fileSystem, IStreamHelper streamHelper, string path, Dictionary<string, string> outputHeaders, ILogger logger, IEnvironmentInfo environment)
  25. {
  26. _fileSystem = fileSystem;
  27. _path = path;
  28. _outputHeaders = outputHeaders;
  29. _logger = logger;
  30. _environment = environment;
  31. _streamHelper = streamHelper;
  32. }
  33. public ProgressiveFileCopier(IDirectStreamProvider directStreamProvider, IStreamHelper streamHelper, Dictionary<string, string> outputHeaders, ILogger logger, IEnvironmentInfo environment)
  34. {
  35. _directStreamProvider = directStreamProvider;
  36. _outputHeaders = outputHeaders;
  37. _logger = logger;
  38. _environment = environment;
  39. _streamHelper = streamHelper;
  40. }
  41. public IDictionary<string, string> Headers => _outputHeaders;
  42. private Stream GetInputStream(bool allowAsyncFileRead)
  43. {
  44. var fileOpenOptions = FileOpenOptions.SequentialScan;
  45. if (allowAsyncFileRead)
  46. {
  47. fileOpenOptions |= FileOpenOptions.Asynchronous;
  48. }
  49. return _fileSystem.GetFileStream(_path, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.ReadWrite, fileOpenOptions);
  50. }
  51. public async Task WriteToAsync(Stream outputStream, CancellationToken cancellationToken)
  52. {
  53. if (_directStreamProvider != null)
  54. {
  55. await _directStreamProvider.CopyToAsync(outputStream, cancellationToken).ConfigureAwait(false);
  56. return;
  57. }
  58. var eofCount = 0;
  59. // use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039
  60. var allowAsyncFileRead = true;
  61. using (var inputStream = GetInputStream(allowAsyncFileRead))
  62. {
  63. if (StartPosition > 0)
  64. {
  65. inputStream.Position = StartPosition;
  66. }
  67. var emptyReadLimit = AllowEndOfFile ? 20 : 100;
  68. while (eofCount < emptyReadLimit)
  69. {
  70. int bytesRead;
  71. bytesRead = await _streamHelper.CopyToAsync(inputStream, outputStream, cancellationToken).ConfigureAwait(false);
  72. //var position = fs.Position;
  73. //_logger.LogDebug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);
  74. if (bytesRead == 0)
  75. {
  76. eofCount++;
  77. await Task.Delay(100, cancellationToken).ConfigureAwait(false);
  78. }
  79. else
  80. {
  81. eofCount = 0;
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }