1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

added caching breaker to media cover images.

This commit is contained in:
Keivan Beigi 2013-06-12 18:37:05 -07:00
parent 1585234055
commit 38589742e3
10 changed files with 115 additions and 54 deletions

View File

@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NzbDrone.Api.Frontend
{
public interface IMapHttpRequestsToDisk

View File

@ -3,6 +3,7 @@
using System.Linq;
using FluentValidation;
using Nancy;
using NzbDrone.Core.MediaCover;
using NzbDrone.Core.SeriesStats;
using NzbDrone.Core.Tv;
using NzbDrone.Api.Validation;
@ -14,12 +15,14 @@ public class SeriesModule : NzbDroneRestModule<SeriesResource>
{
private readonly ISeriesService _seriesService;
private readonly ISeriesStatisticsService _seriesStatisticsService;
private readonly IMapCoversToLocal _coverMapper;
public SeriesModule(ISeriesService seriesService, ISeriesStatisticsService seriesStatisticsService)
public SeriesModule(ISeriesService seriesService, ISeriesStatisticsService seriesStatisticsService, IMapCoversToLocal coverMapper)
: base("/Series")
{
_seriesService = seriesService;
_seriesStatisticsService = seriesStatisticsService;
_coverMapper = coverMapper;
GetResourceAll = AllSeries;
GetResourceById = GetSeries;
@ -51,9 +54,9 @@ private Response GetSeries(string slug)
private List<SeriesResource> AllSeries()
{
var seriesStats = _seriesStatisticsService.SeriesStatistics();
var seriesModels = ToListResource(_seriesService.GetAllSeries);
var seriesResources = ToListResource(_seriesService.GetAllSeries);
foreach (var s in seriesModels)
foreach (var s in seriesResources)
{
var stats = seriesStats.SingleOrDefault(ss => ss.SeriesId == s.Id);
if (stats == null) continue;
@ -64,12 +67,18 @@ private List<SeriesResource> AllSeries()
s.NextAiring = stats.NextAiring;
}
return seriesModels;
MapCoversToLocal(seriesResources.ToArray());
return seriesResources;
}
private SeriesResource GetSeries(int id)
{
return ToResource(_seriesService.GetSeries, id);
var resource = ToResource(_seriesService.GetSeries, id);
MapCoversToLocal(resource);
return resource;
}
private SeriesResource AddSeries(SeriesResource seriesResource)
@ -92,6 +101,14 @@ private void DeleteSeries(int id)
var deleteFiles = Convert.ToBoolean(Request.Headers["deleteFiles"].FirstOrDefault());
_seriesService.DeleteSeries(id, deleteFiles);
}
private void MapCoversToLocal(params SeriesResource[] series)
{
foreach (var seriesResource in series)
{
_coverMapper.ConvertToLocalUrls(seriesResource.Id, seriesResource.Images);
}
}
}
}

View File

@ -33,8 +33,7 @@ public static bool IsSimpleType(this Type type)
|| type == typeof(string)
|| type == typeof(DateTime)
|| type == typeof(Version)
|| type == typeof(Decimal)
|| type.GetInterface("IEmbeddedDocument") != null;
|| type == typeof(Decimal);
}
public static bool IsReadable(this PropertyInfo propertyInfo)

View File

@ -1,7 +1,12 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Common;
using NzbDrone.Core.MediaCover;
using NzbDrone.Core.Test.Framework;
using System.Linq;
namespace NzbDrone.Core.Test.MediaCoverTests
{
@ -11,9 +16,44 @@ public class MediaCoverServiceFixture : CoreTest<MediaCoverService>
[SetUp]
public void Setup()
{
Mocker.SetConstant(new HttpProvider(new EnvironmentProvider()));
Mocker.SetConstant(new DiskProvider());
Mocker.SetConstant(new EnvironmentProvider());
//Mocker.SetConstant(new HttpProvider(new EnvironmentProvider()));
//Mocker.SetConstant(new DiskProvider());
Mocker.SetConstant<IEnvironmentProvider>(new EnvironmentProvider());
}
[Test]
public void should_convert_trakts_urls_to_local()
{
var covers = new List<MediaCover.MediaCover>
{
new MediaCover.MediaCover {CoverType = MediaCoverTypes.Banner}
};
Mocker.GetMock<IDiskProvider>().Setup(c => c.GetLastFileWrite(It.IsAny<string>()))
.Returns(new DateTime(1234));
Mocker.GetMock<IDiskProvider>().Setup(c => c.FileExists(It.IsAny<string>()))
.Returns(true);
Subject.ConvertToLocalUrls(12, covers);
covers.Single().Url.Should().Be("/mediacover/12/banner.jpg?lastWrite=1234");
}
[Test]
public void should_convert_trakts_urls_to_local_without_time_if_file_doesnt_exist()
{
var covers = new List<MediaCover.MediaCover>
{
new MediaCover.MediaCover {CoverType = MediaCoverTypes.Banner}
};
Subject.ConvertToLocalUrls(12, covers);
covers.Single().Url.Should().Be("/mediacover/12/banner.jpg");
}
}

View File

