1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00
Sonarr/NzbDrone.Api/Frontend/StaticResourceProvider.cs

71 lines
2.1 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.Common.EnvironmentInfo;
namespace NzbDrone.Api.Frontend
{
public interface IProcessStaticResource
{
Response ProcessStaticResourceRequest(NancyContext context, string workingFolder);
}
public class StaticResourceProvider : IProcessStaticResource
{
2013-05-11 02:53:50 +03:00
private readonly IDiskProvider _diskProvider;
private readonly IEnumerable<IMapHttpRequestsToDisk> _requestMappers;
2013-07-14 07:19:36 +03:00
private readonly IAddCacheHeaders _addCacheHeaders;
private readonly Logger _logger;
private readonly bool _caseSensitive;
2013-07-01 07:50:51 +03:00
public StaticResourceProvider(IDiskProvider diskProvider,
IEnumerable<IMapHttpRequestsToDisk> requestMappers,
2013-07-14 07:19:36 +03:00
IAddCacheHeaders addCacheHeaders,
2013-07-01 07:50:51 +03:00
Logger logger)
{
_diskProvider = diskProvider;
_requestMappers = requestMappers;
2013-07-14 07:19:36 +03:00
_addCacheHeaders = addCacheHeaders;
_logger = logger;
if (!RuntimeInfo.IsProduction)
{
_caseSensitive = true;
}
}
public Response ProcessStaticResourceRequest(NancyContext context, string workingFolder)
{
var path = context.Request.Url.Path;
2013-05-18 04:18:02 +03:00
if (string.IsNullOrWhiteSpace(path))
{
2013-05-18 04:18:02 +03:00
return null;
}
var mapper = _requestMappers.SingleOrDefault(m => m.CanHandle(path));
if (mapper != null)
{
var filePath = mapper.Map(path);
2013-05-18 04:18:02 +03:00
if (_diskProvider.FileExists(filePath, _caseSensitive))
{
var response = new StreamResponse(() => File.OpenRead(filePath), MimeTypes.GetMimeType(filePath));
2013-07-14 07:19:36 +03:00
_addCacheHeaders.ToResponse(context.Request, response);
2013-07-01 07:50:51 +03:00
2013-07-07 09:50:49 +03:00
return response;
}
_logger.Warn("File {0} not found", filePath);
}
return null;
}
}
}