From b145ea1a707055948559e1e23abbe9ad20019240 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Thu, 29 Jan 2015 21:13:24 +0100 Subject: [PATCH] UI now loads the 250px image if available, and reverts to full size otherwise. --- .../Frontend/Mappers/MediaCoverMapper.cs | 22 +++++++++++++++++++ .../MediaCovers/MediaCoverModule.cs | 14 +++++++++++- src/UI/Handlebars/Helpers/Html.js | 12 ++++++++-- src/UI/Handlebars/Helpers/Series.js | 2 +- 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/NzbDrone.Api/Frontend/Mappers/MediaCoverMapper.cs b/src/NzbDrone.Api/Frontend/Mappers/MediaCoverMapper.cs index e0c967a7e..58e14020d 100644 --- a/src/NzbDrone.Api/Frontend/Mappers/MediaCoverMapper.cs +++ b/src/NzbDrone.Api/Frontend/Mappers/MediaCoverMapper.cs @@ -1,4 +1,6 @@ using System.IO; +using System.Text.RegularExpressions; +using Nancy; using NLog; using NzbDrone.Common.Disk; using NzbDrone.Common.EnvironmentInfo; @@ -8,6 +10,8 @@ namespace NzbDrone.Api.Frontend.Mappers { public class MediaCoverMapper : StaticResourceMapperBase { + private static readonly Regex RegexResizedImage = new Regex(@"-\d+\.jpg($|\?)", RegexOptions.Compiled | RegexOptions.IgnoreCase); + private readonly IAppFolderInfo _appFolderInfo; public MediaCoverMapper(IAppFolderInfo appFolderInfo, IDiskProvider diskProvider, Logger logger) @@ -24,6 +28,24 @@ namespace NzbDrone.Api.Frontend.Mappers return Path.Combine(_appFolderInfo.GetAppDataPath(), path); } + public override Response GetResponse(string resourceUrl) + { + var result = base.GetResponse(resourceUrl); + + // Return the full sized image if someone requests a non-existing resized one. + // TODO: This code can be removed later once everyone had the update for a while. + if (result is NotFoundResponse) + { + var baseResourceUrl = RegexResizedImage.Replace(resourceUrl, ".jpg$1"); + if (baseResourceUrl != resourceUrl) + { + result = base.GetResponse(baseResourceUrl); + } + } + + return result; + } + public override bool CanHandle(string resourceUrl) { return resourceUrl.StartsWith("/MediaCover"); diff --git a/src/NzbDrone.Api/MediaCovers/MediaCoverModule.cs b/src/NzbDrone.Api/MediaCovers/MediaCoverModule.cs index e569b870f..08fbeec08 100644 --- a/src/NzbDrone.Api/MediaCovers/MediaCoverModule.cs +++ b/src/NzbDrone.Api/MediaCovers/MediaCoverModule.cs @@ -1,4 +1,5 @@ using System.IO; +using System.Text.RegularExpressions; using Nancy; using Nancy.Responses; using NzbDrone.Common.Disk; @@ -9,6 +10,8 @@ namespace NzbDrone.Api.MediaCovers { public class MediaCoverModule : NzbDroneApiModule { + private static readonly Regex RegexResizedImage = new Regex(@"-\d+\.jpg$", RegexOptions.Compiled | RegexOptions.IgnoreCase); + private const string MEDIA_COVER_ROUTE = @"/(?\d+)/(?(.+)\.(jpg|png|gif))"; private readonly IAppFolderInfo _appFolderInfo; @@ -27,7 +30,16 @@ namespace NzbDrone.Api.MediaCovers var filePath = Path.Combine(_appFolderInfo.GetAppDataPath(), "MediaCover", seriesId.ToString(), filename); if (!_diskProvider.FileExists(filePath)) - return new NotFoundResponse(); + { + // Return the full sized image if someone requests a non-existing resized one. + // TODO: This code can be removed later once everyone had the update for a while. + var basefilePath = RegexResizedImage.Replace(filePath, ".jpg"); + if (basefilePath == filePath || !_diskProvider.FileExists(basefilePath)) + { + return new NotFoundResponse(); + } + filePath = basefilePath; + } return new StreamResponse(() => File.OpenRead(filePath), MimeTypes.GetMimeType(filePath)); } diff --git a/src/UI/Handlebars/Helpers/Html.js b/src/UI/Handlebars/Helpers/Html.js index 4cc726f82..300e1de68 100644 --- a/src/UI/Handlebars/Helpers/Html.js +++ b/src/UI/Handlebars/Helpers/Html.js @@ -17,8 +17,16 @@ define( img.onerror = null; }; - Handlebars.registerHelper('defaultImg', function () { - return new Handlebars.SafeString('onerror=window.NzbDrone.imageError(this)'); + Handlebars.registerHelper('defaultImg', function (src, size) { + if (!src) { + return new Handlebars.SafeString('onerror="window.NzbDrone.imageError(this);"'); + } + + if (size) { + src = src.replace(/\.jpg($|\?)/g, '-' + size + '.jpg$1'); + } + + return new Handlebars.SafeString('src="{0}" onerror="window.NzbDrone.imageError(this);"'.format(src)); }); Handlebars.registerHelper('UrlBase', function () { diff --git a/src/UI/Handlebars/Helpers/Series.js b/src/UI/Handlebars/Helpers/Series.js index 5c8723bb9..e34ba325d 100644 --- a/src/UI/Handlebars/Helpers/Series.js +++ b/src/UI/Handlebars/Helpers/Series.js @@ -11,7 +11,7 @@ define( var poster = _.where(this.images, {coverType: 'poster'}); if (poster[0]) { - return new Handlebars.SafeString(''.format(poster[0].url, Handlebars.helpers.defaultImg.call())); + return new Handlebars.SafeString(''.format(Handlebars.helpers.defaultImg.call(null, poster[0].url, 250))); } return new Handlebars.SafeString(''.format(placeholder));