From 5fcbedc194a7a8a7a8026a69b44f8192120d14e1 Mon Sep 17 00:00:00 2001 From: Mark Monteiro Date: Sat, 28 Mar 2020 13:40:56 +0100 Subject: [PATCH 01/11] Display extras with an unknown type --- Emby.Server.Implementations/Dto/DtoService.cs | 21 ++----- .../UserLibrary/UserLibraryService.cs | 2 +- MediaBrowser.Controller/Entities/BaseItem.cs | 56 +++++++++++++++++-- 3 files changed, 56 insertions(+), 23 deletions(-) diff --git a/Emby.Server.Implementations/Dto/DtoService.cs b/Emby.Server.Implementations/Dto/DtoService.cs index 65711e89d8..a10e17f6ad 100644 --- a/Emby.Server.Implementations/Dto/DtoService.cs +++ b/Emby.Server.Implementations/Dto/DtoService.cs @@ -1056,30 +1056,19 @@ namespace Emby.Server.Implementations.Dto if (options.ContainsField(ItemFields.SpecialFeatureCount)) { - if (allExtras == null) - { - allExtras = item.GetExtras().ToArray(); - } - - dto.SpecialFeatureCount = allExtras.Count(i => i.ExtraType.HasValue && BaseItem.DisplayExtraTypes.Contains(i.ExtraType.Value)); + allExtras = item.GetExtras().ToArray(); + dto.SpecialFeatureCount = allExtras.Count(i => i.HasExtraType(BaseItem.DisplayExtraTypes, true)); } if (options.ContainsField(ItemFields.LocalTrailerCount)) { - int trailerCount = 0; - if (allExtras == null) - { - allExtras = item.GetExtras().ToArray(); - } - - trailerCount += allExtras.Count(i => i.ExtraType.HasValue && i.ExtraType.Value == ExtraType.Trailer); + allExtras = allExtras ?? item.GetExtras().ToArray(); + dto.LocalTrailerCount = allExtras.Count(i => i.HasExtraType(new[] { ExtraType.Trailer }, false)); if (item is IHasTrailers hasTrailers) { - trailerCount += hasTrailers.GetTrailerCount(); + dto.LocalTrailerCount += hasTrailers.GetTrailerCount(); } - - dto.LocalTrailerCount = trailerCount; } // Add EpisodeInfo diff --git a/MediaBrowser.Api/UserLibrary/UserLibraryService.cs b/MediaBrowser.Api/UserLibrary/UserLibraryService.cs index 2ec08f5787..a55b0253ac 100644 --- a/MediaBrowser.Api/UserLibrary/UserLibraryService.cs +++ b/MediaBrowser.Api/UserLibrary/UserLibraryService.cs @@ -380,7 +380,7 @@ namespace MediaBrowser.Api.UserLibrary var dtoOptions = GetDtoOptions(_authContext, request); - var dtosExtras = item.GetExtras(new[] { ExtraType.Trailer }) + var dtosExtras = item.GetExtras(new[] { ExtraType.Trailer }, false) .Select(i => _dtoService.GetBaseItemDto(i, dtoOptions, user, item)) .ToArray(); diff --git a/MediaBrowser.Controller/Entities/BaseItem.cs b/MediaBrowser.Controller/Entities/BaseItem.cs index a9ec19e2fd..2a941a684a 100644 --- a/MediaBrowser.Controller/Entities/BaseItem.cs +++ b/MediaBrowser.Controller/Entities/BaseItem.cs @@ -2878,14 +2878,30 @@ namespace MediaBrowser.Controller.Entities /// The remote trailers. public IReadOnlyList RemoteTrailers { get; set; } + /// + /// Get all extras associated with this item, sorted by . + /// + /// An enumerable containing the items. public IEnumerable GetExtras() { - return ExtraIds.Select(LibraryManager.GetItemById).Where(i => i != null).OrderBy(i => i.SortName); + return ExtraIds + .Select(LibraryManager.GetItemById) + .Where(i => i != null) + .OrderBy(i => i.SortName); } - public IEnumerable GetExtras(IReadOnlyCollection extraTypes) + /// + /// Get all extras with specific types that are associated with this item. + /// + /// The types of extras to retrieve. + /// If true, include extras whose type could not be determined. + /// An enumerable containing the extras. + public IEnumerable GetExtras(IReadOnlyCollection extraTypes, bool includeUnknownTypes) { - return ExtraIds.Select(LibraryManager.GetItemById).Where(i => i?.ExtraType != null && extraTypes.Contains(i.ExtraType.Value)); + return ExtraIds + .Select(LibraryManager.GetItemById) + .Where(i => i != null) + .Where(i => i.HasExtraType(extraTypes, includeUnknownTypes)); } public IEnumerable GetTrailers() @@ -2896,9 +2912,27 @@ namespace MediaBrowser.Controller.Entities return Array.Empty(); } + /// + /// Get all extras associated with this item that should be displayed as "Special Features" in the UI. This is + /// all extras with either an unknown type, or a type contained in . + /// + /// An IEnumerable containing the extra items. public IEnumerable GetDisplayExtras() { - return GetExtras(DisplayExtraTypes); + return GetExtras(DisplayExtraTypes, true); + } + + /// + /// Check if this item is an extra with a type that matches a given set. + /// + /// The types of extras to match with. + /// If true, include extras whose type could not be determined. + /// True if this item matches, otherwise false. + public bool HasExtraType(IReadOnlyCollection extraTypes, bool includeUnknownTypes) + { + return + (includeUnknownTypes && (ExtraType == null || ExtraType == 0)) + || (ExtraType.HasValue && extraTypes.Contains(ExtraType.Value)); } public virtual bool IsHD => Height >= 720; @@ -2918,8 +2952,18 @@ namespace MediaBrowser.Controller.Entities return RunTimeTicks ?? 0; } - // Possible types of extra videos - public static readonly IReadOnlyCollection DisplayExtraTypes = new[] { Model.Entities.ExtraType.BehindTheScenes, Model.Entities.ExtraType.Clip, Model.Entities.ExtraType.DeletedScene, Model.Entities.ExtraType.Interview, Model.Entities.ExtraType.Sample, Model.Entities.ExtraType.Scene }; + /// + /// Extra types that should be counted and displayed as "Special Features" in the UI. + /// + public static readonly IReadOnlyCollection DisplayExtraTypes = new HashSet + { + Model.Entities.ExtraType.BehindTheScenes, + Model.Entities.ExtraType.Clip, + Model.Entities.ExtraType.DeletedScene, + Model.Entities.ExtraType.Interview, + Model.Entities.ExtraType.Sample, + Model.Entities.ExtraType.Scene + }; public virtual bool SupportsExternalTransfer => false; From ea306e8f6d28e4c4acb78ee7208508d329473414 Mon Sep 17 00:00:00 2001 From: Mark Monteiro Date: Sat, 28 Mar 2020 13:54:14 +0100 Subject: [PATCH 02/11] Do not assign an invalid value of zero to ExtraType --- MediaBrowser.Controller/Entities/BaseItem.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/MediaBrowser.Controller/Entities/BaseItem.cs b/MediaBrowser.Controller/Entities/BaseItem.cs index 2a941a684a..66917e4428 100644 --- a/MediaBrowser.Controller/Entities/BaseItem.cs +++ b/MediaBrowser.Controller/Entities/BaseItem.cs @@ -1327,8 +1327,9 @@ namespace MediaBrowser.Controller.Entities } // Use some hackery to get the extra type based on foldername - Enum.TryParse(extraFolderName.Replace(" ", ""), true, out ExtraType extraType); - item.ExtraType = extraType; + item.ExtraType = Enum.TryParse(extraFolderName.Replace(" ", ""), true, out ExtraType extraType) + ? extraType + : (ExtraType?)null; return item; From e266ad51c52763f11a2eab9ac86f638542a4ddb0 Mon Sep 17 00:00:00 2001 From: Mark Monteiro Date: Sat, 28 Mar 2020 14:17:33 +0100 Subject: [PATCH 03/11] Simplify logic; remove unnecessary methods --- Emby.Server.Implementations/Dto/DtoService.cs | 4 +-- .../UserLibrary/UserLibraryService.cs | 5 +-- MediaBrowser.Controller/Entities/BaseItem.cs | 32 +++---------------- 3 files changed, 10 insertions(+), 31 deletions(-) diff --git a/Emby.Server.Implementations/Dto/DtoService.cs b/Emby.Server.Implementations/Dto/DtoService.cs index a10e17f6ad..f8cf00cb3b 100644 --- a/Emby.Server.Implementations/Dto/DtoService.cs +++ b/Emby.Server.Implementations/Dto/DtoService.cs @@ -1057,13 +1057,13 @@ namespace Emby.Server.Implementations.Dto if (options.ContainsField(ItemFields.SpecialFeatureCount)) { allExtras = item.GetExtras().ToArray(); - dto.SpecialFeatureCount = allExtras.Count(i => i.HasExtraType(BaseItem.DisplayExtraTypes, true)); + dto.SpecialFeatureCount = allExtras.Count(i => BaseItem.DisplayExtraTypes.Contains(i.ExtraType)); } if (options.ContainsField(ItemFields.LocalTrailerCount)) { allExtras = allExtras ?? item.GetExtras().ToArray(); - dto.LocalTrailerCount = allExtras.Count(i => i.HasExtraType(new[] { ExtraType.Trailer }, false)); + dto.LocalTrailerCount = allExtras.Count(i => i.ExtraType == ExtraType.Trailer); if (item is IHasTrailers hasTrailers) { diff --git a/MediaBrowser.Api/UserLibrary/UserLibraryService.cs b/MediaBrowser.Api/UserLibrary/UserLibraryService.cs index a55b0253ac..21d2fc9923 100644 --- a/MediaBrowser.Api/UserLibrary/UserLibraryService.cs +++ b/MediaBrowser.Api/UserLibrary/UserLibraryService.cs @@ -361,7 +361,8 @@ namespace MediaBrowser.Api.UserLibrary var dtoOptions = GetDtoOptions(_authContext, request); - var dtos = item.GetDisplayExtras() + var dtos = item + .GetExtras(BaseItem.DisplayExtraTypes) .Select(i => _dtoService.GetBaseItemDto(i, dtoOptions, user, item)); return dtos.ToArray(); @@ -380,7 +381,7 @@ namespace MediaBrowser.Api.UserLibrary var dtoOptions = GetDtoOptions(_authContext, request); - var dtosExtras = item.GetExtras(new[] { ExtraType.Trailer }, false) + var dtosExtras = item.GetExtras(new ExtraType?[] { ExtraType.Trailer }) .Select(i => _dtoService.GetBaseItemDto(i, dtoOptions, user, item)) .ToArray(); diff --git a/MediaBrowser.Controller/Entities/BaseItem.cs b/MediaBrowser.Controller/Entities/BaseItem.cs index 66917e4428..2424293fe1 100644 --- a/MediaBrowser.Controller/Entities/BaseItem.cs +++ b/MediaBrowser.Controller/Entities/BaseItem.cs @@ -2895,14 +2895,13 @@ namespace MediaBrowser.Controller.Entities /// Get all extras with specific types that are associated with this item. /// /// The types of extras to retrieve. - /// If true, include extras whose type could not be determined. /// An enumerable containing the extras. - public IEnumerable GetExtras(IReadOnlyCollection extraTypes, bool includeUnknownTypes) + public IEnumerable GetExtras(IReadOnlyCollection extraTypes) { return ExtraIds .Select(LibraryManager.GetItemById) .Where(i => i != null) - .Where(i => i.HasExtraType(extraTypes, includeUnknownTypes)); + .Where(i => extraTypes.Contains(i.ExtraType)); } public IEnumerable GetTrailers() @@ -2913,29 +2912,6 @@ namespace MediaBrowser.Controller.Entities return Array.Empty(); } - /// - /// Get all extras associated with this item that should be displayed as "Special Features" in the UI. This is - /// all extras with either an unknown type, or a type contained in . - /// - /// An IEnumerable containing the extra items. - public IEnumerable GetDisplayExtras() - { - return GetExtras(DisplayExtraTypes, true); - } - - /// - /// Check if this item is an extra with a type that matches a given set. - /// - /// The types of extras to match with. - /// If true, include extras whose type could not be determined. - /// True if this item matches, otherwise false. - public bool HasExtraType(IReadOnlyCollection extraTypes, bool includeUnknownTypes) - { - return - (includeUnknownTypes && (ExtraType == null || ExtraType == 0)) - || (ExtraType.HasValue && extraTypes.Contains(ExtraType.Value)); - } - public virtual bool IsHD => Height >= 720; public bool IsShortcut { get; set; } @@ -2956,8 +2932,10 @@ namespace MediaBrowser.Controller.Entities /// /// Extra types that should be counted and displayed as "Special Features" in the UI. /// - public static readonly IReadOnlyCollection DisplayExtraTypes = new HashSet + public static readonly IReadOnlyCollection DisplayExtraTypes = new HashSet { + null, + 0, Model.Entities.ExtraType.BehindTheScenes, Model.Entities.ExtraType.Clip, Model.Entities.ExtraType.DeletedScene, From cc8294842a41478e7c22bcef39c0cfcc114f05a0 Mon Sep 17 00:00:00 2001 From: Mark Monteiro Date: Wed, 1 Apr 2020 18:10:29 +0200 Subject: [PATCH 04/11] Add ExtraType.Unknown enum value and use it instead of null --- Emby.Server.Implementations/Dto/DtoService.cs | 2 +- MediaBrowser.Api/UserLibrary/UserLibraryService.cs | 2 +- MediaBrowser.Controller/Entities/BaseItem.cs | 11 +++++------ MediaBrowser.Model/Entities/ExtraType.cs | 1 + 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Emby.Server.Implementations/Dto/DtoService.cs b/Emby.Server.Implementations/Dto/DtoService.cs index f8cf00cb3b..67ecdb4ea7 100644 --- a/Emby.Server.Implementations/Dto/DtoService.cs +++ b/Emby.Server.Implementations/Dto/DtoService.cs @@ -1057,7 +1057,7 @@ namespace Emby.Server.Implementations.Dto if (options.ContainsField(ItemFields.SpecialFeatureCount)) { allExtras = item.GetExtras().ToArray(); - dto.SpecialFeatureCount = allExtras.Count(i => BaseItem.DisplayExtraTypes.Contains(i.ExtraType)); + dto.SpecialFeatureCount = allExtras.Count(i => i.ExtraType.HasValue && BaseItem.DisplayExtraTypes.Contains(i.ExtraType.Value)); } if (options.ContainsField(ItemFields.LocalTrailerCount)) diff --git a/MediaBrowser.Api/UserLibrary/UserLibraryService.cs b/MediaBrowser.Api/UserLibrary/UserLibraryService.cs index 21d2fc9923..4cd201c3b0 100644 --- a/MediaBrowser.Api/UserLibrary/UserLibraryService.cs +++ b/MediaBrowser.Api/UserLibrary/UserLibraryService.cs @@ -381,7 +381,7 @@ namespace MediaBrowser.Api.UserLibrary var dtoOptions = GetDtoOptions(_authContext, request); - var dtosExtras = item.GetExtras(new ExtraType?[] { ExtraType.Trailer }) + var dtosExtras = item.GetExtras(new ExtraType[] { ExtraType.Trailer }) .Select(i => _dtoService.GetBaseItemDto(i, dtoOptions, user, item)) .ToArray(); diff --git a/MediaBrowser.Controller/Entities/BaseItem.cs b/MediaBrowser.Controller/Entities/BaseItem.cs index 2424293fe1..cfc80ac095 100644 --- a/MediaBrowser.Controller/Entities/BaseItem.cs +++ b/MediaBrowser.Controller/Entities/BaseItem.cs @@ -1329,7 +1329,7 @@ namespace MediaBrowser.Controller.Entities // Use some hackery to get the extra type based on foldername item.ExtraType = Enum.TryParse(extraFolderName.Replace(" ", ""), true, out ExtraType extraType) ? extraType - : (ExtraType?)null; + : Model.Entities.ExtraType.Unknown; return item; @@ -2896,12 +2896,12 @@ namespace MediaBrowser.Controller.Entities /// /// The types of extras to retrieve. /// An enumerable containing the extras. - public IEnumerable GetExtras(IReadOnlyCollection extraTypes) + public IEnumerable GetExtras(IReadOnlyCollection extraTypes) { return ExtraIds .Select(LibraryManager.GetItemById) .Where(i => i != null) - .Where(i => extraTypes.Contains(i.ExtraType)); + .Where(i => i.ExtraType.HasValue && extraTypes.Contains(i.ExtraType.Value)); } public IEnumerable GetTrailers() @@ -2932,10 +2932,9 @@ namespace MediaBrowser.Controller.Entities /// /// Extra types that should be counted and displayed as "Special Features" in the UI. /// - public static readonly IReadOnlyCollection DisplayExtraTypes = new HashSet + public static readonly IReadOnlyCollection DisplayExtraTypes = new HashSet { - null, - 0, + Model.Entities.ExtraType.Unknown, Model.Entities.ExtraType.BehindTheScenes, Model.Entities.ExtraType.Clip, Model.Entities.ExtraType.DeletedScene, diff --git a/MediaBrowser.Model/Entities/ExtraType.cs b/MediaBrowser.Model/Entities/ExtraType.cs index 857e92adbe..aca4bd2829 100644 --- a/MediaBrowser.Model/Entities/ExtraType.cs +++ b/MediaBrowser.Model/Entities/ExtraType.cs @@ -4,6 +4,7 @@ namespace MediaBrowser.Model.Entities { public enum ExtraType { + Unknown = 0, Clip = 1, Trailer = 2, BehindTheScenes = 3, From 2e1ec2858a5bd2a03d8dd5fd0ae9f9c11487ed7f Mon Sep 17 00:00:00 2001 From: Mark Monteiro Date: Wed, 1 Apr 2020 18:53:19 +0200 Subject: [PATCH 05/11] Match using directory names in ExtraResolver --- Emby.Naming/Common/NamingOptions.cs | 10 +++++++++- Emby.Naming/Video/ExtraResolver.cs | 9 +++++++++ Emby.Naming/Video/ExtraRuleType.cs | 13 +++++++++---- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/Emby.Naming/Common/NamingOptions.cs b/Emby.Naming/Common/NamingOptions.cs index 793847f84c..8ac33d3f55 100644 --- a/Emby.Naming/Common/NamingOptions.cs +++ b/Emby.Naming/Common/NamingOptions.cs @@ -505,7 +505,15 @@ namespace Emby.Naming.Common RuleType = ExtraRuleType.Suffix, Token = "-short", MediaType = MediaType.Video - } + }, + new ExtraRule { RuleType = ExtraRuleType.DirectoryName, MediaType = MediaType.Video, ExtraType = ExtraType.BehindTheScenes, Token = "behind the scenes" }, + new ExtraRule { RuleType = ExtraRuleType.DirectoryName, MediaType = MediaType.Video, ExtraType = ExtraType.DeletedScene, Token = "deleted scenes" }, + new ExtraRule { RuleType = ExtraRuleType.DirectoryName, MediaType = MediaType.Video, ExtraType = ExtraType.Interview, Token = "interviews" }, + new ExtraRule { RuleType = ExtraRuleType.DirectoryName, MediaType = MediaType.Video, ExtraType = ExtraType.Scene, Token = "scenes" }, + new ExtraRule { RuleType = ExtraRuleType.DirectoryName, MediaType = MediaType.Video, ExtraType = ExtraType.Sample, Token = "samples" }, + new ExtraRule { RuleType = ExtraRuleType.DirectoryName, MediaType = MediaType.Video, ExtraType = ExtraType.Clip, Token = "shorts" }, + new ExtraRule { RuleType = ExtraRuleType.DirectoryName, MediaType = MediaType.Video, ExtraType = ExtraType.Clip, Token = "featurettes" }, + new ExtraRule { RuleType = ExtraRuleType.DirectoryName, MediaType = MediaType.Video, ExtraType = ExtraType.Unknown, Token = "extras" }, }; Format3DRules = new[] diff --git a/Emby.Naming/Video/ExtraResolver.cs b/Emby.Naming/Video/ExtraResolver.cs index 42a5c88b31..fc0424faab 100644 --- a/Emby.Naming/Video/ExtraResolver.cs +++ b/Emby.Naming/Video/ExtraResolver.cs @@ -80,6 +80,15 @@ namespace Emby.Naming.Video result.Rule = rule; } } + else if (rule.RuleType == ExtraRuleType.DirectoryName) + { + var directoryName = Path.GetFileName(Path.GetDirectoryName(path)); + if (string.Equals(directoryName, rule.Token, StringComparison.OrdinalIgnoreCase)) + { + result.ExtraType = rule.ExtraType; + result.Rule = rule; + } + } return result; } diff --git a/Emby.Naming/Video/ExtraRuleType.cs b/Emby.Naming/Video/ExtraRuleType.cs index b021a04a31..0a304874d2 100644 --- a/Emby.Naming/Video/ExtraRuleType.cs +++ b/Emby.Naming/Video/ExtraRuleType.cs @@ -5,18 +5,23 @@ namespace Emby.Naming.Video public enum ExtraRuleType { /// - /// The suffix + /// Match against a suffix in the file name. /// Suffix = 0, /// - /// The filename + /// Match against the file name. /// Filename = 1, /// - /// The regex + /// Match against the a regex. /// - Regex = 2 + Regex = 2, + + /// + /// Match against the directory name of the file. + /// + DirectoryName = 3, } } From 34204046dd8304c6fbd06c112a3e13b4bba1c506 Mon Sep 17 00:00:00 2001 From: Mark Monteiro Date: Wed, 1 Apr 2020 18:53:53 +0200 Subject: [PATCH 06/11] Remove duplicate array of extras directory names --- Emby.Server.Implementations/Library/LibraryManager.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Emby.Server.Implementations/Library/LibraryManager.cs b/Emby.Server.Implementations/Library/LibraryManager.cs index 8ec4d08be5..0ddef0bc24 100644 --- a/Emby.Server.Implementations/Library/LibraryManager.cs +++ b/Emby.Server.Implementations/Library/LibraryManager.cs @@ -2609,14 +2609,12 @@ namespace Emby.Server.Implementations.Library }).OrderBy(i => i.Path); } - private static readonly string[] ExtrasSubfolderNames = new[] { "extras", "specials", "shorts", "scenes", "featurettes", "behind the scenes", "deleted scenes", "interviews" }; - public IEnumerable