@ -1,20 +1,8 @@
using System;
using FizzWare.NBuilder;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Core.Model;
using NzbDrone.Core.Notifications;
using NUnit.Framework;
using NzbDrone.Core.Notifications.Growl;
using NzbDrone.Core.Providers;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common.AutoMoq;
namespace NzbDrone.Core.Test.ProviderTests
namespace NzbDrone.Core.Test.NotificationTests
{
[Explicit]
[TestFixture]

View File

@ -1,9 +1,7 @@
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using FizzWare.NBuilder;
using FluentAssertions;
using Moq;
using NUnit.Framework;
@ -11,7 +9,7 @@
using NzbDrone.Core.Notifications.Plex;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.ProviderTests
namespace NzbDrone.Core.Test.NotificationTests
{
[TestFixture]

View File

@ -1,21 +1,11 @@
using System;
using FizzWare.NBuilder;
using FluentAssertions;
using Moq;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Model;
using NzbDrone.Core.Notifications.Prowl;
using NzbDrone.Core.Providers;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common;
using NzbDrone.Test.Common.AutoMoq;
using Prowlin;
namespace NzbDrone.Core.Test.ProviderTests
namespace NzbDrone.Core.Test.NotificationTests
{
[Explicit]
[TestFixture]

View File

@ -150,12 +150,10 @@
<Compile Include="MetadataSourceTests\TracktProxyFixture.cs" />
<Compile Include="NotificationTests\NotificationServiceFixture.cs" />
<Compile Include="NotificationTests\Xbmc\GetJsonVersionFixture.cs" />
<Compile Include="NotificationTests\Xbmc\HttpApiProviderFixture.cs" />
<Compile Include="NotificationTests\Xbmc\Http\ActivePlayersFixture.cs" />
<Compile Include="NotificationTests\Xbmc\Http\CheckForErrorFixture.cs" />
<Compile Include="NotificationTests\Xbmc\Http\GetSeriesPathFixture.cs" />
<Compile Include="NotificationTests\Xbmc\Http\UpdateFixture.cs" />
<Compile Include="NotificationTests\Xbmc\JsonApiProviderFixture.cs" />
<Compile Include="NotificationTests\Xbmc\Json\ActivePlayersFixture.cs" />
<Compile Include="NotificationTests\Xbmc\Json\CheckForErrorFixture.cs" />
<Compile Include="NotificationTests\Xbmc\Json\GetSeriesPathFixture.cs" />
@ -198,7 +196,6 @@
<Compile Include="HelperTests\SortHelperTest.cs" />
<Compile Include="DecisionEngineTests\AcceptableSizeSpecificationFixture.cs" />
<Compile Include="Qualities\QualitySizeServiceFixture.cs" />
<Compile Include="NotificationTests\Xbmc\XbmcServiceFixture.cs" />
<Compile Include="TvTests\EpisodeProviderTests\EpisodeProviderTest_GetEpisodesByParseResult.cs" />
<Compile Include="ProviderTests\DiskScanProviderTests\ImportFileFixture.cs" />
<Compile Include="FluentTest.cs" />

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using NLog;
using NzbDrone.Common;
@ -10,7 +11,8 @@ namespace NzbDrone.Core.MediaCover
{
public class MediaCoverService :
IHandleAsync<SeriesUpdatedEvent>,
IHandleAsync<SeriesDeletedEvent>
IHandleAsync<SeriesDeletedEvent>,
IMapCoversToLocal
{
private readonly IHttpProvider _httpProvider;
private readonly IDiskProvider _diskProvider;
@ -80,5 +82,26 @@ private string GetSeriesCoverPath(int seriesId)
{
return Path.Combine(_coverRootFolder, seriesId.ToString());
}
public void ConvertToLocalUrls(int seriesId, IEnumerable<MediaCover> covers)
{
foreach (var mediaCover in covers)
{
var filePath = GetCoverPath(seriesId, mediaCover.CoverType);
mediaCover.Url = @"/mediacover/" + seriesId + "/" + mediaCover.CoverType.ToString().ToLower() + ".jpg";
if (_diskProvider.FileExists(filePath))
{
var lastWrite = _diskProvider.GetLastFileWrite(filePath);
mediaCover.Url += "?lastWrite=" + lastWrite.Ticks;
}
}
}
}
public interface IMapCoversToLocal
{
void ConvertToLocalUrls(int seriesId, IEnumerable<MediaCover> covers);
}
}

View File

@ -17,14 +17,27 @@ define(['app', 'Quality/QualityProfileCollection'], function (app, qualityProfil
return percent;
},
banner : function () {
return "/mediacover/" + this.get('id') + "/banner.jpg";
},
poster : function () {
return "/mediacover/" + this.get('id') + "/poster.jpg";
var poster = _.find(this.get('images'), function (image) {
return image.coverType === 'poster';
});
if (poster) {
return poster.url;
}
return undefined;
},
fanArt : function () {
return "/mediacover/" + this.get('id') + "/fanart.jpg";
var poster = _.find(this.get('images'), function (image) {
return image.coverType === 3;
});
if (poster) {
return poster.url;
}
return undefined;
},
traktUrl : function () {
return "http://trakt.tv/show/" + this.get('titleSlug');