Skip to content

Commit

Permalink
Fixed: Don't force images to JPG and fix Kodi album art filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
ta264 committed Aug 2, 2019
1 parent 8b860bc commit 8d780f4
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 32 deletions.
39 changes: 27 additions & 12 deletions src/NzbDrone.Core.Test/MediaCoverTests/MediaCoverServiceFixture.cs
Expand Up @@ -41,12 +41,17 @@ public void Setup()
Mocker.GetMock<IHttpClient>().Setup(c => c.Head(It.IsAny<HttpRequest>())).Returns(_httpResponse);
}

[Test]
public void should_convert_cover_urls_to_local()
[TestCase(".png")]
[TestCase(".jpg")]
public void should_convert_cover_urls_to_local(string extension)
{
var covers = new List<MediaCover.MediaCover>
{
new MediaCover.MediaCover {CoverType = MediaCoverTypes.Banner}
new MediaCover.MediaCover
{
Url = "http://dummy.com/test" + extension,
CoverType = MediaCoverTypes.Banner
}
};

Mocker.GetMock<IDiskProvider>().Setup(c => c.FileGetLastWrite(It.IsAny<string>()))
Expand All @@ -58,15 +63,20 @@ public void should_convert_cover_urls_to_local()
Subject.ConvertToLocalUrls(12, MediaCoverEntity.Artist, covers);


covers.Single().Url.Should().Be("/MediaCover/12/banner.jpg?lastWrite=1234");
covers.Single().Url.Should().Be("/MediaCover/12/banner" + extension + "?lastWrite=1234");
}

[Test]
public void should_convert_album_cover_urls_to_local()
[TestCase(".png")]
[TestCase(".jpg")]
public void should_convert_album_cover_urls_to_local(string extension)
{
var covers = new List<MediaCover.MediaCover>
{
new MediaCover.MediaCover {CoverType = MediaCoverTypes.Disc}
new MediaCover.MediaCover
{
Url = "http://dummy.com/test" + extension,
CoverType = MediaCoverTypes.Disc
}
};

Mocker.GetMock<IDiskProvider>().Setup(c => c.FileGetLastWrite(It.IsAny<string>()))
Expand All @@ -78,22 +88,27 @@ public void should_convert_album_cover_urls_to_local()
Subject.ConvertToLocalUrls(6, MediaCoverEntity.Album, covers);


covers.Single().Url.Should().Be("/MediaCover/Albums/6/disc.jpg?lastWrite=1234");
covers.Single().Url.Should().Be("/MediaCover/Albums/6/disc" + extension + "?lastWrite=1234");
}

[Test]
public void should_convert_media_urls_to_local_without_time_if_file_doesnt_exist()
[TestCase(".png")]
[TestCase(".jpg")]
public void should_convert_media_urls_to_local_without_time_if_file_doesnt_exist(string extension)
{
var covers = new List<MediaCover.MediaCover>
{
new MediaCover.MediaCover {CoverType = MediaCoverTypes.Banner}
new MediaCover.MediaCover
{
Url = "http://dummy.com/test" + extension,
CoverType = MediaCoverTypes.Banner
}
};


Subject.ConvertToLocalUrls(12, MediaCoverEntity.Artist, covers);


covers.Single().Url.Should().Be("/MediaCover/12/banner.jpg");
covers.Single().Url.Should().Be("/MediaCover/12/banner" + extension);
}

[Test]
Expand Down
Expand Up @@ -165,7 +165,7 @@ public override List<ImageFileResult> ArtistImages(Artist artist)
return new List<ImageFileResult>(); ;
}

var source = _mediaCoverService.GetCoverPath(artist.Id, MediaCoverEntity.Artist, image.CoverType);
var source = _mediaCoverService.GetCoverPath(artist.Id, MediaCoverEntity.Artist, image.CoverType, image.Extension);
var destination = Path.GetFileName(artist.Path) + Path.GetExtension(source);

