From 16e13e0c24d568223a5cec282000ad1dc4bc1194 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Thu, 16 May 2013 20:03:52 -0700 Subject: [PATCH] Posters/Banners/Fanart served from App_Data --- .../Frontend/IMapHttpRequestsToDisk.cs | 19 ++++++++++++++ NzbDrone.Api/Frontend/MediaCoverMapper.cs | 25 +++++++++++++++++++ NzbDrone.Api/Frontend/StaticResourceMapper.cs | 7 ++---- .../Frontend/StaticResourceProvider.cs | 22 +++++++++++++--- NzbDrone.Api/NzbDrone.Api.csproj | 2 ++ NzbDrone.Core/MediaCover/MediaCoverService.cs | 2 +- UI/Series/SeriesModel.js | 23 ++++------------- 7 files changed, 72 insertions(+), 28 deletions(-) create mode 100644 NzbDrone.Api/Frontend/IMapHttpRequestsToDisk.cs create mode 100644 NzbDrone.Api/Frontend/MediaCoverMapper.cs diff --git a/NzbDrone.Api/Frontend/IMapHttpRequestsToDisk.cs b/NzbDrone.Api/Frontend/IMapHttpRequestsToDisk.cs new file mode 100644 index 000000000..d837ff060 --- /dev/null +++ b/NzbDrone.Api/Frontend/IMapHttpRequestsToDisk.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NzbDrone.Api.Frontend +{ + public interface IMapHttpRequestsToDisk + { + string Map(string resourceUrl); + RequestType IHandle { get; } + } + + public enum RequestType + { + StaticResources, + MediaCovers + } +} diff --git a/NzbDrone.Api/Frontend/MediaCoverMapper.cs b/NzbDrone.Api/Frontend/MediaCoverMapper.cs new file mode 100644 index 000000000..b11c5300a --- /dev/null +++ b/NzbDrone.Api/Frontend/MediaCoverMapper.cs @@ -0,0 +1,25 @@ +using System.IO; +using NzbDrone.Common; + +namespace NzbDrone.Api.Frontend +{ + public class MediaCoverMapper : IMapHttpRequestsToDisk + { + private readonly IEnvironmentProvider _environmentProvider; + + public MediaCoverMapper(IEnvironmentProvider environmentProvider) + { + _environmentProvider = environmentProvider; + } + + public string Map(string resourceUrl) + { + var path = resourceUrl.Replace('/', Path.DirectorySeparatorChar); + path = path.Trim(Path.DirectorySeparatorChar).ToLower(); + + return Path.Combine(_environmentProvider.GetAppDataPath(), path); + } + + public RequestType IHandle { get { return RequestType.MediaCovers; } } + } +} \ No newline at end of file diff --git a/NzbDrone.Api/Frontend/StaticResourceMapper.cs b/NzbDrone.Api/Frontend/StaticResourceMapper.cs index 9ef688ee0..ca6fe0574 100644 --- a/NzbDrone.Api/Frontend/StaticResourceMapper.cs +++ b/NzbDrone.Api/Frontend/StaticResourceMapper.cs @@ -2,11 +2,6 @@ using System.IO; namespace NzbDrone.Api.Frontend { - public interface IMapHttpRequestsToDisk - { - string Map(string resourceUrl); - } - public class StaticResourceMapper : IMapHttpRequestsToDisk { public string Map(string resourceUrl) @@ -17,5 +12,7 @@ namespace NzbDrone.Api.Frontend return Path.Combine("ui", path); } + + public RequestType IHandle { get { return RequestType.StaticResources; } } } } \ No newline at end of file diff --git a/NzbDrone.Api/Frontend/StaticResourceProvider.cs b/NzbDrone.Api/Frontend/StaticResourceProvider.cs index 95bdc5d72..4099bb6c2 100644 --- a/NzbDrone.Api/Frontend/StaticResourceProvider.cs +++ b/NzbDrone.Api/Frontend/StaticResourceProvider.cs @@ -1,3 +1,5 @@ +using System.Collections.Generic; +using System.IO; using System.Linq; using NLog; using Nancy; @@ -14,13 +16,13 @@ namespace NzbDrone.Api.Frontend public class StaticResourceProvider : IProcessStaticResource { private readonly IDiskProvider _diskProvider; - private readonly IMapHttpRequestsToDisk _requestMapper; + private readonly IEnumerable _requestMappers; private readonly Logger _logger; - public StaticResourceProvider(IDiskProvider diskProvider, IMapHttpRequestsToDisk requestMapper, Logger logger) + public StaticResourceProvider(IDiskProvider diskProvider, IEnumerable requestMappers, Logger logger) { _diskProvider = diskProvider; - _requestMapper = requestMapper; + _requestMappers = requestMappers; _logger = logger; } @@ -28,9 +30,21 @@ 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 = _requestMapper.Map(path); + var filePath = _requestMappers.Single(r => r.IHandle == RequestType.StaticResources).Map(path); if (_diskProvider.FileExists(filePath)) { diff --git a/NzbDrone.Api/NzbDrone.Api.csproj b/NzbDrone.Api/NzbDrone.Api.csproj index 7694cf32b..d4c9ddee1 100644 --- a/NzbDrone.Api/NzbDrone.Api.csproj +++ b/NzbDrone.Api/NzbDrone.Api.csproj @@ -97,6 +97,8 @@ + + diff --git a/NzbDrone.Core/MediaCover/MediaCoverService.cs b/NzbDrone.Core/MediaCover/MediaCoverService.cs index 282d39b6b..7fe14893c 100644 --- a/NzbDrone.Core/MediaCover/MediaCoverService.cs +++ b/NzbDrone.Core/MediaCover/MediaCoverService.cs @@ -75,7 +75,7 @@ namespace NzbDrone.Core.MediaCover private string GetSeriesCoverPath(int seriesId) { - return Path.Combine(_coverRootFolder, seriesId.ToString("0000")); + return Path.Combine(_coverRootFolder, seriesId.ToString()); } } } diff --git a/UI/Series/SeriesModel.js b/UI/Series/SeriesModel.js index d30557e4e..324c81f4c 100644 --- a/UI/Series/SeriesModel.js +++ b/UI/Series/SeriesModel.js @@ -21,27 +21,14 @@ define(['app', 'Quality/QualityProfileCollection', 'AddSeries/RootFolders/RootFo return percent; }, + banner : function () { + return "/mediacover/" + this.get('id') + "/banner.jpg"; + }, poster : function () { - var poster = _.find(this.get('images'), function (image) { - return image.coverType === 'poster'; - }); - - if (poster) { - return poster.url; - } - - return undefined; + return "/mediacover/" + this.get('id') + "/poster.jpg"; }, fanArt : function () { - var poster = _.find(this.get('images'), function (image) { - return image.coverType === 3; - }); - - if (poster) { - return poster.url; - } - - return undefined; + return "/mediacover/" + this.get('id') + "/fanart.jpg"; }, traktUrl : function () { return "http://trakt.tv/show/" + this.get('titleSlug');