Fixed up StaticResourceProvider

pull/2/head
Mark McDowall 12 years ago
parent 92d913e4ac
commit 1edb1d211b

@ -8,12 +8,6 @@ namespace NzbDrone.Api.Frontend
public interface IMapHttpRequestsToDisk public interface IMapHttpRequestsToDisk
{ {
string Map(string resourceUrl); string Map(string resourceUrl);
RequestType IHandle { get; } bool CanHandle(string resourceUrl);
}
public enum RequestType
{
StaticResources,
MediaCovers
} }
} }

@ -20,6 +20,9 @@ namespace NzbDrone.Api.Frontend
return Path.Combine(_environmentProvider.GetAppDataPath(), path); return Path.Combine(_environmentProvider.GetAppDataPath(), path);
} }
public RequestType IHandle { get { return RequestType.MediaCovers; } } public bool CanHandle(string resourceUrl)
{
return resourceUrl.StartsWith("/mediacover");
}
} }
} }

@ -1,9 +1,12 @@
using System.IO; using System.IO;
using System.Linq;
namespace NzbDrone.Api.Frontend namespace NzbDrone.Api.Frontend
{ {
public class StaticResourceMapper : IMapHttpRequestsToDisk public class StaticResourceMapper : IMapHttpRequestsToDisk
{ {
private static readonly string[] Extensions = new[] { ".css", ".js", ".html", ".htm", ".jpg", ".jpeg", ".icon", ".gif", ".png", ".woff", ".ttf" };
public string Map(string resourceUrl) public string Map(string resourceUrl)
{ {
var path = resourceUrl.Replace('/', Path.DirectorySeparatorChar); var path = resourceUrl.Replace('/', Path.DirectorySeparatorChar);
@ -13,6 +16,14 @@ namespace NzbDrone.Api.Frontend
return Path.Combine("ui", path); return Path.Combine("ui", path);
} }
public RequestType IHandle { get { return RequestType.StaticResources; } } public bool CanHandle(string resourceUrl)
{
if (string.IsNullOrWhiteSpace(resourceUrl))
{
return false;
}
return Extensions.Any(resourceUrl.EndsWith);
}
} }
} }

@ -30,45 +30,26 @@ namespace NzbDrone.Api.Frontend
{ {
var path = context.Request.Url.Path.ToLower(); var path = context.Request.Url.Path.ToLower();
if (path.StartsWith("/mediacover")) if (string.IsNullOrWhiteSpace(path))
{
var filePath = _requestMappers.Single(r => r.IHandle == RequestType.MediaCovers).Map(path);
if (_diskProvider.FileExists(filePath))
{ {
return new StreamResponse(() => File.OpenRead(filePath), "image/jpeg"); return null;
}
_logger.Warn("Couldn't find file [{0}] for [{1}]", filePath, path);
} }
if (IsStaticResource(path)) foreach (var requestMapper in _requestMappers)
{
if (requestMapper.CanHandle(path))
{ {
var filePath = _requestMappers.Single(r => r.IHandle == RequestType.StaticResources).Map(path); var filePath = requestMapper.Map(path);
if (_diskProvider.FileExists(filePath)) if (_diskProvider.FileExists(filePath))
{ {
return new GenericFileResponse(filePath); return new StreamResponse(() => File.OpenRead(filePath), MimeTypes.GetMimeType(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); _logger.Warn("Couldn't find a matching file for: {0}", path);
return null;
} }
} }
} }
Loading…
Cancel
Save