return new List<ImageFileResult>{ new ImageFileResult(destination, source) };
Expand Down
22 changes: 18 additions & 4 deletions src/NzbDrone.Core/Extras/Metadata/Consumers/Xbmc/XbmcMetadata.cs
Expand Up @@ -205,11 +205,11 @@ private IEnumerable<ImageFileResult> ProcessArtistImages(Artist artist)
{
foreach (var image in artist.Metadata.Value.Images)
{
var source = _mediaCoverService.GetCoverPath(artist.Id, MediaCoverEntity.Artist, image.CoverType);
var destination = image.CoverType.ToString().ToLowerInvariant() + Path.GetExtension(image.Url);
var source = _mediaCoverService.GetCoverPath(artist.Id, MediaCoverEntity.Artist, image.CoverType, image.Extension);
var destination = image.CoverType.ToString().ToLowerInvariant() + image.Extension;
if (image.CoverType == MediaCoverTypes.Poster)
{
destination = "folder" + Path.GetExtension(image.Url);
destination = "folder" + image.Extension;
}

yield return new ImageFileResult(destination, source);
Expand All @@ -222,7 +222,21 @@ private IEnumerable<ImageFileResult> ProcessAlbumImages(Artist artist, Album alb
{
// TODO: Make Source fallback to URL if local does not exist
// var source = _mediaCoverService.GetCoverPath(album.ArtistId, image.CoverType, null, album.Id);
var destination = Path.Combine(albumPath, image.CoverType.ToString().ToLowerInvariant() + Path.GetExtension(image.Url));
string filename;

switch(image.CoverType)
{
case MediaCoverTypes.Cover:
filename = "folder";
break;
case MediaCoverTypes.Disc:
filename = "discart";
break;
default:
continue;
}

var destination = Path.Combine(albumPath, filename + image.Extension);

yield return new ImageFileResult(destination, image.Url);
}
Expand Down
2 changes: 2 additions & 0 deletions src/NzbDrone.Core/MediaCover/MediaCover.cs
@@ -1,3 +1,4 @@
using System.IO;
using NzbDrone.Core.Datastore;

namespace NzbDrone.Core.MediaCover
Expand Down Expand Up @@ -26,6 +27,7 @@ public class MediaCover : IEmbeddedDocument
{
public MediaCoverTypes CoverType { get; set; }
public string Url { get; set; }
public string Extension => Path.GetExtension(Url);

public MediaCover()
{
Expand Down
30 changes: 15 additions & 15 deletions src/NzbDrone.Core/MediaCover/MediaCoverService.cs
Expand Up @@ -18,7 +18,7 @@ namespace NzbDrone.Core.MediaCover
public interface IMapCoversToLocal
{
void ConvertToLocalUrls(int entityId, MediaCoverEntity coverEntity, IEnumerable<MediaCover> covers);
string GetCoverPath(int entityId, MediaCoverEntity coverEntity, MediaCoverTypes mediaCoverTypes, int? height = null);
string GetCoverPath(int entityId, MediaCoverEntity coverEntity, MediaCoverTypes mediaCoverTypes, string extension, int? height = null);
}

public class MediaCoverService :
Expand Down Expand Up @@ -59,33 +59,33 @@ public class MediaCoverService :
_coverRootFolder = appFolderInfo.GetMediaCoverPath();
}

public string GetCoverPath(int entityId, MediaCoverEntity coverEntity, MediaCoverTypes coverTypes, int? height = null)
public string GetCoverPath(int entityId, MediaCoverEntity coverEntity, MediaCoverTypes coverTypes, string extension, int? height = null)
{
var heightSuffix = height.HasValue ? "-" + height.ToString() : "";

if (coverEntity == MediaCoverEntity.Album)
{
return Path.Combine(GetAlbumCoverPath(entityId), coverTypes.ToString().ToLower() + heightSuffix + ".jpg");
return Path.Combine(GetAlbumCoverPath(entityId), coverTypes.ToString().ToLower() + heightSuffix + extension);
}
else
{
return Path.Combine(GetArtistCoverPath(entityId), coverTypes.ToString().ToLower() + heightSuffix + ".jpg");
return Path.Combine(GetArtistCoverPath(entityId), coverTypes.ToString().ToLower() + heightSuffix + extension);
}
}

public void ConvertToLocalUrls(int entityId, MediaCoverEntity coverEntity, IEnumerable<MediaCover> covers)
{
foreach (var mediaCover in covers)
{
var filePath = GetCoverPath(entityId, coverEntity, mediaCover.CoverType, null);
var filePath = GetCoverPath(entityId, coverEntity, mediaCover.CoverType, mediaCover.Extension, null);

if (coverEntity == MediaCoverEntity.Album)
{
mediaCover.Url = _configFileProvider.UrlBase + @"/MediaCover/Albums/" + entityId + "/" + mediaCover.CoverType.ToString().ToLower() + ".jpg";
mediaCover.Url = _configFileProvider.UrlBase + @"/MediaCover/Albums/" + entityId + "/" + mediaCover.CoverType.ToString().ToLower() + mediaCover.Extension;
}
else
{
mediaCover.Url = _configFileProvider.UrlBase + @"/MediaCover/" + entityId + "/" + mediaCover.CoverType.ToString().ToLower() + ".jpg";
mediaCover.Url = _configFileProvider.UrlBase + @"/MediaCover/" + entityId + "/" + mediaCover.CoverType.ToString().ToLower() + mediaCover.Extension;
}

if (_diskProvider.FileExists(filePath))
Expand All @@ -110,7 +110,7 @@ private void EnsureArtistCovers(Artist artist)
{
foreach (var cover in artist.Metadata.Value.Images)
{
var fileName = GetCoverPath(artist.Id, MediaCoverEntity.Artist, cover.CoverType);
var fileName = GetCoverPath(artist.Id, MediaCoverEntity.Artist, cover.CoverType, cover.Extension);
var alreadyExists = false;

try
Expand Down Expand Up @@ -141,7 +141,7 @@ private void EnsureAlbumCovers(Album album)
{
foreach (var cover in album.Images.Where(e => e.CoverType == MediaCoverTypes.Cover))
{
var fileName = GetCoverPath(album.Id, MediaCoverEntity.Album, cover.CoverType, null);
var fileName = GetCoverPath(album.Id, MediaCoverEntity.Album, cover.CoverType, cover.Extension, null);
var alreadyExists = false;
try
{
Expand Down Expand Up @@ -169,7 +169,7 @@ private void EnsureAlbumCovers(Album album)

private void DownloadCover(Artist artist, MediaCover cover, DateTime lastModified)
{
var fileName = GetCoverPath(artist.Id, MediaCoverEntity.Artist, cover.CoverType);
var fileName = GetCoverPath(artist.Id, MediaCoverEntity.Artist, cover.CoverType, cover.Extension);

_logger.Info("Downloading {0} for {1} {2}", cover.CoverType, artist, cover.Url);
_httpClient.DownloadFile(cover.Url, fileName);
Expand All @@ -186,7 +186,7 @@ private void DownloadCover(Artist artist, MediaCover cover, DateTime lastModifie

private void DownloadAlbumCover(Album album, MediaCover cover, DateTime lastModified)
{
var fileName = GetCoverPath(album.Id, MediaCoverEntity.Album, cover.CoverType, null);
var fileName = GetCoverPath(album.Id, MediaCoverEntity.Album, cover.CoverType, cover.Extension, null);

_logger.Info("Downloading {0} for {1} {2}", cover.CoverType, album, cover.Url);
_httpClient.DownloadFile(cover.Url, fileName);
Expand All @@ -207,8 +207,8 @@ private void EnsureResizedCovers(Artist artist, MediaCover cover, bool forceResi

foreach (var height in heights)
{
var mainFileName = GetCoverPath(artist.Id, MediaCoverEntity.Artist, cover.CoverType);
var resizeFileName = GetCoverPath(artist.Id, MediaCoverEntity.Artist, cover.CoverType, height);
var mainFileName = GetCoverPath(artist.Id, MediaCoverEntity.Artist, cover.CoverType, cover.Extension);
var resizeFileName = GetCoverPath(artist.Id, MediaCoverEntity.Artist, cover.CoverType, cover.Extension, height);

if (forceResize || !_diskProvider.FileExists(resizeFileName) || _diskProvider.GetFileSize(resizeFileName) == 0)
{
Expand All @@ -232,8 +232,8 @@ private void EnsureResizedAlbumCovers(Album album, MediaCover cover, bool forceR

foreach (var height in heights)
{
var mainFileName = GetCoverPath(album.Id, MediaCoverEntity.Album, cover.CoverType, null);
var resizeFileName = GetCoverPath(album.Id, MediaCoverEntity.Album, cover.CoverType, height);
var mainFileName = GetCoverPath(album.Id, MediaCoverEntity.Album, cover.CoverType, cover.Extension, null);
var resizeFileName = GetCoverPath(album.Id, MediaCoverEntity.Album, cover.CoverType, cover.Extension, height);

if (forceResize || !_diskProvider.FileExists(resizeFileName) || _diskProvider.GetFileSize(resizeFileName) == 0)
{
Expand Down

0 comments on commit 8d780f4

Please sign in to comment.