Sonarr/NzbDrone.Api/Frontend/StaticResourceProvider.cs

66 lines
1.9 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.IO;
using System.Linq;
using NLog;
using Nancy;
using Nancy.Responses;
using NzbDrone.Common;
using NzbDrone.Api.Extensions;
2013-07-01 04:50:51 +00:00
using NzbDrone.Common.EnvironmentInfo;
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;
private readonly IEnumerable<IMapHttpRequestsToDisk> _requestMappers;
private readonly Logger _logger;
2013-07-01 04:50:51 +00:00
public StaticResourceProvider(IDiskProvider diskProvider,
IEnumerable<IMapHttpRequestsToDisk> requestMappers,
Logger logger)
{
_diskProvider = diskProvider;
_requestMappers = requestMappers;
_logger = logger;
}
public Response ProcessStaticResourceRequest(NancyContext context, string workingFolder)
{
var path = context.Request.Url.Path.ToLower();
2013-05-18 01:18:02 +00:00
if (string.IsNullOrWhiteSpace(path))
{
2013-05-18 01:18:02 +00:00
return null;
}
var mapper = _requestMappers.SingleOrDefault(m => m.CanHandle(path));
if (mapper != null)
{
var filePath = mapper.Map(path);
2013-05-18 01:18:02 +00:00
if (_diskProvider.FileExists(filePath))
{
var response = new StreamResponse(() => File.OpenRead(filePath), MimeTypes.GetMimeType(filePath));
2013-07-01 04:50:51 +00:00
if (RuntimeInfo.IsProduction)
{
response.Headers.EnableCache();
}
return response;
}
_logger.Warn("File {0} not found", filePath);
}
return null;
}
}
}