2019-01-13 19:54:44 +00:00
|
|
|
using System;
|
2017-03-12 19:27:26 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
2019-01-13 19:20:41 +00:00
|
|
|
using System.Linq;
|
2017-03-12 19:27:26 +00:00
|
|
|
using System.Net;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2019-02-21 09:07:21 +00:00
|
|
|
using Emby.Server.Implementations.IO;
|
2017-03-12 19:27:26 +00:00
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
using MediaBrowser.Model.Services;
|
2019-01-13 19:20:41 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2019-03-05 18:20:28 +00:00
|
|
|
using Microsoft.Net.Http.Headers;
|
2017-03-12 19:27:26 +00:00
|
|
|
|
|
|
|
namespace Emby.Server.Implementations.HttpServer
|
|
|
|
{
|
|
|
|
public class FileWriter : IHttpResult
|
|
|
|
{
|
2019-03-05 18:32:22 +00:00
|
|
|
private readonly IStreamHelper _streamHelper;
|
2017-03-12 19:27:26 +00:00
|
|
|
private ILogger Logger { get; set; }
|
2019-03-06 18:29:25 +00:00
|
|
|
private readonly IFileSystem _fileSystem;
|
2017-03-12 19:27:26 +00:00
|
|
|
|
|
|
|
private string RangeHeader { get; set; }
|
|
|
|
private bool IsHeadRequest { get; set; }
|
|
|
|
|
|
|
|
private long RangeStart { get; set; }
|
|
|
|
private long RangeEnd { get; set; }
|
|
|
|
private long RangeLength { get; set; }
|
2019-01-22 20:18:48 +00:00
|
|
|
public long TotalContentLength { get; set; }
|
2017-03-12 19:27:26 +00:00
|
|
|
|
|
|
|
public Action OnComplete { get; set; }
|
|
|
|
public Action OnError { get; set; }
|
|
|
|
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
|
|
|
|
public List<Cookie> Cookies { get; private set; }
|
|
|
|
|
2017-03-13 04:08:23 +00:00
|
|
|
public FileShareMode FileShare { get; set; }
|
|
|
|
|
2017-03-12 19:27:26 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The _options
|
|
|
|
/// </summary>
|
|
|
|
private readonly IDictionary<string, string> _options = new Dictionary<string, string>();
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the options.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The options.</value>
|
2019-01-06 20:50:43 +00:00
|
|
|
public IDictionary<string, string> Headers => _options;
|
2017-03-12 19:27:26 +00:00
|
|
|
|
|
|
|
public string Path { get; set; }
|
|
|
|
|
2019-03-05 18:32:22 +00:00
|
|
|
public FileWriter(string path, string contentType, string rangeHeader, ILogger logger, IFileSystem fileSystem, IStreamHelper streamHelper)
|
2017-03-12 19:27:26 +00:00
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(contentType))
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(contentType));
|
2017-03-12 19:27:26 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 18:32:22 +00:00
|
|
|
_streamHelper = streamHelper;
|
2019-03-06 18:29:25 +00:00
|
|
|
_fileSystem = fileSystem;
|
2019-03-05 18:32:22 +00:00
|
|
|
|
2017-03-12 19:27:26 +00:00
|
|
|
Path = path;
|
|
|
|
Logger = logger;
|
|
|
|
RangeHeader = rangeHeader;
|
|
|
|
|
2019-03-05 18:20:28 +00:00
|
|
|
Headers[HeaderNames.ContentType] = contentType;
|
2017-03-12 19:27:26 +00:00
|
|
|
|
|
|
|
TotalContentLength = fileSystem.GetFileInfo(path).Length;
|
2019-03-05 18:20:28 +00:00
|
|
|
Headers[HeaderNames.AcceptRanges] = "bytes";
|
2017-03-12 19:27:26 +00:00
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(rangeHeader))
|
|
|
|
{
|
|
|
|
StatusCode = HttpStatusCode.OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
StatusCode = HttpStatusCode.PartialContent;
|
|
|
|
SetRangeValues();
|
|
|
|
}
|
|
|
|
|
2017-03-13 04:08:23 +00:00
|
|
|
FileShare = FileShareMode.Read;
|
2017-03-12 19:27:26 +00:00
|
|
|
Cookies = new List<Cookie>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sets the range values.
|
|
|
|
/// </summary>
|
|
|
|
private void SetRangeValues()
|
|
|
|
{
|
|
|
|
var requestedRange = RequestedRanges[0];
|
|
|
|
|
|
|
|
// If the requested range is "0-", we can optimize by just doing a stream copy
|
|
|
|
if (!requestedRange.Value.HasValue)
|
|
|
|
{
|
|
|
|
RangeEnd = TotalContentLength - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RangeEnd = requestedRange.Value.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
RangeStart = requestedRange.Key;
|
|
|
|
RangeLength = 1 + RangeEnd - RangeStart;
|
|
|
|
|
2019-03-05 18:20:28 +00:00
|
|
|
var rangeString = $"bytes {RangeStart}-{RangeEnd}/{TotalContentLength}";
|
|
|
|
Headers[HeaderNames.ContentRange] = rangeString;
|
2017-05-05 17:55:38 +00:00
|
|
|
|
2019-03-05 19:48:04 +00:00
|
|
|
Logger.LogInformation("Setting range response values for {0}. RangeRequest: {1} Content-Range: {2}", Path, RangeHeader, rangeString);
|
2017-03-12 19:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The _requested ranges
|
|
|
|
/// </summary>
|
|
|
|
private List<KeyValuePair<long, long?>> _requestedRanges;
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the requested ranges.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The requested ranges.</value>
|
|
|
|
protected List<KeyValuePair<long, long?>> RequestedRanges
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (_requestedRanges == null)
|
|
|
|
{
|
|
|
|
_requestedRanges = new List<KeyValuePair<long, long?>>();
|
|
|
|
|
|
|
|
// Example: bytes=0-,32-63
|
|
|
|
var ranges = RangeHeader.Split('=')[1].Split(',');
|
|
|
|
|
|
|
|
foreach (var range in ranges)
|
|
|
|
{
|
|
|
|
var vals = range.Split('-');
|
|
|
|
|
|
|
|
long start = 0;
|
|
|
|
long? end = null;
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(vals[0]))
|
|
|
|
{
|
|
|
|
start = long.Parse(vals[0], UsCulture);
|
|
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(vals[1]))
|
|
|
|
{
|
|
|
|
end = long.Parse(vals[1], UsCulture);
|
|
|
|
}
|
|
|
|
|
|
|
|
_requestedRanges.Add(new KeyValuePair<long, long?>(start, end));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _requestedRanges;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 18:32:22 +00:00
|
|
|
private readonly string[] SkipLogExtensions = {
|
2018-09-12 17:26:21 +00:00
|
|
|
".js",
|
|
|
|
".html",
|
|
|
|
".css"
|
|
|
|
};
|
|
|
|
|
2017-03-12 19:27:26 +00:00
|
|
|
public async Task WriteToAsync(IResponse response, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Headers only
|
|
|
|
if (IsHeadRequest)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var path = Path;
|
2019-03-05 18:32:22 +00:00
|
|
|
var offset = RangeStart;
|
|
|
|
var count = RangeLength;
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2019-03-05 18:32:22 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(RangeHeader) || RangeStart <= 0 && RangeEnd >= TotalContentLength - 1)
|
2017-03-12 19:27:26 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var extension = System.IO.Path.GetExtension(path);
|
|
|
|
|
|
|
|
if (extension == null || !SkipLogExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
|
|
|
|
{
|
2018-12-13 13:18:25 +00:00
|
|
|
Logger.LogDebug("Transmit file {0}", path);
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
2017-09-04 19:28:22 +00:00
|
|
|
|
2019-03-05 18:32:22 +00:00
|
|
|
offset = 0;
|
|
|
|
count = 0;
|
2017-03-12 19:27:26 +00:00
|
|
|
}
|
2019-03-05 18:32:22 +00:00
|
|
|
|
2019-03-06 18:29:25 +00:00
|
|
|
await response.TransmitFile(path, offset, count, FileShare, _fileSystem, _streamHelper, cancellationToken).ConfigureAwait(false);
|
2017-03-12 19:27:26 +00:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2019-03-05 18:32:22 +00:00
|
|
|
OnComplete?.Invoke();
|
2017-03-12 19:27:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string ContentType { get; set; }
|
|
|
|
|
|
|
|
public IRequest RequestContext { get; set; }
|
|
|
|
|
|
|
|
public object Response { get; set; }
|
|
|
|
|
|
|
|
public int Status { get; set; }
|
|
|
|
|
|
|
|
public HttpStatusCode StatusCode
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
get => (HttpStatusCode)Status;
|
|
|
|
set => Status = (int)value;
|
2017-03-12 19:27:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|