using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.IO;
using MediaBrowser.Common.Kernel;
using MediaBrowser.Model.Logging;
using ServiceStack.Common;
using ServiceStack.Common.Web;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface;
using ServiceStack.WebHost.Endpoints;
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
namespace MediaBrowser.Common.Net
{
///
/// Class BaseRestService
///
public class BaseRestService : Service, IRestfulService
{
///
/// Gets or sets the kernel.
///
/// The kernel.
public IKernel Kernel { get; set; }
///
/// Gets or sets the logger.
///
/// The logger.
public ILogger Logger { get; set; }
///
/// Gets a value indicating whether this instance is range request.
///
/// true if this instance is range request; otherwise, false.
protected bool IsRangeRequest
{
get
{
return Request.Headers.AllKeys.Contains("Range");
}
}
///
/// Adds the routes.
///
/// The app host.
public virtual void Configure(IAppHost appHost)
{
}
///
/// To the optimized result.
///
///
/// The result.
/// System.Object.
/// result
protected object ToOptimizedResult(T result)
where T : class
{
if (result == null)
{
throw new ArgumentNullException("result");
}
Response.AddHeader("Vary", "Accept-Encoding");
return RequestContext.ToOptimizedResult(result);
}
///
/// To the optimized result using cache.
///
///
/// The cache key.
/// The last date modified.
/// Duration of the cache.
/// The factory fn.
/// System.Object.
/// cacheKey
protected object ToOptimizedResultUsingCache(Guid cacheKey, DateTime lastDateModified, TimeSpan? cacheDuration, Func factoryFn)
where T : class
{
if (cacheKey == Guid.Empty)
{
throw new ArgumentNullException("cacheKey");
}
if (factoryFn == null)
{
throw new ArgumentNullException("factoryFn");
}
var key = cacheKey.ToString("N");
var result = PreProcessCachedResult(cacheKey, key, lastDateModified, cacheDuration, string.Empty);
if (result != null)
{
return result;
}
return ToOptimizedResult(factoryFn());
}
///
/// To the cached result.
///
///
/// The cache key.
/// The last date modified.
/// Duration of the cache.
/// The factory fn.
/// Type of the content.
/// System.Object.
/// cacheKey
protected object ToCachedResult(Guid cacheKey, DateTime lastDateModified, TimeSpan? cacheDuration, Func factoryFn, string contentType)
where T : class
{
if (cacheKey == Guid.Empty)
{
throw new ArgumentNullException("cacheKey");
}
if (factoryFn == null)
{
throw new ArgumentNullException("factoryFn");
}
var key = cacheKey.ToString("N");
var result = PreProcessCachedResult(cacheKey, key, lastDateModified, cacheDuration, contentType);
if (result != null)
{
return result;
}
return factoryFn();
}
///
/// To the static file result.
///
/// The path.
/// System.Object.
/// path
protected object ToStaticFileResult(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException("path");
}
var dateModified = File.GetLastWriteTimeUtc(path);
var cacheKey = path + dateModified.Ticks;
return ToStaticResult(cacheKey.GetMD5(), dateModified, null, MimeTypes.GetMimeType(path), () => Task.FromResult(GetFileStream(path)));
}
///
/// Gets the file stream.
///
/// The path.
/// Stream.
private Stream GetFileStream(string path)
{
return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous);
}
///
/// To the static result.
///
/// The cache key.
/// The last date modified.
/// Duration of the cache.
/// Type of the content.
/// The factory fn.
/// System.Object.
/// cacheKey
protected object ToStaticResult(Guid cacheKey, DateTime? lastDateModified, TimeSpan? cacheDuration, string contentType, Func> factoryFn)
{
if (cacheKey == Guid.Empty)
{
throw new ArgumentNullException("cacheKey");
}
if (factoryFn == null)
{
throw new ArgumentNullException("factoryFn");
}
var key = cacheKey.ToString("N");
var result = PreProcessCachedResult(cacheKey, key, lastDateModified, cacheDuration, contentType);
if (result != null)
{
return result;
}
var compress = ShouldCompressResponse(contentType);
if (compress)
{
Response.AddHeader("Vary", "Accept-Encoding");
}
return ToStaticResult(contentType, factoryFn, compress).Result;
}
///
/// Shoulds the compress response.
///
/// Type of the content.
/// true if XXXX, false otherwise
private bool ShouldCompressResponse(string contentType)
{
// It will take some work to support compression with byte range requests
if (IsRangeRequest)
{
return false;
}
// Don't compress media
if (contentType.StartsWith("audio/", StringComparison.OrdinalIgnoreCase) || contentType.StartsWith("video/", StringComparison.OrdinalIgnoreCase))
{
return false;
}
// Don't compress images
if (contentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
{
return false;
}
if (contentType.StartsWith("font/", StringComparison.OrdinalIgnoreCase))
{
return false;
}
if (contentType.StartsWith("application/", StringComparison.OrdinalIgnoreCase))
{
return false;
}
return true;
}
///
/// To the static result.
///
/// Type of the content.
/// The factory fn.
/// if set to true [compress].
/// System.Object.
private async Task