mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-20 21:43:33 -07:00
Fixed: ImageResizer Tweaks
Co-Authored-By: taloth <taloth@users.noreply.github.com> Co-Authored-By: ta264 <ta264@users.noreply.github.com>
This commit is contained in:
parent
ae6db26a77
commit
3c423871d3
4 changed files with 58 additions and 15 deletions
|
@ -4,7 +4,9 @@ using FluentAssertions;
|
||||||
using Moq;
|
using Moq;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
using NzbDrone.Common.Disk;
|
using NzbDrone.Common.Disk;
|
||||||
|
using NzbDrone.Common.EnvironmentInfo;
|
||||||
using NzbDrone.Core.Test.Framework;
|
using NzbDrone.Core.Test.Framework;
|
||||||
|
using SixLabors.ImageSharp;
|
||||||
|
|
||||||
namespace NzbDrone.Core.Test.MediaCoverTests
|
namespace NzbDrone.Core.Test.MediaCoverTests
|
||||||
{
|
{
|
||||||
|
@ -14,13 +16,10 @@ namespace NzbDrone.Core.Test.MediaCoverTests
|
||||||
[SetUp]
|
[SetUp]
|
||||||
public void SetUp()
|
public void SetUp()
|
||||||
{
|
{
|
||||||
Mocker.GetMock<IDiskProvider>()
|
if (PlatformInfo.IsMono && PlatformInfo.GetVersion() < new Version(5, 8))
|
||||||
.Setup(v => v.OpenReadStream(It.IsAny<string>()))
|
{
|
||||||
.Returns<string>(s => new FileStream(s, FileMode.Open));
|
Assert.Inconclusive("Not supported on Mono < 5.8");
|
||||||
|
}
|
||||||
Mocker.GetMock<IDiskProvider>()
|
|
||||||
.Setup(v => v.OpenWriteStream(It.IsAny<string>()))
|
|
||||||
.Returns<string>(s => new FileStream(s, FileMode.Create));
|
|
||||||
|
|
||||||
Mocker.GetMock<IDiskProvider>()
|
Mocker.GetMock<IDiskProvider>()
|
||||||
.Setup(v => v.FileExists(It.IsAny<string>()))
|
.Setup(v => v.FileExists(It.IsAny<string>()))
|
||||||
|
@ -29,6 +28,8 @@ namespace NzbDrone.Core.Test.MediaCoverTests
|
||||||
Mocker.GetMock<IDiskProvider>()
|
Mocker.GetMock<IDiskProvider>()
|
||||||
.Setup(v => v.DeleteFile(It.IsAny<string>()))
|
.Setup(v => v.DeleteFile(It.IsAny<string>()))
|
||||||
.Callback<string>(s => File.Delete(s));
|
.Callback<string>(s => File.Delete(s));
|
||||||
|
|
||||||
|
Mocker.SetConstant<IPlatformInfo>(Mocker.Resolve<PlatformInfo>());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
@ -45,10 +46,12 @@ namespace NzbDrone.Core.Test.MediaCoverTests
|
||||||
fileInfo.Exists.Should().BeTrue();
|
fileInfo.Exists.Should().BeTrue();
|
||||||
fileInfo.Length.Should().BeInRange(1000, 30000);
|
fileInfo.Length.Should().BeInRange(1000, 30000);
|
||||||
|
|
||||||
var image = System.Drawing.Image.FromFile(resizedFile);
|
using (var image = Image.Load(resizedFile))
|
||||||
|
{
|
||||||
image.Height.Should().Be(170);
|
image.Height.Should().Be(170);
|
||||||
image.Width.Should().Be(170);
|
image.Width.Should().Be(170);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void should_delete_file_if_failed()
|
public void should_delete_file_if_failed()
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
using System;
|
using System;
|
||||||
using NzbDrone.Common.Disk;
|
using NzbDrone.Common.Disk;
|
||||||
|
using NzbDrone.Common.EnvironmentInfo;
|
||||||
using SixLabors.ImageSharp;
|
using SixLabors.ImageSharp;
|
||||||
|
using SixLabors.ImageSharp.Formats.Jpeg;
|
||||||
using SixLabors.ImageSharp.Processing;
|
using SixLabors.ImageSharp.Processing;
|
||||||
using SixLabors.Memory;
|
using SixLabors.Memory;
|
||||||
|
|
||||||
|
@ -14,23 +16,39 @@ namespace NzbDrone.Core.MediaCover
|
||||||
public class ImageResizer : IImageResizer
|
public class ImageResizer : IImageResizer
|
||||||
{
|
{
|
||||||
private readonly IDiskProvider _diskProvider;
|
private readonly IDiskProvider _diskProvider;
|
||||||
|
private readonly bool _enabled;
|
||||||
|
|
||||||
public ImageResizer(IDiskProvider diskProvider)
|
public ImageResizer(IDiskProvider diskProvider, IPlatformInfo platformInfo)
|
||||||
{
|
{
|
||||||
_diskProvider = diskProvider;
|
_diskProvider = diskProvider;
|
||||||
|
|
||||||
|
// Random segfaults on mono 5.0 and 5.4
|
||||||
|
if (PlatformInfo.IsMono && platformInfo.Version < new System.Version(5, 8))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_enabled = true;
|
||||||
|
|
||||||
// More conservative memory allocation
|
// More conservative memory allocation
|
||||||
SixLabors.ImageSharp.Configuration.Default.MemoryAllocator = new SimpleGcMemoryAllocator();
|
SixLabors.ImageSharp.Configuration.Default.MemoryAllocator = new SimpleGcMemoryAllocator();
|
||||||
|
|
||||||
|
// Thumbnails don't need super high quality
|
||||||
|
SixLabors.ImageSharp.Configuration.Default.ImageFormatsManager.SetEncoder(JpegFormat.Instance, new JpegEncoder
|
||||||
|
{
|
||||||
|
Quality = 92
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Resize(string source, string destination, int height)
|
public void Resize(string source, string destination, int height)
|
||||||
{
|
{
|
||||||
|
if (!_enabled) return;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (var image = Image.Load(source))
|
using (var image = Image.Load(source))
|
||||||
{
|
{
|
||||||
var width = (int)Math.Floor((double)image.Width * (double)height / (double)image.Height);
|
image.Mutate(x => x.Resize(0, height));
|
||||||
image.Mutate(x => x.Resize(width, height));
|
|
||||||
image.Save(destination);
|
image.Save(destination);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Threading;
|
||||||
using NLog;
|
using NLog;
|
||||||
using NzbDrone.Common.Disk;
|
using NzbDrone.Common.Disk;
|
||||||
using NzbDrone.Common.EnvironmentInfo;
|
using NzbDrone.Common.EnvironmentInfo;
|
||||||
|
@ -38,6 +39,11 @@ namespace NzbDrone.Core.MediaCover
|
||||||
|
|
||||||
private readonly string _coverRootFolder;
|
private readonly string _coverRootFolder;
|
||||||
|
|
||||||
|
// ImageSharp is slow on ARM (no hardware acceleration on mono yet)
|
||||||
|
// So limit the number of concurrent resizing tasks
|
||||||
|
private static SemaphoreSlim _semaphore = new SemaphoreSlim((int)Math.Ceiling(Environment.ProcessorCount / 2.0));
|
||||||
|
|
||||||
|
|
||||||
public MediaCoverService(IImageResizer resizer,
|
public MediaCoverService(IImageResizer resizer,
|
||||||
IAlbumService albumService,
|
IAlbumService albumService,
|
||||||
IHttpClient httpClient,
|
IHttpClient httpClient,
|
||||||
|
@ -109,6 +115,8 @@ namespace NzbDrone.Core.MediaCover
|
||||||
|
|
||||||
private void EnsureArtistCovers(Artist artist)
|
private void EnsureArtistCovers(Artist artist)
|
||||||
{
|
{
|
||||||
|
var toResize = new List<Tuple<MediaCover, bool>>();
|
||||||
|
|
||||||
foreach (var cover in artist.Metadata.Value.Images)
|
foreach (var cover in artist.Metadata.Value.Images)
|
||||||
{
|
{
|
||||||
var fileName = GetCoverPath(artist.Id, MediaCoverEntity.Artist, cover.CoverType, cover.Extension);
|
var fileName = GetCoverPath(artist.Id, MediaCoverEntity.Artist, cover.CoverType, cover.Extension);
|
||||||
|
@ -134,7 +142,21 @@ namespace NzbDrone.Core.MediaCover
|
||||||
_logger.Error(e, "Couldn't download media cover for {0}", artist);
|
_logger.Error(e, "Couldn't download media cover for {0}", artist);
|
||||||
}
|
}
|
||||||
|
|
||||||
EnsureResizedCovers(artist, cover, !alreadyExists);
|
toResize.Add(Tuple.Create(cover, alreadyExists));
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_semaphore.Wait();
|
||||||
|
|
||||||
|
foreach (var tuple in toResize)
|
||||||
|
{
|
||||||
|
EnsureResizedCovers(artist, tuple.Item1, !tuple.Item2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_semaphore.Release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -76,7 +76,7 @@ namespace NzbDrone.Test.Common
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine("Waiting for Lidarr to start. Response Status : {0} [{1}] {2}", statusCall.ResponseStatus, statusCall.StatusDescription, statusCall.ErrorException);
|
Console.WriteLine("Waiting for Lidarr to start. Response Status : {0} [{1}] {2}", statusCall.ResponseStatus, statusCall.StatusDescription, statusCall.ErrorException.Message);
|
||||||
|
|
||||||
Thread.Sleep(500);
|
Thread.Sleep(500);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue