From 577d665192ab79cdd2f725ca0be0b9948ff5eed3 Mon Sep 17 00:00:00 2001 From: David Ullmer Date: Fri, 13 Aug 2021 20:16:05 +0200 Subject: [PATCH 1/3] Move thumb tag parsing to separate method --- .../Parsers/BaseNfoParser.cs | 110 +++++++++--------- 1 file changed, 57 insertions(+), 53 deletions(-) diff --git a/MediaBrowser.XbmcMetadata/Parsers/BaseNfoParser.cs b/MediaBrowser.XbmcMetadata/Parsers/BaseNfoParser.cs index 2c86f9242..242a7132b 100644 --- a/MediaBrowser.XbmcMetadata/Parsers/BaseNfoParser.cs +++ b/MediaBrowser.XbmcMetadata/Parsers/BaseNfoParser.cs @@ -783,59 +783,7 @@ namespace MediaBrowser.XbmcMetadata.Parsers case "thumb": { - var artType = reader.GetAttribute("aspect"); - var val = reader.ReadElementContentAsString(); - - // skip: - // - empty aspect tag - // - empty uri - // - tag containing '.' because we can't set images for seasons, episodes or movie sets within series or movies - if (string.IsNullOrEmpty(artType) || string.IsNullOrEmpty(val) || artType.Contains('.', StringComparison.Ordinal)) - { - break; - } - - ImageType imageType = GetImageType(artType); - - if (!Uri.TryCreate(val, UriKind.Absolute, out var uri)) - { - Logger.LogError("Image location {Path} specified in nfo file for {ItemName} is not a valid URL or file path.", val, item.Name); - break; - } - - if (uri.IsFile) - { - // only allow one item of each type - if (itemResult.Images.Any(x => x.Type == imageType)) - { - break; - } - - var fileSystemMetadata = _directoryService.GetFile(val); - // non existing file returns null - if (fileSystemMetadata == null || !fileSystemMetadata.Exists) - { - Logger.LogWarning("Artwork file {Path} specified in nfo file for {ItemName} does not exist.", uri, item.Name); - break; - } - - itemResult.Images.Add(new LocalImageInfo() - { - FileInfo = fileSystemMetadata, - Type = imageType - }); - } - else - { - // only allow one item of each type - if (itemResult.RemoteImages.Any(x => x.type == imageType)) - { - break; - } - - itemResult.RemoteImages.Add((uri.ToString(), imageType)); - } - + FetchThumbNode(reader, itemResult); break; } @@ -858,6 +806,62 @@ namespace MediaBrowser.XbmcMetadata.Parsers } } + private void FetchThumbNode(XmlReader reader, MetadataResult itemResult) + { + var artType = reader.GetAttribute("aspect"); + var val = reader.ReadElementContentAsString(); + + // skip: + // - empty aspect tag + // - empty uri + // - tag containing '.' because we can't set images for seasons, episodes or movie sets within series or movies + if (string.IsNullOrEmpty(artType) || string.IsNullOrEmpty(val) || artType.Contains('.', StringComparison.Ordinal)) + { + return; + } + + ImageType imageType = GetImageType(artType); + + if (!Uri.TryCreate(val, UriKind.Absolute, out var uri)) + { + Logger.LogError("Image location {Path} specified in nfo file for {ItemName} is not a valid URL or file path.", val, itemResult.Item.Name); + return; + } + + if (uri.IsFile) + { + // only allow one item of each type + if (itemResult.Images.Any(x => x.Type == imageType)) + { + return; + } + + var fileSystemMetadata = _directoryService.GetFile(val); + // non existing file returns null + if (fileSystemMetadata == null || !fileSystemMetadata.Exists) + { + Logger.LogWarning("Artwork file {Path} specified in nfo file for {ItemName} does not exist.", uri, itemResult.Item.Name); + return; + } + + itemResult.Images.Add(new LocalImageInfo() + { + FileInfo = fileSystemMetadata, + Type = imageType + }); + } + else + { + // only allow one item of each type + if (itemResult.RemoteImages.Any(x => x.type == imageType)) + { + return; + } + + itemResult.RemoteImages.Add((uri.ToString(), imageType)); + } + } + private void FetchFromFileInfoNode(XmlReader reader, T item) { reader.MoveToContent(); From 12e58840eb6d7045e6b706580e057f4fb910fc3d Mon Sep 17 00:00:00 2001 From: David Ullmer Date: Fri, 13 Aug 2021 20:33:53 +0200 Subject: [PATCH 2/3] Modify FetchThumbNode method to read children of fanart tag --- .../Parsers/BaseNfoParser.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/MediaBrowser.XbmcMetadata/Parsers/BaseNfoParser.cs b/MediaBrowser.XbmcMetadata/Parsers/BaseNfoParser.cs index 242a7132b..0edb7f43c 100644 --- a/MediaBrowser.XbmcMetadata/Parsers/BaseNfoParser.cs +++ b/MediaBrowser.XbmcMetadata/Parsers/BaseNfoParser.cs @@ -787,6 +787,14 @@ namespace MediaBrowser.XbmcMetadata.Parsers break; } + case "fanart": + { + var subtree = reader.ReadSubtree(); + subtree.ReadToDescendant("thumb"); + FetchThumbNode(subtree, itemResult); + break; + } + default: string readerName = reader.Name; if (_validProviderIds.TryGetValue(readerName, out string? providerIdValue)) @@ -811,11 +819,17 @@ namespace MediaBrowser.XbmcMetadata.Parsers var artType = reader.GetAttribute("aspect"); var val = reader.ReadElementContentAsString(); + // artType is null if the thumb node is a child of the fanart tag + // -> set image type to fanart + if (string.IsNullOrWhiteSpace(artType)) + { + artType = "fanart"; + } + // skip: - // - empty aspect tag // - empty uri // - tag containing '.' because we can't set images for seasons, episodes or movie sets within series or movies - if (string.IsNullOrEmpty(artType) || string.IsNullOrEmpty(val) || artType.Contains('.', StringComparison.Ordinal)) + if (string.IsNullOrEmpty(val) || artType.Contains('.', StringComparison.Ordinal)) { return; } From bf441e49b9128024eec57de1122960ad4cde90f7 Mon Sep 17 00:00:00 2001 From: David Ullmer Date: Fri, 13 Aug 2021 20:36:14 +0200 Subject: [PATCH 3/3] Add test for fanart tag --- .../Parsers/MovieNfoParserTests.cs | 14 ++++++++ .../Test Data/Fanart.nfo | 33 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 tests/Jellyfin.XbmcMetadata.Tests/Test Data/Fanart.nfo diff --git a/tests/Jellyfin.XbmcMetadata.Tests/Parsers/MovieNfoParserTests.cs b/tests/Jellyfin.XbmcMetadata.Tests/Parsers/MovieNfoParserTests.cs index cbcce73eb..ef3ca15d5 100644 --- a/tests/Jellyfin.XbmcMetadata.Tests/Parsers/MovieNfoParserTests.cs +++ b/tests/Jellyfin.XbmcMetadata.Tests/Parsers/MovieNfoParserTests.cs @@ -207,6 +207,20 @@ namespace Jellyfin.XbmcMetadata.Tests.Parsers Assert.Equal(id, item.ProviderIds[provider]); } + [Fact] + public void Parse_GivenFileWithFanartTag_Success() + { + var result = new MetadataResult