parent
290e072f2e
commit
9f0ae763ff
@ -1,32 +0,0 @@
|
||||
using System;
|
||||
using Nancy;
|
||||
using NzbDrone.Api.Extensions;
|
||||
using NzbDrone.Common.EnvironmentInfo;
|
||||
|
||||
namespace NzbDrone.Api.Frontend
|
||||
{
|
||||
public interface IAddCacheHeaders
|
||||
{
|
||||
void ToResponse(Request request, Response response);
|
||||
}
|
||||
|
||||
public class AddCacheHeaders : IAddCacheHeaders
|
||||
{
|
||||
public void ToResponse(Request request, Response response)
|
||||
{
|
||||
if (!RuntimeInfo.IsProduction)
|
||||
{
|
||||
response.Headers.DisableCache();
|
||||
return;
|
||||
}
|
||||
|
||||
if (request.Url.Path.EndsWith("app.js", StringComparison.CurrentCultureIgnoreCase))
|
||||
{
|
||||
response.Headers.DisableCache();
|
||||
return;
|
||||
}
|
||||
|
||||
response.Headers.EnableCache();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,25 +1,47 @@
|
||||
using System.IO;
|
||||
using NLog;
|
||||
using NzbDrone.Common;
|
||||
using NzbDrone.Common.EnvironmentInfo;
|
||||
|
||||
namespace NzbDrone.Api.Frontend.Mappers
|
||||
{
|
||||
public class IndexHtmlMapper : IMapHttpRequestsToDisk
|
||||
public class IndexHtmlMapper : StaticResourceMapperBase
|
||||
{
|
||||
private readonly IDiskProvider _diskProvider;
|
||||
private readonly string _indexPath;
|
||||
|
||||
public IndexHtmlMapper(IAppFolderInfo appFolderInfo)
|
||||
public IndexHtmlMapper(IAppFolderInfo appFolderInfo, IDiskProvider diskProvider, Logger logger)
|
||||
: base(diskProvider, logger)
|
||||
{
|
||||
_diskProvider = diskProvider;
|
||||
_indexPath = Path.Combine(appFolderInfo.StartUpFolder, "UI", "index.html");
|
||||
}
|
||||
|
||||
public string Map(string resourceUrl)
|
||||
protected override string Map(string resourceUrl)
|
||||
{
|
||||
return _indexPath;
|
||||
}
|
||||
|
||||
public bool CanHandle(string resourceUrl)
|
||||
public override bool CanHandle(string resourceUrl)
|
||||
{
|
||||
return !resourceUrl.Contains(".");
|
||||
}
|
||||
|
||||
protected override Stream GetContentStream(string filePath)
|
||||
{
|
||||
return StringToStream(GetIndexText());
|
||||
}
|
||||
|
||||
|
||||
private string GetIndexText()
|
||||
{
|
||||
var text = _diskProvider.ReadAllText(_indexPath);
|
||||
|
||||
text = text.Replace(".css", ".css?v=" + BuildInfo.Version);
|
||||
text = text.Replace(".js", ".js?v=" + BuildInfo.Version);
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
using System.IO;
|
||||
using NLog;
|
||||
using Nancy;
|
||||
using Nancy.Responses;
|
||||
using NzbDrone.Common;
|
||||
using NzbDrone.Common.EnvironmentInfo;
|
||||
|
||||
namespace NzbDrone.Api.Frontend.Mappers
|
||||
{
|
||||
public abstract class StaticResourceMapperBase : IMapHttpRequestsToDisk
|
||||
{
|
||||
private readonly IDiskProvider _diskProvider;
|
||||
private readonly Logger _logger;
|
||||
private readonly bool _caseSensitive;
|
||||
|
||||
private static readonly NotFoundResponse NotFoundResponse = new NotFoundResponse();
|
||||
|
||||
protected StaticResourceMapperBase(IDiskProvider diskProvider, Logger logger)
|
||||
{
|
||||
_diskProvider = diskProvider;
|
||||
_logger = logger;
|
||||
|
||||
if (!RuntimeInfo.IsProduction)
|
||||
{
|
||||
_caseSensitive = true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
protected abstract string Map(string resourceUrl);
|
||||
|
||||
|
||||
public abstract bool CanHandle(string resourceUrl);
|
||||
|
||||
public Response GetResponse(string resourceUrl)
|
||||
{
|
||||
var filePath = Map(resourceUrl);
|
||||
|
||||
if (_diskProvider.FileExists(filePath, _caseSensitive))
|
||||
{
|
||||
var response = new StreamResponse(() => GetContentStream(filePath), MimeTypes.GetMimeType(filePath));
|
||||
return response;
|
||||
}
|
||||
|
||||
_logger.Warn("File {0} not found", filePath);
|
||||
|
||||
return NotFoundResponse;
|
||||
}
|
||||
|
||||
protected virtual Stream GetContentStream(string filePath)
|
||||
{
|
||||
return File.OpenRead(filePath);
|
||||
}
|
||||
|
||||
protected static Stream StringToStream(string text)
|
||||
{
|
||||
var stream = new MemoryStream();
|
||||
var writer = new StreamWriter(stream);
|
||||
writer.Write(text);
|
||||
writer.Flush();
|
||||
stream.Position = 0;
|
||||
return stream;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,79 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
using Nancy;
|
||||
using Nancy.Responses;
|
||||
using NzbDrone.Api.Frontend.Mappers;
|
||||
using NzbDrone.Common;
|
||||
using NzbDrone.Common.EnvironmentInfo;
|
||||
|
||||
namespace NzbDrone.Api.Frontend
|
||||
{
|
||||
public interface IProcessStaticResource
|
||||
{
|
||||
Response ProcessStaticResourceRequest(NancyContext context, string workingFolder);
|
||||
}
|
||||
|
||||
public class StaticResourceProvider : IProcessStaticResource
|
||||
{
|
||||
private readonly IDiskProvider _diskProvider;
|
||||
private readonly IEnumerable<IMapHttpRequestsToDisk> _requestMappers;
|
||||
private readonly IAddCacheHeaders _addCacheHeaders;
|
||||
private readonly Logger _logger;
|
||||
|
||||
private readonly bool _caseSensitive;
|
||||
|
||||
public StaticResourceProvider(IDiskProvider diskProvider,
|
||||
IEnumerable<IMapHttpRequestsToDisk> requestMappers,
|
||||
IAddCacheHeaders addCacheHeaders,
|
||||
Logger logger)
|
||||
{
|
||||
_diskProvider = diskProvider;
|
||||
_requestMappers = requestMappers;
|
||||
_addCacheHeaders = addCacheHeaders;
|
||||
_logger = logger;
|
||||
|
||||
if (!RuntimeInfo.IsProduction)
|
||||
{
|
||||
_caseSensitive = true;
|
||||
}
|
||||
}
|
||||
|
||||
public Response ProcessStaticResourceRequest(NancyContext context, string workingFolder)
|
||||
{
|
||||
var path = context.Request.Url.Path;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (context.Request.Headers.IfModifiedSince.HasValue)
|
||||
{
|
||||
var response = new Response { ContentType = MimeTypes.GetMimeType(path), StatusCode = HttpStatusCode.NotModified };
|
||||
_addCacheHeaders.ToResponse(context.Request, response);
|
||||
return response;
|
||||
}
|
||||
|
||||
var mapper = _requestMappers.SingleOrDefault(m => m.CanHandle(path));
|
||||
|
||||
if (mapper != null)
|
||||
{
|
||||
var filePath = mapper.Map(path);
|
||||
|
||||
if (_diskProvider.FileExists(filePath, _caseSensitive))
|
||||
{
|
||||
var response = new StreamResponse(() => File.OpenRead(filePath), MimeTypes.GetMimeType(filePath));
|
||||
_addCacheHeaders.ToResponse(context.Request, response);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
_logger.Warn("File {0} not found", filePath);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue