From 1edb1d211ba42c88bce919736465f76e8f1880e4 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Fri, 17 May 2013 18:18:02 -0700 Subject: [PATCH] Fixed up StaticResourceProvider --- .../Frontend/IMapHttpRequestsToDisk.cs | 8 +-- NzbDrone.Api/Frontend/MediaCoverMapper.cs | 5 +- NzbDrone.Api/Frontend/StaticResourceMapper.cs | 13 ++++- .../Frontend/StaticResourceProvider.cs | 51 ++++++------------- 4 files changed, 33 insertions(+), 44 deletions(-) diff --git a/NzbDrone.Api/Frontend/IMapHttpRequestsToDisk.cs b/NzbDrone.Api/Frontend/IMapHttpRequestsToDisk.cs index d837ff060..fc5811cdd 100644 --- a/NzbDrone.Api/Frontend/IMapHttpRequestsToDisk.cs +++ b/NzbDrone.Api/Frontend/IMapHttpRequestsToDisk.cs @@ -8,12 +8,6 @@ namespace NzbDrone.Api.Frontend public interface IMapHttpRequestsToDisk { string Map(string resourceUrl); - RequestType IHandle { get; } - } - - public enum RequestType - { - StaticResources, - MediaCovers + bool CanHandle(string resourceUrl); } } diff --git a/NzbDrone.Api/Frontend/MediaCoverMapper.cs b/NzbDrone.Api/Frontend/MediaCoverMapper.cs index b11c5300a..ed757e266 100644 --- a/NzbDrone.Api/Frontend/MediaCoverMapper.cs +++ b/NzbDrone.Api/Frontend/MediaCoverMapper.cs @@ -20,6 +20,9 @@ namespace NzbDrone.Api.Frontend return Path.Combine(_environmentProvider.GetAppDataPath(), path); } - public RequestType IHandle { get { return RequestType.MediaCovers; } } + public bool CanHandle(string resourceUrl) + { + return resourceUrl.StartsWith("/mediacover"); + } } } \ No newline at end of file diff --git a/NzbDrone.Api/Frontend/StaticResourceMapper.cs b/NzbDrone.Api/Frontend/StaticResourceMapper.cs index ca6fe0574..384705e2f 100644 --- a/NzbDrone.Api/Frontend/StaticResourceMapper.cs +++ b/NzbDrone.Api/Frontend/StaticResourceMapper.cs @@ -1,9 +1,12 @@ using System.IO; +using System.Linq; namespace NzbDrone.Api.Frontend { 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) { var path = resourceUrl.Replace('/', Path.DirectorySeparatorChar); @@ -13,6 +16,14 @@ namespace NzbDrone.Api.Frontend 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); + } } } \ No newline at end of file diff --git a/NzbDrone.Api/Frontend/StaticResourceProvider.cs b/NzbDrone.Api/Frontend/StaticResourceProvider.cs index 4099bb6c2..a3334edc8 100644 --- a/NzbDrone.Api/Frontend/StaticResourceProvider.cs +++ b/NzbDrone.Api/Frontend/StaticResourceProvider.cs @@ -30,45 +30,26 @@ namespace NzbDrone.Api.Frontend { var path = context.Request.Url.Path.ToLower(); - if (path.StartsWith("/mediacover")) - { - var filePath = _requestMappers.Single(r => r.IHandle == RequestType.MediaCovers).Map(path); - - if (_diskProvider.FileExists(filePath)) - { - return new StreamResponse(() => File.OpenRead(filePath), "image/jpeg"); - } - - _logger.Warn("Couldn't find file [{0}] for [{1}]", filePath, path); - } - - if (IsStaticResource(path)) - { - var filePath = _requestMappers.Single(r => r.IHandle == RequestType.StaticResources).Map(path); - - if (_diskProvider.FileExists(filePath)) - { - return new GenericFileResponse(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 null; } - return Extensions.Any(path.EndsWith); + foreach (var requestMapper in _requestMappers) + { + if (requestMapper.CanHandle(path)) + { + var filePath = requestMapper.Map(path); + + if (_diskProvider.FileExists(filePath)) + { + return new StreamResponse(() => File.OpenRead(filePath), MimeTypes.GetMimeType(filePath)); + } + } + } + + _logger.Warn("Couldn't find a matching file for: {0}", path); + return null; } } } \ No newline at end of file