From 62bcd8df4956532880fe11475149990de0e0ef8a Mon Sep 17 00:00:00 2001 From: Mikal Stordal Date: Sun, 28 Apr 2024 16:41:21 +0200 Subject: [PATCH] misc: move resolvers above ignore rules --- Shokofin/Resolvers/ShokoResolveManager.cs | 278 +++++++++++----------- 1 file changed, 139 insertions(+), 139 deletions(-) diff --git a/Shokofin/Resolvers/ShokoResolveManager.cs b/Shokofin/Resolvers/ShokoResolveManager.cs index b9bb3470..027f6b5a 100644 --- a/Shokofin/Resolvers/ShokoResolveManager.cs +++ b/Shokofin/Resolvers/ShokoResolveManager.cs @@ -1175,6 +1175,145 @@ private static bool TryGetIdsForPath(string path, [NotNullWhen(true)] out string #endregion + #region Resolvers + + public async Task ResolveSingle(Folder? parent, string? collectionType, FileSystemMetadata fileInfo) + { + if (!(collectionType == CollectionType.TvShows || collectionType == CollectionType.Movies || collectionType == null) || parent == null || fileInfo == null) + return null; + + var root = LibraryManager.RootFolder; + if (root == null || parent == root) + return null; + + try { + if (!Lookup.IsEnabledForItem(parent)) + return null; + + // Skip anything outside the VFS. + if (!fileInfo.FullName.StartsWith(Plugin.Instance.VirtualRoot)) + return null; + + if (parent.GetTopParent() is not Folder mediaFolder) + return null; + + var vfsPath = await GenerateStructureInVFS(mediaFolder, fileInfo.FullName).ConfigureAwait(false); + if (string.IsNullOrEmpty(vfsPath)) + return null; + + if (parent.Id == mediaFolder.Id && fileInfo.IsDirectory) { + if (!fileInfo.Name.TryGetAttributeValue(ShokoSeriesId.Name, out var seriesId) || !int.TryParse(seriesId, out _)) + return null; + + return new TvSeries() { + Path = fileInfo.FullName, + }; + } + + return null; + } + catch (Exception ex) { + Logger.LogError(ex, "Threw unexpectedly; {Message}", ex.Message); + throw; + } + } + + public async Task ResolveMultiple(Folder? parent, string? collectionType, List fileInfoList) + { + if (!(collectionType == CollectionType.TvShows || collectionType == CollectionType.Movies || collectionType == null) || parent == null) + return null; + + var root = LibraryManager.RootFolder; + if (root == null || parent == root) + return null; + + try { + if (!Lookup.IsEnabledForItem(parent)) + return null; + + if (parent.GetTopParent() is not Folder mediaFolder) + return null; + + var vfsPath = await GenerateStructureInVFS(mediaFolder, parent.Path).ConfigureAwait(false); + if (string.IsNullOrEmpty(vfsPath)) + return null; + + // Redirect children of a VFS managed media folder to the VFS. + if (parent.IsTopParent) { + var createMovies = collectionType == CollectionType.Movies || (collectionType == null && Plugin.Instance.Configuration.SeparateMovies); + var items = FileSystem.GetDirectories(vfsPath) + .AsParallel() + .SelectMany(dirInfo => { + if (!dirInfo.Name.TryGetAttributeValue(ShokoSeriesId.Name, out var seriesId) || !int.TryParse(seriesId, out _)) + return Array.Empty(); + + var season = ApiManager.GetSeasonInfoForSeries(seriesId) + .ConfigureAwait(false) + .GetAwaiter() + .GetResult(); + if (season == null) + return Array.Empty(); + + if (createMovies && season.Type == SeriesType.Movie) { + return FileSystem.GetFiles(dirInfo.FullName) + .AsParallel() + .Select(fileInfo => { + // Only allow the video files, since the subtitle files also have the ids set. + if (!NamingOptions.VideoFileExtensions.Contains(Path.GetExtension(fileInfo.Name))) + return null; + + if (!TryGetIdsForPath(fileInfo.FullName, out seriesId, out var fileId)) + return null; + + // This will hopefully just re-use the pre-cached entries from the cache, but it may + // also get it from remote if the cache was emptied for whatever reason. + var file = ApiManager.GetFileInfo(fileId, seriesId) + .ConfigureAwait(false) + .GetAwaiter() + .GetResult(); + + // Abort if the file was not recognised. + if (file == null || file.ExtraType != null) + return null; + + return new Movie() { + Path = fileInfo.FullName, + } as BaseItem; + }) + .ToArray(); + } + + return new BaseItem[1] { + new TvSeries() { + Path = dirInfo.FullName, + }, + }; + }) + .OfType() + .ToList(); + + // TODO: uncomment the code snippet once the PR is in stable JF. + // return new() { Items = items, ExtraFiles = new() }; + + // TODO: Remove these two hacks once we have proper support for adding multiple series at once. + if (!items.Any(i => i is Movie) && items.Count > 0) { + fileInfoList.Clear(); + fileInfoList.AddRange(items.OrderBy(s => int.Parse(s.Path.GetAttributeValue(ShokoSeriesId.Name)!)).Select(s => FileSystem.GetFileSystemInfo(s.Path))); + } + + return new() { Items = items.Where(i => i is Movie).ToList(), ExtraFiles = items.OfType().Select(s => FileSystem.GetFileSystemInfo(s.Path)).ToList() }; + } + + return null; + } + catch (Exception ex) { + Logger.LogError(ex, "Threw unexpectedly; {Message}", ex.Message); + throw; + } + } + + #endregion + #region Ignore Rule public async Task ShouldFilterItem(Folder? parent, FileSystemMetadata fileInfo) @@ -1319,143 +1458,4 @@ private async Task ShouldFilterFile(string partialPath, string fullPath, b } #endregion - - #region Resolvers - - public async Task ResolveSingle(Folder? parent, string? collectionType, FileSystemMetadata fileInfo) - { - if (!(collectionType == CollectionType.TvShows || collectionType == CollectionType.Movies || collectionType == null) || parent == null || fileInfo == null) - return null; - - var root = LibraryManager.RootFolder; - if (root == null || parent == root) - return null; - - try { - if (!Lookup.IsEnabledForItem(parent)) - return null; - - // Skip anything outside the VFS. - if (!fileInfo.FullName.StartsWith(Plugin.Instance.VirtualRoot)) - return null; - - if (parent.GetTopParent() is not Folder mediaFolder) - return null; - - var vfsPath = await GenerateStructureInVFS(mediaFolder, fileInfo.FullName).ConfigureAwait(false); - if (string.IsNullOrEmpty(vfsPath)) - return null; - - if (parent.Id == mediaFolder.Id && fileInfo.IsDirectory) { - if (!fileInfo.Name.TryGetAttributeValue(ShokoSeriesId.Name, out var seriesId) || !int.TryParse(seriesId, out _)) - return null; - - return new TvSeries() { - Path = fileInfo.FullName, - }; - } - - return null; - } - catch (Exception ex) { - Logger.LogError(ex, "Threw unexpectedly; {Message}", ex.Message); - throw; - } - } - - public async Task ResolveMultiple(Folder? parent, string? collectionType, List fileInfoList) - { - if (!(collectionType == CollectionType.TvShows || collectionType == CollectionType.Movies || collectionType == null) || parent == null) - return null; - - var root = LibraryManager.RootFolder; - if (root == null || parent == root) - return null; - - try { - if (!Lookup.IsEnabledForItem(parent)) - return null; - - if (parent.GetTopParent() is not Folder mediaFolder) - return null; - - var vfsPath = await GenerateStructureInVFS(mediaFolder, parent.Path).ConfigureAwait(false); - if (string.IsNullOrEmpty(vfsPath)) - return null; - - // Redirect children of a VFS managed media folder to the VFS. - if (parent.IsTopParent) { - var createMovies = collectionType == CollectionType.Movies || (collectionType == null && Plugin.Instance.Configuration.SeparateMovies); - var items = FileSystem.GetDirectories(vfsPath) - .AsParallel() - .SelectMany(dirInfo => { - if (!dirInfo.Name.TryGetAttributeValue(ShokoSeriesId.Name, out var seriesId) || !int.TryParse(seriesId, out _)) - return Array.Empty(); - - var season = ApiManager.GetSeasonInfoForSeries(seriesId) - .ConfigureAwait(false) - .GetAwaiter() - .GetResult(); - if (season == null) - return Array.Empty(); - - if (createMovies && season.Type == SeriesType.Movie) { - return FileSystem.GetFiles(dirInfo.FullName) - .AsParallel() - .Select(fileInfo => { - // Only allow the video files, since the subtitle files also have the ids set. - if (!NamingOptions.VideoFileExtensions.Contains(Path.GetExtension(fileInfo.Name))) - return null; - - if (!TryGetIdsForPath(fileInfo.FullName, out seriesId, out var fileId)) - return null; - - // This will hopefully just re-use the pre-cached entries from the cache, but it may - // also get it from remote if the cache was emptied for whatever reason. - var file = ApiManager.GetFileInfo(fileId, seriesId) - .ConfigureAwait(false) - .GetAwaiter() - .GetResult(); - - // Abort if the file was not recognised. - if (file == null || file.ExtraType != null) - return null; - - return new Movie() { - Path = fileInfo.FullName, - } as BaseItem; - }) - .ToArray(); - } - - return new BaseItem[1] { - new TvSeries() { - Path = dirInfo.FullName, - }, - }; - }) - .OfType() - .ToList(); - - // TODO: uncomment the code snippet once the PR is in stable JF. - // return new() { Items = items, ExtraFiles = new() }; - - // TODO: Remove these two hacks once we have proper support for adding multiple series at once. - if (!items.Any(i => i is Movie) && items.Count > 0) { - fileInfoList.Clear(); - fileInfoList.AddRange(items.OrderBy(s => int.Parse(s.Path.GetAttributeValue(ShokoSeriesId.Name)!)).Select(s => FileSystem.GetFileSystemInfo(s.Path))); - } - - return new() { Items = items.Where(i => i is Movie).ToList(), ExtraFiles = items.OfType().Select(s => FileSystem.GetFileSystemInfo(s.Path)).ToList() }; - } - - return null; - } - catch (Exception ex) { - Logger.LogError(ex, "Threw unexpectedly; {Message}", ex.Message); - throw; - } - } - - #endregion } \ No newline at end of file