diff options
Diffstat (limited to 'MediaBrowser.Server.Implementations/Library/Resolvers/Movies/MovieResolver.cs')
| -rw-r--r-- | MediaBrowser.Server.Implementations/Library/Resolvers/Movies/MovieResolver.cs | 301 |
1 files changed, 181 insertions, 120 deletions
diff --git a/MediaBrowser.Server.Implementations/Library/Resolvers/Movies/MovieResolver.cs b/MediaBrowser.Server.Implementations/Library/Resolvers/Movies/MovieResolver.cs index 6d396ad9cc..f48b8329e9 100644 --- a/MediaBrowser.Server.Implementations/Library/Resolvers/Movies/MovieResolver.cs +++ b/MediaBrowser.Server.Implementations/Library/Resolvers/Movies/MovieResolver.cs @@ -2,12 +2,14 @@ using MediaBrowser.Controller; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Movies; +using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Providers; using MediaBrowser.Controller.Resolvers; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Logging; using MediaBrowser.Naming.Common; +using MediaBrowser.Naming.IO; using MediaBrowser.Naming.Video; using System; using System.Collections.Generic; @@ -19,13 +21,14 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Movies /// <summary> /// Class MovieResolver /// </summary> - public class MovieResolver : BaseVideoResolver<Video> + public class MovieResolver : BaseVideoResolver<Video>, IMultiItemResolver { private readonly IServerApplicationPaths _applicationPaths; private readonly ILogger _logger; private readonly IFileSystem _fileSystem; - public MovieResolver(ILibraryManager libraryManager, IServerApplicationPaths applicationPaths, ILogger logger, IFileSystem fileSystem) : base(libraryManager) + public MovieResolver(ILibraryManager libraryManager, IServerApplicationPaths applicationPaths, ILogger logger, IFileSystem fileSystem) + : base(libraryManager) { _applicationPaths = applicationPaths; _logger = logger; @@ -43,10 +46,107 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Movies // Give plugins a chance to catch iso's first // Also since we have to loop through child files looking for videos, // see if we can avoid some of that by letting other resolvers claim folders first - return ResolverPriority.Second; + // Also run after series resolver + return ResolverPriority.Third; } } + public MultiItemResolverResult ResolveMultiple(Folder parent, + List<FileSystemInfo> files, + string collectionType, + IDirectoryService directoryService) + { + if (IsInvalid(parent, collectionType, files)) + { + return null; + } + + if (string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase)) + { + return ResolveVideos<MusicVideo>(parent, files, directoryService, collectionType); + } + + if (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase)) + { + return ResolveVideos<Video>(parent, files, directoryService, collectionType); + } + + if (string.IsNullOrEmpty(collectionType)) + { + // Owned items should just use the plain video type + if (parent == null) + { + return ResolveVideos<Video>(parent, files, directoryService, collectionType); + } + + return ResolveVideos<Movie>(parent, files, directoryService, collectionType); + } + + if (string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase) || + string.Equals(collectionType, CollectionType.BoxSets, StringComparison.OrdinalIgnoreCase)) + { + return ResolveVideos<Movie>(parent, files, directoryService, collectionType); + } + + return null; + } + + private MultiItemResolverResult ResolveVideos<T>(Folder parent, IEnumerable<FileSystemInfo> fileSystemEntries, IDirectoryService directoryService, string collectionType) + where T : Video, new() + { + var files = new List<FileSystemInfo>(); + var videos = new List<BaseItem>(); + var leftOver = new List<FileSystemInfo>(); + + // Loop through each child file/folder and see if we find a video + foreach (var child in fileSystemEntries) + { + if ((child.Attributes & FileAttributes.Directory) == FileAttributes.Directory) + { + leftOver.Add(child); + } + else + { + files.Add(child); + } + } + + var resolver = new VideoListResolver(new ExtendedNamingOptions(), new Naming.Logging.NullLogger()); + var resolverResult = resolver.Resolve(files.Select(i => new PortableFileInfo + { + FullName = i.FullName, + Type = FileInfoType.File + + }).ToList()); + + var result = new MultiItemResolverResult + { + ExtraFiles = leftOver, + Items = videos + }; + + foreach (var video in resolverResult) + { + var firstVideo = video.Files.First(); + + var videoItem = new T + { + Path = video.Files[0].Path, + IsInMixedFolder = true, + ProductionYear = video.Year, + Name = video.Name, + AdditionalParts = video.Files.Skip(1).Select(i => i.Path).ToList() + }; + + SetVideoType(videoItem, firstVideo); + Set3DFormat(videoItem, firstVideo); + + result.Items.Add(videoItem); + } + + return result; + } + /// <summary> /// Resolves the specified args. /// </summary> @@ -54,28 +154,24 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Movies /// <returns>Video.</returns> protected override Video Resolve(ItemResolveArgs args) { - // Avoid expensive tests against VF's and all their children by not allowing this - if (args.Parent != null) + var collectionType = args.GetCollectionType(); + + if (IsInvalid(args.Parent, collectionType, args.FileSystemChildren)) { - if (args.Parent.IsRoot) - { - return null; - } + return null; } - var collectionType = args.GetCollectionType(); - // Find movies with their own folders if (args.IsDirectory) { if (string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase)) { - return FindMovie<MusicVideo>(args.Path, args.Parent, args.FileSystemChildren.ToList(), args.DirectoryService, false, collectionType); + return FindMovie<MusicVideo>(args.Path, args.Parent, args.FileSystemChildren.ToList(), args.DirectoryService, collectionType); } if (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase)) { - return FindMovie<Video>(args.Path, args.Parent, args.FileSystemChildren.ToList(), args.DirectoryService, false, collectionType); + return FindMovie<Video>(args.Path, args.Parent, args.FileSystemChildren.ToList(), args.DirectoryService, collectionType); } if (string.IsNullOrEmpty(collectionType)) @@ -83,7 +179,7 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Movies // Owned items should just use the plain video type if (args.Parent == null) { - return FindMovie<Video>(args.Path, args.Parent, args.FileSystemChildren.ToList(), args.DirectoryService, false, collectionType); + return FindMovie<Video>(args.Path, args.Parent, args.FileSystemChildren.ToList(), args.DirectoryService, collectionType); } // Since the looping is expensive, this is an optimization to help us avoid it @@ -91,22 +187,21 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Movies { return null; } - - return FindMovie<Movie>(args.Path, args.Parent, args.FileSystemChildren.ToList(), args.DirectoryService, true, collectionType); + + return FindMovie<Movie>(args.Path, args.Parent, args.FileSystemChildren.ToList(), args.DirectoryService, collectionType); } if (string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase) || string.Equals(collectionType, CollectionType.BoxSets, StringComparison.OrdinalIgnoreCase)) { - return FindMovie<Movie>(args.Path, args.Parent, args.FileSystemChildren.ToList(), args.DirectoryService, true, collectionType); + return FindMovie<Movie>(args.Path, args.Parent, args.FileSystemChildren.ToList(), args.DirectoryService, collectionType); } - + return null; } - var filename = Path.GetFileName(args.Path); - // Don't misidentify extras or trailers - if (BaseItem.ExtraSuffixes.Any(i => filename.IndexOf(i.Key, StringComparison.OrdinalIgnoreCase) != -1)) + // Owned items will be caught by the plain video resolver + if (args.Parent == null) { return null; } @@ -115,7 +210,7 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Movies if (string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase)) { - item = ResolveVideo<MusicVideo>(args, true); + item = ResolveVideo<MusicVideo>(args, false); } // To find a movie file, the collection type must be movies or boxsets @@ -124,7 +219,16 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Movies { item = ResolveVideo<Movie>(args, true); } - + + else if (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase)) + { + item = ResolveVideo<Video>(args, false); + } + else if (string.IsNullOrEmpty(collectionType)) + { + item = ResolveVideo<Movie>(args, false); + } + if (item != null) { item.IsInMixedFolder = true; @@ -170,17 +274,14 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Movies /// <param name="parent">The parent.</param> /// <param name="fileSystemEntries">The file system entries.</param> /// <param name="directoryService">The directory service.</param> - /// <param name="supportsMultipleSources">if set to <c>true</c> [supports multiple sources].</param> /// <param name="collectionType">Type of the collection.</param> /// <returns>Movie.</returns> - private T FindMovie<T>(string path, Folder parent, IEnumerable<FileSystemInfo> fileSystemEntries, IDirectoryService directoryService, bool supportsMultipleSources, string collectionType) + private T FindMovie<T>(string path, Folder parent, List<FileSystemInfo> fileSystemEntries, IDirectoryService directoryService, string collectionType) where T : Video, new() { - var movies = new List<T>(); - var multiDiscFolders = new List<FileSystemInfo>(); - - // Loop through each child file/folder and see if we find a video + + // Search for a folder rip foreach (var child in fileSystemEntries) { var filename = child.Name; @@ -189,76 +290,59 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Movies { if (IsDvdDirectory(filename)) { - return new T + var movie = new T { Path = path, VideoType = VideoType.Dvd }; + Set3DFormat(movie); + return movie; } if (IsBluRayDirectory(filename)) { - return new T + var movie = new T { Path = path, VideoType = VideoType.BluRay }; + Set3DFormat(movie); + return movie; } multiDiscFolders.Add(child); - - continue; - } - - // Don't misidentify extras or trailers as a movie - if (BaseItem.ExtraSuffixes.Any(i => filename.IndexOf(i.Key, StringComparison.OrdinalIgnoreCase) != -1)) - { - continue; - } - - var childArgs = new ItemResolveArgs(_applicationPaths, LibraryManager, directoryService) - { - FileInfo = child, - Path = child.FullName, - Parent = parent, - CollectionType = collectionType - }; - - var item = ResolveVideo<T>(childArgs, true); - - if (item != null) - { - item.IsInMixedFolder = false; - movies.Add(item); } } + + var result = ResolveVideos<T>(parent, fileSystemEntries, directoryService, collectionType); - if (movies.Count > 1) + // Test for multi-editions + if (result.Items.Count > 1) { - var multiFileResult = GetMultiFileMovie(movies); + var filenamePrefix = Path.GetFileName(path); - if (multiFileResult != null) + if (!string.IsNullOrWhiteSpace(filenamePrefix)) { - return multiFileResult; - } + if (result.Items.All(i => _fileSystem.GetFileNameWithoutExtension(i.Path).StartsWith(filenamePrefix + " - ", StringComparison.OrdinalIgnoreCase))) + { + var movie = (T)result.Items[0]; + movie.Name = filenamePrefix; + movie.LocalAlternateVersions = result.Items.Skip(1).Select(i => i.Path).ToList(); - if (supportsMultipleSources) - { - var result = GetMovieWithMultipleSources(movies); + _logger.Debug("Multi-version video found: " + movie.Path); - if (result != null) - { - return result; + return movie; } } - return null; } - if (movies.Count == 1) + if (result.Items.Count == 1) { - return movies[0]; + var movie = (T)result.Items[0]; + movie.IsInMixedFolder = false; + return movie; } - if (multiDiscFolders.Count > 0) + if (result.Items.Count == 0 && multiDiscFolders.Count > 0) { return GetMultiDiscMovie<T>(multiDiscFolders, directoryService); } @@ -318,7 +402,7 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Movies { return null; } - + return new T { Path = folderPaths[0], @@ -331,65 +415,42 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Movies }; } - /// <summary> - /// Gets the multi file movie. - /// </summary> - /// <typeparam name="T"></typeparam> - /// <param name="movies">The movies.</param> - /// <returns>``0.</returns> - private T GetMultiFileMovie<T>(IEnumerable<T> movies) - where T : Video, new() - { - var sortedMovies = movies.OrderBy(i => i.Path).ToList(); - - var firstMovie = sortedMovies[0]; - - var paths = sortedMovies.Select(i => i.Path).ToList(); - - var resolver = new StackResolver(new ExtendedNamingOptions(), new Naming.Logging.NullLogger()); - - var result = resolver.ResolveFiles(paths); - - if (result.Stacks.Count != 1) - { - return null; - } - - firstMovie.AdditionalParts = result.Stacks[0].Files.Skip(1).ToList(); - firstMovie.Name = result.Stacks[0].Name; - - // They must all be part of the sequence if we're going to consider it a multi-part movie - return firstMovie; - } - - private T GetMovieWithMultipleSources<T>(IEnumerable<T> movies) - where T : Video, new() + private bool IsInvalid(Folder parent, string collectionType, IEnumerable<FileSystemInfo> files) { - var sortedMovies = movies.OrderBy(i => i.Path).ToList(); - - // Cap this at five to help avoid incorrect matching - if (sortedMovies.Count > 5) + if (parent != null) { - return null; + if (parent.IsRoot) + { + return true; + } } - var firstMovie = sortedMovies[0]; - - var filenamePrefix = Path.GetFileName(Path.GetDirectoryName(firstMovie.Path)); - - if (!string.IsNullOrWhiteSpace(filenamePrefix)) + // Don't do any resolving within a series structure + if (string.IsNullOrEmpty(collectionType)) { - if (sortedMovies.All(i => _fileSystem.GetFileNameWithoutExtension(i.Path).StartsWith(filenamePrefix + " - ", StringComparison.OrdinalIgnoreCase))) + if (parent is Season || parent is Series) { - firstMovie.LocalAlternateVersions = sortedMovies.Skip(1).Select(i => i.Path).ToList(); - - _logger.Debug("Multi-version video found: " + firstMovie.Path); + return true; + } - return firstMovie; + // Since the looping is expensive, this is an optimization to help us avoid it + if (files.Select(i => i.Name).Contains("series.xml", StringComparer.OrdinalIgnoreCase)) + { + return true; } } - return null; + var validCollectionTypes = new[] + { + string.Empty, + CollectionType.Movies, + CollectionType.HomeVideos, + CollectionType.MusicVideos, + CollectionType.BoxSets, + CollectionType.Movies + }; + + return !validCollectionTypes.Contains(collectionType ?? string.Empty, StringComparer.OrdinalIgnoreCase); } } } |
