2019-03-13 16:51:33 +00:00
|
|
|
using System;
|
2019-01-13 20:01:16 +00:00
|
|
|
using System.Collections.Generic;
|
2013-02-27 04:19:05 +00:00
|
|
|
using System.IO;
|
2016-07-24 16:46:17 +00:00
|
|
|
using System.Threading;
|
2013-02-27 04:19:05 +00:00
|
|
|
using System.Threading.Tasks;
|
2016-10-05 07:15:29 +00:00
|
|
|
using MediaBrowser.Controller.Library;
|
2017-08-09 19:56:38 +00:00
|
|
|
using MediaBrowser.Model.IO;
|
2016-10-25 19:02:04 +00:00
|
|
|
using MediaBrowser.Model.Services;
|
2018-12-14 23:48:06 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2013-02-27 04:19:05 +00:00
|
|
|
|
2017-08-09 19:56:38 +00:00
|
|
|
namespace MediaBrowser.Api.LiveTv
|
2013-02-27 04:19:05 +00:00
|
|
|
{
|
2016-10-25 19:02:04 +00:00
|
|
|
public class ProgressiveFileCopier : IAsyncStreamWriter, IHasHeaders
|
2014-06-26 17:04:11 +00:00
|
|
|
{
|
2016-03-22 03:27:46 +00:00
|
|
|
private readonly ILogger _logger;
|
2016-08-05 04:08:11 +00:00
|
|
|
private readonly string _path;
|
|
|
|
private readonly Dictionary<string, string> _outputHeaders;
|
2014-06-26 17:04:11 +00:00
|
|
|
|
2016-09-25 18:39:13 +00:00
|
|
|
public bool AllowEndOfFile = true;
|
|
|
|
|
2016-10-25 19:02:04 +00:00
|
|
|
private readonly IDirectStreamProvider _directStreamProvider;
|
2018-09-12 17:26:21 +00:00
|
|
|
private IStreamHelper _streamHelper;
|
2016-10-05 07:15:29 +00:00
|
|
|
|
2019-03-13 16:51:33 +00:00
|
|
|
public ProgressiveFileCopier(IStreamHelper streamHelper, string path, Dictionary<string, string> outputHeaders, ILogger logger)
|
2014-06-26 17:04:11 +00:00
|
|
|
{
|
2016-08-05 04:08:11 +00:00
|
|
|
_path = path;
|
|
|
|
_outputHeaders = outputHeaders;
|
2016-03-22 03:27:46 +00:00
|
|
|
_logger = logger;
|
2018-09-12 17:26:21 +00:00
|
|
|
_streamHelper = streamHelper;
|
2014-06-26 17:04:11 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 16:39:40 +00:00
|
|
|
public ProgressiveFileCopier(IDirectStreamProvider directStreamProvider, IStreamHelper streamHelper, Dictionary<string, string> outputHeaders, ILogger logger)
|
2016-10-05 07:15:29 +00:00
|
|
|
{
|
|
|
|
_directStreamProvider = directStreamProvider;
|
|
|
|
_outputHeaders = outputHeaders;
|
|
|
|
_logger = logger;
|
2018-09-12 17:26:21 +00:00
|
|
|
_streamHelper = streamHelper;
|
2016-10-05 07:15:29 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
public IDictionary<string, string> Headers => _outputHeaders;
|
2016-08-05 04:08:11 +00:00
|
|
|
|
2016-10-25 19:02:04 +00:00
|
|
|
public async Task WriteToAsync(Stream outputStream, CancellationToken cancellationToken)
|
2013-02-27 04:19:05 +00:00
|
|
|
{
|
2017-08-09 19:56:38 +00:00
|
|
|
if (_directStreamProvider != null)
|
2013-02-27 04:19:05 +00:00
|
|
|
{
|
2017-08-09 19:56:38 +00:00
|
|
|
await _directStreamProvider.CopyToAsync(outputStream, cancellationToken).ConfigureAwait(false);
|
|
|
|
return;
|
|
|
|
}
|
2016-10-05 07:15:29 +00:00
|
|
|
|
2019-03-13 16:51:33 +00:00
|
|
|
var fileOptions = FileOptions.SequentialScan;
|
2016-08-06 14:08:38 +00:00
|
|
|
|
2017-08-09 19:56:38 +00:00
|
|
|
// use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039
|
2019-03-13 16:51:33 +00:00
|
|
|
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
|
2017-08-09 19:56:38 +00:00
|
|
|
{
|
2019-03-13 16:51:33 +00:00
|
|
|
fileOptions |= FileOptions.Asynchronous;
|
|
|
|
}
|
2017-08-09 19:56:38 +00:00
|
|
|
|
2019-03-13 16:51:33 +00:00
|
|
|
using (var inputStream = new FileStream(_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096, fileOptions))
|
|
|
|
{
|
2017-10-14 06:52:56 +00:00
|
|
|
var emptyReadLimit = AllowEndOfFile ? 20 : 100;
|
2019-03-13 16:51:33 +00:00
|
|
|
var eofCount = 0;
|
2017-10-14 06:52:56 +00:00
|
|
|
while (eofCount < emptyReadLimit)
|
2017-08-09 19:56:38 +00:00
|
|
|
{
|
|
|
|
int bytesRead;
|
2018-09-12 17:26:21 +00:00
|
|
|
bytesRead = await _streamHelper.CopyToAsync(inputStream, outputStream, cancellationToken).ConfigureAwait(false);
|
2016-09-30 02:21:24 +00:00
|
|
|
|
2017-08-09 19:56:38 +00:00
|
|
|
if (bytesRead == 0)
|
2016-08-06 14:08:38 +00:00
|
|
|
{
|
2017-08-09 19:56:38 +00:00
|
|
|
eofCount++;
|
|
|
|
await Task.Delay(100, cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eofCount = 0;
|
2013-02-27 04:19:05 +00:00
|
|
|
}
|
2016-08-06 14:08:38 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-27 04:19:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|