From 914e891689af38911332d14b18b4a1b4834cd9ae Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Wed, 24 Feb 2021 02:05:12 +0100 Subject: [PATCH 1/3] Fix unchecked input --- .../Library/LibraryManager.cs | 6 +++--- .../Controllers/LibraryStructureController.cs | 2 +- .../Library/ILibraryManager.cs | 2 +- .../Entities/CollectionTypeOptions.cs | 15 +++++++++++++++ 4 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 MediaBrowser.Model/Entities/CollectionTypeOptions.cs diff --git a/Emby.Server.Implementations/Library/LibraryManager.cs b/Emby.Server.Implementations/Library/LibraryManager.cs index db27862ce7..03da1add85 100644 --- a/Emby.Server.Implementations/Library/LibraryManager.cs +++ b/Emby.Server.Implementations/Library/LibraryManager.cs @@ -2956,7 +2956,7 @@ namespace Emby.Server.Implementations.Library throw new InvalidOperationException(); } - public async Task AddVirtualFolder(string name, string collectionType, LibraryOptions options, bool refreshLibrary) + public async Task AddVirtualFolder(string name, CollectionTypeOptions? collectionType, LibraryOptions options, bool refreshLibrary) { if (string.IsNullOrWhiteSpace(name)) { @@ -2990,9 +2990,9 @@ namespace Emby.Server.Implementations.Library { Directory.CreateDirectory(virtualFolderPath); - if (!string.IsNullOrEmpty(collectionType)) + if (collectionType != null) { - var path = Path.Combine(virtualFolderPath, collectionType + ".collection"); + var path = Path.Combine(virtualFolderPath, collectionType.ToString() + ".collection"); File.WriteAllBytes(path, Array.Empty()); } diff --git a/Jellyfin.Api/Controllers/LibraryStructureController.cs b/Jellyfin.Api/Controllers/LibraryStructureController.cs index 94995650cb..328efea26e 100644 --- a/Jellyfin.Api/Controllers/LibraryStructureController.cs +++ b/Jellyfin.Api/Controllers/LibraryStructureController.cs @@ -75,7 +75,7 @@ namespace Jellyfin.Api.Controllers [ProducesResponseType(StatusCodes.Status204NoContent)] public async Task AddVirtualFolder( [FromQuery] string? name, - [FromQuery] string? collectionType, + [FromQuery] CollectionTypeOptions? collectionType, [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] paths, [FromBody] AddVirtualFolderDto? libraryOptionsDto, [FromQuery] bool refreshLibrary = false) diff --git a/MediaBrowser.Controller/Library/ILibraryManager.cs b/MediaBrowser.Controller/Library/ILibraryManager.cs index 6700761fcc..80fcf71f3e 100644 --- a/MediaBrowser.Controller/Library/ILibraryManager.cs +++ b/MediaBrowser.Controller/Library/ILibraryManager.cs @@ -542,7 +542,7 @@ namespace MediaBrowser.Controller.Library Guid GetMusicGenreId(string name); - Task AddVirtualFolder(string name, string collectionType, LibraryOptions options, bool refreshLibrary); + Task AddVirtualFolder(string name, CollectionTypeOptions? collectionType, LibraryOptions options, bool refreshLibrary); Task RemoveVirtualFolder(string name, bool refreshLibrary); diff --git a/MediaBrowser.Model/Entities/CollectionTypeOptions.cs b/MediaBrowser.Model/Entities/CollectionTypeOptions.cs new file mode 100644 index 0000000000..d7615a7eda --- /dev/null +++ b/MediaBrowser.Model/Entities/CollectionTypeOptions.cs @@ -0,0 +1,15 @@ +#pragma warning disable CS1591 + +namespace MediaBrowser.Model.Entities +{ + public enum CollectionTypeOptions + { + Movies, + Music, + TvShows, + Books, + HomeVideos, + MusicVideos, + Mixed + } +} From 1c74e2f40e6790621ebc06dd37083a29be2459bd Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Wed, 24 Feb 2021 02:34:50 +0100 Subject: [PATCH 2/3] Fix build --- .../Collections/CollectionManager.cs | 2 +- .../Library/LibraryManager.cs | 17 +++++++++++++---- .../LiveTv/EmbyTV/EmbyTV.cs | 4 ++-- .../Entities/CollectionTypeOptions.cs | 15 ++++++++------- .../Entities/VirtualFolderInfo.cs | 2 +- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/Emby.Server.Implementations/Collections/CollectionManager.cs b/Emby.Server.Implementations/Collections/CollectionManager.cs index 3011a37e31..1ab2bdfbe8 100644 --- a/Emby.Server.Implementations/Collections/CollectionManager.cs +++ b/Emby.Server.Implementations/Collections/CollectionManager.cs @@ -107,7 +107,7 @@ namespace Emby.Server.Implementations.Collections var name = _localizationManager.GetLocalizedString("Collections"); - await _libraryManager.AddVirtualFolder(name, CollectionType.BoxSets, libraryOptions, true).ConfigureAwait(false); + await _libraryManager.AddVirtualFolder(name, CollectionTypeOptions.BoxSets, libraryOptions, true).ConfigureAwait(false); return FindFolders(path).First(); } diff --git a/Emby.Server.Implementations/Library/LibraryManager.cs b/Emby.Server.Implementations/Library/LibraryManager.cs index 03da1add85..799f8abc34 100644 --- a/Emby.Server.Implementations/Library/LibraryManager.cs +++ b/Emby.Server.Implementations/Library/LibraryManager.cs @@ -1240,11 +1240,20 @@ namespace Emby.Server.Implementations.Library return info; } - private string GetCollectionType(string path) + private CollectionTypeOptions GetCollectionType(string path) { - return _fileSystem.GetFilePaths(path, new[] { ".collection" }, true, false) - .Select(Path.GetFileNameWithoutExtension) - .FirstOrDefault(i => !string.IsNullOrEmpty(i)); + var files = _fileSystem.GetFilePaths(path, new[] { ".collection" }, true, false); + foreach (var file in files) + { + // TODO: @bond use a ReadOnlySpan here when Enum.TryParse supports it + // https://github.com/dotnet/runtime/issues/20008 + if (Enum.TryParse(Path.GetExtension(file), true, out var res)) + { + return res; + } + } + + throw new FileNotFoundException("Coudn't find an appropriate collection type file."); } /// diff --git a/Emby.Server.Implementations/LiveTv/EmbyTV/EmbyTV.cs b/Emby.Server.Implementations/LiveTv/EmbyTV/EmbyTV.cs index 2c0de661df..13b5a1c552 100644 --- a/Emby.Server.Implementations/LiveTv/EmbyTV/EmbyTV.cs +++ b/Emby.Server.Implementations/LiveTv/EmbyTV/EmbyTV.cs @@ -2604,7 +2604,7 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV { Locations = new string[] { customPath }, Name = "Recorded Movies", - CollectionType = CollectionType.Movies + CollectionType = CollectionTypeOptions.Movies }; } @@ -2615,7 +2615,7 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV { Locations = new string[] { customPath }, Name = "Recorded Shows", - CollectionType = CollectionType.TvShows + CollectionType = CollectionTypeOptions.TvShows }; } } diff --git a/MediaBrowser.Model/Entities/CollectionTypeOptions.cs b/MediaBrowser.Model/Entities/CollectionTypeOptions.cs index d7615a7eda..e1894d84ad 100644 --- a/MediaBrowser.Model/Entities/CollectionTypeOptions.cs +++ b/MediaBrowser.Model/Entities/CollectionTypeOptions.cs @@ -4,12 +4,13 @@ namespace MediaBrowser.Model.Entities { public enum CollectionTypeOptions { - Movies, - Music, - TvShows, - Books, - HomeVideos, - MusicVideos, - Mixed + Movies = 0, + TvShows = 1, + Music = 2, + MusicVideos = 3, + HomeVideos = 4, + BoxSets = 5, + Books = 6, + Mixed = 7 } } diff --git a/MediaBrowser.Model/Entities/VirtualFolderInfo.cs b/MediaBrowser.Model/Entities/VirtualFolderInfo.cs index 1b0e592401..55879b2e02 100644 --- a/MediaBrowser.Model/Entities/VirtualFolderInfo.cs +++ b/MediaBrowser.Model/Entities/VirtualFolderInfo.cs @@ -35,7 +35,7 @@ namespace MediaBrowser.Model.Entities /// Gets or sets the type of the collection. /// /// The type of the collection. - public string CollectionType { get; set; } + public CollectionTypeOptions CollectionType { get; set; } public LibraryOptions LibraryOptions { get; set; } From 81f527f8083289ce8eab77d522f51f06c3a5a70c Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Wed, 24 Feb 2021 11:57:04 +0100 Subject: [PATCH 3/3] CollectionType can be null --- Emby.Server.Implementations/Library/LibraryManager.cs | 4 ++-- MediaBrowser.Model/Entities/VirtualFolderInfo.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Emby.Server.Implementations/Library/LibraryManager.cs b/Emby.Server.Implementations/Library/LibraryManager.cs index 799f8abc34..d9ffe64b3b 100644 --- a/Emby.Server.Implementations/Library/LibraryManager.cs +++ b/Emby.Server.Implementations/Library/LibraryManager.cs @@ -1240,7 +1240,7 @@ namespace Emby.Server.Implementations.Library return info; } - private CollectionTypeOptions GetCollectionType(string path) + private CollectionTypeOptions? GetCollectionType(string path) { var files = _fileSystem.GetFilePaths(path, new[] { ".collection" }, true, false); foreach (var file in files) @@ -1253,7 +1253,7 @@ namespace Emby.Server.Implementations.Library } } - throw new FileNotFoundException("Coudn't find an appropriate collection type file."); + return null; } /// diff --git a/MediaBrowser.Model/Entities/VirtualFolderInfo.cs b/MediaBrowser.Model/Entities/VirtualFolderInfo.cs index 55879b2e02..ea3df37265 100644 --- a/MediaBrowser.Model/Entities/VirtualFolderInfo.cs +++ b/MediaBrowser.Model/Entities/VirtualFolderInfo.cs @@ -35,7 +35,7 @@ namespace MediaBrowser.Model.Entities /// Gets or sets the type of the collection. /// /// The type of the collection. - public CollectionTypeOptions CollectionType { get; set; } + public CollectionTypeOptions? CollectionType { get; set; } public LibraryOptions LibraryOptions { get; set; }