2013-05-17 03:03:52 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
2013-03-29 23:00:38 +00:00
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
|
|
|
using Nancy;
|
|
|
|
using Nancy.Responses;
|
|
|
|
using NzbDrone.Common;
|
|
|
|
|
|
|
|
namespace NzbDrone.Api.Frontend
|
|
|
|
{
|
|
|
|
public interface IProcessStaticResource
|
|
|
|
{
|
|
|
|
Response ProcessStaticResourceRequest(NancyContext context, string workingFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
public class StaticResourceProvider : IProcessStaticResource
|
|
|
|
{
|
2013-05-10 23:53:50 +00:00
|
|
|
private readonly IDiskProvider _diskProvider;
|
2013-05-17 03:03:52 +00:00
|
|
|
private readonly IEnumerable<IMapHttpRequestsToDisk> _requestMappers;
|
2013-03-29 23:00:38 +00:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2013-05-17 03:03:52 +00:00
|
|
|
public StaticResourceProvider(IDiskProvider diskProvider, IEnumerable<IMapHttpRequestsToDisk> requestMappers, Logger logger)
|
2013-03-29 23:00:38 +00:00
|
|
|
{
|
|
|
|
_diskProvider = diskProvider;
|
2013-05-17 03:03:52 +00:00
|
|
|
_requestMappers = requestMappers;
|
2013-03-29 23:00:38 +00:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Response ProcessStaticResourceRequest(NancyContext context, string workingFolder)
|
|
|
|
{
|
|
|
|
var path = context.Request.Url.Path.ToLower();
|
|
|
|
|
2013-05-17 03:03:52 +00:00
|
|
|
if (path.StartsWith("/mediacover"))
|
|
|
|
{
|
|
|
|
var filePath = _requestMappers.Single(r => r.IHandle == RequestType.MediaCovers).Map(path);
|
|
|
|
|
|
|
|
if (_diskProvider.FileExists(filePath))
|
|
|
|
{
|
|
|
|
return new StreamResponse(() => File.OpenRead(filePath), "image/jpeg");
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.Warn("Couldn't find file [{0}] for [{1}]", filePath, path);
|
|
|
|
}
|
|
|
|
|
2013-03-29 23:00:38 +00:00
|
|
|
if (IsStaticResource(path))
|
|
|
|
{
|
2013-05-17 03:03:52 +00:00
|
|
|
var filePath = _requestMappers.Single(r => r.IHandle == RequestType.StaticResources).Map(path);
|
2013-03-29 23:00:38 +00:00
|
|
|
|
|
|
|
if (_diskProvider.FileExists(filePath))
|
|
|
|
{
|
|
|
|
return new GenericFileResponse(filePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.Warn("Couldn't find file [{0}] for [{1}]", filePath, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static readonly string[] Extensions = new[] { ".css", ".js", ".html", ".htm", ".jpg", ".jpeg", ".icon", ".gif", ".png", ".woff", ".ttf" };
|
|
|
|
|
|
|
|
|
|
|
|
private bool IsStaticResource(string path)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(path))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Extensions.Any(path.EndsWith);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|