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;
|
2013-07-24 06:26:10 +00:00
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2013-03-29 23:00:38 +00:00
|
|
|
|
|
|
|
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-07-14 04:19:36 +00:00
|
|
|
private readonly IAddCacheHeaders _addCacheHeaders;
|
2013-03-29 23:00:38 +00:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2013-07-24 06:26:10 +00:00
|
|
|
private readonly bool _caseSensitive;
|
|
|
|
|
2013-07-01 04:50:51 +00:00
|
|
|
public StaticResourceProvider(IDiskProvider diskProvider,
|
|
|
|
IEnumerable<IMapHttpRequestsToDisk> requestMappers,
|
2013-07-14 04:19:36 +00:00
|
|
|
IAddCacheHeaders addCacheHeaders,
|
2013-07-01 04:50:51 +00:00
|
|
|
Logger logger)
|
2013-03-29 23:00:38 +00:00
|
|
|
{
|
|
|
|
_diskProvider = diskProvider;
|
2013-05-17 03:03:52 +00:00
|
|
|
_requestMappers = requestMappers;
|
2013-07-14 04:19:36 +00:00
|
|
|
_addCacheHeaders = addCacheHeaders;
|
2013-03-29 23:00:38 +00:00
|
|
|
_logger = logger;
|
2013-07-24 06:26:10 +00:00
|
|
|
|
|
|
|
if (!RuntimeInfo.IsProduction)
|
|
|
|
{
|
|
|
|
_caseSensitive = true;
|
|
|
|
}
|
2013-03-29 23:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Response ProcessStaticResourceRequest(NancyContext context, string workingFolder)
|
|
|
|
{
|
2013-07-24 06:26:10 +00:00
|
|
|
var path = context.Request.Url.Path;
|
2013-03-29 23:00:38 +00:00
|
|
|
|
2013-05-18 01:18:02 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(path))
|
2013-05-17 03:03:52 +00:00
|
|
|
{
|
2013-05-18 01:18:02 +00:00
|
|
|
return null;
|
2013-05-17 03:03:52 +00:00
|
|
|
}
|
|
|
|
|
2013-05-20 01:07:26 +00:00
|
|
|
var mapper = _requestMappers.SingleOrDefault(m => m.CanHandle(path));
|
|
|
|
|
|
|
|
if (mapper != null)
|
2013-03-29 23:00:38 +00:00
|
|
|
{
|
2013-05-20 01:07:26 +00:00
|
|
|
var filePath = mapper.Map(path);
|
2013-05-18 01:18:02 +00:00
|
|
|
|
2013-07-24 06:26:10 +00:00
|
|
|
if (_diskProvider.FileExists(filePath, _caseSensitive))
|
2013-05-20 01:07:26 +00:00
|
|
|
{
|
2013-06-28 19:19:40 +00:00
|
|
|
var response = new StreamResponse(() => File.OpenRead(filePath), MimeTypes.GetMimeType(filePath));
|
2013-07-14 04:19:36 +00:00
|
|
|
_addCacheHeaders.ToResponse(context.Request, response);
|
2013-07-01 04:50:51 +00:00
|
|
|
|
2013-07-07 06:50:49 +00:00
|
|
|
return response;
|
2013-03-29 23:00:38 +00:00
|
|
|
}
|
2013-05-20 01:07:26 +00:00
|
|
|
|
|
|
|
_logger.Warn("File {0} not found", filePath);
|
2013-03-29 23:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|