mirror of
https://github.com/Sonarr/Sonarr.git
synced 2026-03-05 13:20:20 -05:00
Compare commits
3 Commits
v3.0.5.114
...
v2.0.0.285
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4394860fac | ||
|
|
1eeb40aede | ||
|
|
7aaf40d9d4 |
@@ -13,11 +13,13 @@ namespace NzbDrone.Api.Frontend.Mappers
|
||||
private static readonly Regex RegexResizedImage = new Regex(@"-\d+\.jpg($|\?)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
|
||||
private readonly IAppFolderInfo _appFolderInfo;
|
||||
private readonly IDiskProvider _diskProvider;
|
||||
|
||||
public MediaCoverMapper(IAppFolderInfo appFolderInfo, IDiskProvider diskProvider, Logger logger)
|
||||
: base(diskProvider, logger)
|
||||
{
|
||||
_appFolderInfo = appFolderInfo;
|
||||
_diskProvider = diskProvider;
|
||||
}
|
||||
|
||||
public override string Map(string resourceUrl)
|
||||
@@ -25,25 +27,18 @@ namespace NzbDrone.Api.Frontend.Mappers
|
||||
var path = resourceUrl.Replace('/', Path.DirectorySeparatorChar);
|
||||
path = path.Trim(Path.DirectorySeparatorChar);
|
||||
|
||||
return Path.Combine(_appFolderInfo.GetAppDataPath(), path);
|
||||
}
|
||||
var resourcePath = 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)
|
||||
if (!_diskProvider.FileExists(resourcePath) || _diskProvider.GetFileSize(resourcePath) == 0)
|
||||
{
|
||||
var baseResourceUrl = RegexResizedImage.Replace(resourceUrl, ".jpg$1");
|
||||
if (baseResourceUrl != resourceUrl)
|
||||
var baseResourcePath = RegexResizedImage.Replace(resourcePath, ".jpg$1");
|
||||
if (baseResourcePath != resourcePath)
|
||||
{
|
||||
result = base.GetResponse(baseResourceUrl);
|
||||
return baseResourcePath;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return resourcePath;
|
||||
}
|
||||
|
||||
public override bool CanHandle(string resourceUrl)
|
||||
|
||||
@@ -29,7 +29,7 @@ namespace NzbDrone.Api.MediaCovers
|
||||
{
|
||||
var filePath = Path.Combine(_appFolderInfo.GetAppDataPath(), "MediaCover", seriesId.ToString(), filename);
|
||||
|
||||
if (!_diskProvider.FileExists(filePath))
|
||||
if (!_diskProvider.FileExists(filePath) || _diskProvider.GetFileSize(filePath) == 0)
|
||||
{
|
||||
// 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.
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using NLog;
|
||||
using NzbDrone.Common.EnvironmentInfo;
|
||||
|
||||
namespace NzbDrone.Common.Instrumentation
|
||||
{
|
||||
@@ -9,18 +10,22 @@ namespace NzbDrone.Common.Instrumentation
|
||||
private static readonly Logger Logger = NzbDroneLogger.GetLogger(typeof(GlobalExceptionHandlers));
|
||||
public static void Register()
|
||||
{
|
||||
AppDomain.CurrentDomain.UnhandledException += ((s, e) => AppDomainException(e.ExceptionObject as Exception));
|
||||
TaskScheduler.UnobservedTaskException += ((s, e) => TaskException(e.Exception));
|
||||
AppDomain.CurrentDomain.UnhandledException += HandleAppDomainException;
|
||||
TaskScheduler.UnobservedTaskException += HandleTaskException;
|
||||
}
|
||||
|
||||
private static void TaskException(Exception exception)
|
||||
private static void HandleTaskException(object sender, UnobservedTaskExceptionEventArgs e)
|
||||
{
|
||||
var exception = e.Exception;
|
||||
|
||||
Console.WriteLine("Task Error: {0}", exception);
|
||||
Logger.Error("Task Error: " + exception.Message, exception);
|
||||
}
|
||||
|
||||
private static void AppDomainException(Exception exception)
|
||||
private static void HandleAppDomainException(object sender, UnhandledExceptionEventArgs e)
|
||||
{
|
||||
var exception = e.ExceptionObject as Exception;
|
||||
|
||||
if (exception == null) return;
|
||||
|
||||
if (exception is NullReferenceException &&
|
||||
@@ -30,13 +35,18 @@ namespace NzbDrone.Common.Instrumentation
|
||||
return;
|
||||
}
|
||||
|
||||
if (OsInfo.IsMonoRuntime)
|
||||
{
|
||||
if (exception is TypeInitializationException && exception.InnerException is DllNotFoundException ||
|
||||
exception is DllNotFoundException)
|
||||
{
|
||||
Logger.DebugException("Minor Fail: " + exception.Message, exception);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("EPIC FAIL: {0}", exception);
|
||||
Logger.FatalException("EPIC FAIL: " + exception.Message, exception);
|
||||
|
||||
if (exception.InnerException != null)
|
||||
{
|
||||
AppDomainException(exception.InnerException);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,14 @@ namespace NzbDrone.Core.Test.MediaCoverTests
|
||||
Mocker.GetMock<IDiskProvider>()
|
||||
.Setup(v => v.OpenWriteStream(It.IsAny<string>()))
|
||||
.Returns<string>(s => new FileStream(s, FileMode.Create));
|
||||
|
||||
Mocker.GetMock<IDiskProvider>()
|
||||
.Setup(v => v.FileExists(It.IsAny<string>()))
|
||||
.Returns<string>(s => File.Exists(s));
|
||||
|
||||
Mocker.GetMock<IDiskProvider>()
|
||||
.Setup(v => v.DeleteFile(It.IsAny<string>()))
|
||||
.Callback<string>(s => File.Delete(s));
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -46,5 +54,18 @@ namespace NzbDrone.Core.Test.MediaCoverTests
|
||||
image.Height.Should().Be(170);
|
||||
image.Width.Should().Be(170);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_delete_file_if_failed()
|
||||
{
|
||||
var mainFile = Path.Combine(TempFolder, "junk.png");
|
||||
var resizedFile = Path.Combine(TempFolder, "junk-170.png");
|
||||
|
||||
File.WriteAllText(mainFile, "Just some junk data that should make it throw an Exception.");
|
||||
|
||||
Assert.Throws(Is.InstanceOf<Exception>(), () => Subject.Resize(mainFile, resizedFile, 170));
|
||||
|
||||
File.Exists(resizedFile).Should().BeFalse();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ using NzbDrone.Core.MediaCover;
|
||||
using NzbDrone.Core.Test.Framework;
|
||||
using NzbDrone.Core.Tv;
|
||||
using NzbDrone.Core.Tv.Events;
|
||||
using NzbDrone.Test.Common;
|
||||
|
||||
namespace NzbDrone.Core.Test.MediaCoverTests
|
||||
{
|
||||
@@ -110,10 +111,56 @@ namespace NzbDrone.Core.Test.MediaCoverTests
|
||||
.Setup(v => v.FileExists(It.IsAny<string>()))
|
||||
.Returns(true);
|
||||
|
||||
Mocker.GetMock<IDiskProvider>()
|
||||
.Setup(v => v.GetFileSize(It.IsAny<string>()))
|
||||
.Returns(1000);
|
||||
|
||||
Subject.HandleAsync(new SeriesUpdatedEvent(_series));
|
||||
|
||||
Mocker.GetMock<IImageResizer>()
|
||||
.Verify(v => v.Resize(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>()), Times.Never());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_resize_covers_if_existing_is_empty()
|
||||
{
|
||||
Mocker.GetMock<ICoverExistsSpecification>()
|
||||
.Setup(v => v.AlreadyExists(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(true);
|
||||
|
||||
Mocker.GetMock<IDiskProvider>()
|
||||
.Setup(v => v.FileExists(It.IsAny<string>()))
|
||||
.Returns(true);
|
||||
|
||||
Mocker.GetMock<IDiskProvider>()
|
||||
.Setup(v => v.GetFileSize(It.IsAny<string>()))
|
||||
.Returns(0);
|
||||
|
||||
Subject.HandleAsync(new SeriesUpdatedEvent(_series));
|
||||
|
||||
Mocker.GetMock<IImageResizer>()
|
||||
.Verify(v => v.Resize(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>()), Times.Exactly(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_log_error_if_resize_failed()
|
||||
{
|
||||
Mocker.GetMock<ICoverExistsSpecification>()
|
||||
.Setup(v => v.AlreadyExists(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(true);
|
||||
|
||||
Mocker.GetMock<IDiskProvider>()
|
||||
.Setup(v => v.FileExists(It.IsAny<string>()))
|
||||
.Returns(false);
|
||||
|
||||
Mocker.GetMock<IImageResizer>()
|
||||
.Setup(v => v.Resize(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>()))
|
||||
.Throws<ApplicationException>();
|
||||
|
||||
Subject.HandleAsync(new SeriesUpdatedEvent(_series));
|
||||
|
||||
Mocker.GetMock<IImageResizer>()
|
||||
.Verify(v => v.Resize(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>()), Times.Exactly(2));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,18 +22,29 @@ namespace NzbDrone.Core.MediaCover
|
||||
|
||||
public void Resize(string source, string destination, int height)
|
||||
{
|
||||
using (var sourceStream = _diskProvider.OpenReadStream(source))
|
||||
try
|
||||
{
|
||||
using (var outputStream = _diskProvider.OpenWriteStream(destination))
|
||||
using (var sourceStream = _diskProvider.OpenReadStream(source))
|
||||
{
|
||||
var settings = new Instructions();
|
||||
settings.Height = height;
|
||||
using (var outputStream = _diskProvider.OpenWriteStream(destination))
|
||||
{
|
||||
var settings = new Instructions();
|
||||
settings.Height = height;
|
||||
|
||||
var job = new ImageJob(sourceStream, outputStream, settings);
|
||||
var job = new ImageJob(sourceStream, outputStream, settings);
|
||||
|
||||
ImageBuilder.Current.Build(job);
|
||||
ImageBuilder.Current.Build(job);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (_diskProvider.FileExists(destination))
|
||||
{
|
||||
_diskProvider.DeleteFile(destination);
|
||||
}
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,16 +88,13 @@ namespace NzbDrone.Core.MediaCover
|
||||
foreach (var cover in series.Images)
|
||||
{
|
||||
var fileName = GetCoverPath(series.Id, cover.CoverType);
|
||||
var alreadyExists = false;
|
||||
try
|
||||
{
|
||||
if (!_coverExistsSpecification.AlreadyExists(cover.Url, fileName))
|
||||
alreadyExists = _coverExistsSpecification.AlreadyExists(cover.Url, fileName);
|
||||
if (!alreadyExists)
|
||||
{
|
||||
DownloadCover(series, cover);
|
||||
EnsureResizedCovers(series, cover, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
EnsureResizedCovers(series, cover, false);
|
||||
}
|
||||
}
|
||||
catch (WebException e)
|
||||
@@ -108,6 +105,8 @@ namespace NzbDrone.Core.MediaCover
|
||||
{
|
||||
_logger.ErrorException("Couldn't download media cover for " + series, e);
|
||||
}
|
||||
|
||||
EnsureResizedCovers(series, cover, !alreadyExists);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,11 +147,18 @@ namespace NzbDrone.Core.MediaCover
|
||||
var mainFileName = GetCoverPath(series.Id, cover.CoverType);
|
||||
var resizeFileName = GetCoverPath(series.Id, cover.CoverType, height);
|
||||
|
||||
if (forceResize || !_diskProvider.FileExists(resizeFileName))
|
||||
if (forceResize || !_diskProvider.FileExists(resizeFileName) || _diskProvider.GetFileSize(resizeFileName) == 0)
|
||||
{
|
||||
_logger.Debug("Resizing {0}-{1} for {2}", cover.CoverType, height, series);
|
||||
|
||||
_resizer.Resize(mainFileName, resizeFileName, height);
|
||||
try
|
||||
{
|
||||
_resizer.Resize(mainFileName, resizeFileName, height);
|
||||
}
|
||||
catch
|
||||
{
|
||||
_logger.Debug("Couldn't resize media cover {0}-{1} for {2}, using full size image instead.", cover.CoverType, height, series);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user