From 5eb44c42c586af34dd16efc76240d0d6c8e02069 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 4 Dec 2014 00:24:41 -0500 Subject: resolve mixed folder detection --- .../Library/CoreResolutionIgnoreRule.cs | 5 - .../Library/EntityResolutionHelper.cs | 74 ----- .../Library/LibraryManager.cs | 106 +++++--- .../Library/ResolverHelper.cs | 134 ++++++++- .../Library/Resolvers/BaseVideoResolver.cs | 124 +++++---- .../Library/Resolvers/Movies/BoxSetResolver.cs | 6 +- .../Library/Resolvers/Movies/MovieResolver.cs | 301 +++++++++++++-------- .../Library/Resolvers/PhotoResolver.cs | 4 +- .../Library/Resolvers/TV/EpisodeResolver.cs | 9 +- .../Library/Resolvers/TV/SeriesResolver.cs | 5 +- .../Library/Resolvers/VideoResolver.cs | 22 +- 11 files changed, 456 insertions(+), 334 deletions(-) (limited to 'MediaBrowser.Server.Implementations/Library') diff --git a/MediaBrowser.Server.Implementations/Library/CoreResolutionIgnoreRule.cs b/MediaBrowser.Server.Implementations/Library/CoreResolutionIgnoreRule.cs index 0fc04c504..3768b4492 100644 --- a/MediaBrowser.Server.Implementations/Library/CoreResolutionIgnoreRule.cs +++ b/MediaBrowser.Server.Implementations/Library/CoreResolutionIgnoreRule.cs @@ -95,11 +95,6 @@ namespace MediaBrowser.Server.Implementations.Library { return true; } - - if (BaseItem.ExtraSuffixes.Any(i => filename.IndexOf(i.Key, StringComparison.OrdinalIgnoreCase) != -1)) - { - return true; - } } // Ignore samples diff --git a/MediaBrowser.Server.Implementations/Library/EntityResolutionHelper.cs b/MediaBrowser.Server.Implementations/Library/EntityResolutionHelper.cs index fdb0b35a1..2a8c4f7f3 100644 --- a/MediaBrowser.Server.Implementations/Library/EntityResolutionHelper.cs +++ b/MediaBrowser.Server.Implementations/Library/EntityResolutionHelper.cs @@ -26,79 +26,5 @@ namespace MediaBrowser.Server.Implementations.Library ".wd_tv" }; - - /// - /// Ensures DateCreated and DateModified have values - /// - /// The file system. - /// The item. - /// The args. - /// if set to true [include creation time]. - public static void EnsureDates(IFileSystem fileSystem, BaseItem item, ItemResolveArgs args, bool includeCreationTime) - { - if (fileSystem == null) - { - throw new ArgumentNullException("fileSystem"); - } - if (item == null) - { - throw new ArgumentNullException("item"); - } - if (args == null) - { - throw new ArgumentNullException("args"); - } - - // See if a different path came out of the resolver than what went in - if (!string.Equals(args.Path, item.Path, StringComparison.OrdinalIgnoreCase)) - { - var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null; - - if (childData != null) - { - if (includeCreationTime) - { - SetDateCreated(item, fileSystem, childData); - } - - item.DateModified = fileSystem.GetLastWriteTimeUtc(childData); - } - else - { - var fileData = fileSystem.GetFileSystemInfo(item.Path); - - if (fileData.Exists) - { - if (includeCreationTime) - { - SetDateCreated(item, fileSystem, fileData); - } - item.DateModified = fileSystem.GetLastWriteTimeUtc(fileData); - } - } - } - else - { - if (includeCreationTime) - { - SetDateCreated(item, fileSystem, args.FileInfo); - } - item.DateModified = fileSystem.GetLastWriteTimeUtc(args.FileInfo); - } - } - - private static void SetDateCreated(BaseItem item, IFileSystem fileSystem, FileSystemInfo info) - { - var config = BaseItem.ConfigurationManager.GetMetadataConfiguration(); - - if (config.UseFileCreationTimeForDateAdded) - { - item.DateCreated = fileSystem.GetCreationTimeUtc(info); - } - else - { - item.DateCreated = DateTime.UtcNow; - } - } } } diff --git a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs index 9c3255833..e06720192 100644 --- a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs +++ b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs @@ -466,22 +466,10 @@ namespace MediaBrowser.Server.Implementations.Library /// /// The args. /// BaseItem. - public BaseItem ResolveItem(ItemResolveArgs args) + private BaseItem ResolveItem(ItemResolveArgs args) { - var item = EntityResolvers.Select(r => - { - try - { - return r.ResolvePath(args); - } - catch (Exception ex) - { - _logger.ErrorException("Error in {0} resolving {1}", ex, r.GetType().Name, args.Path); - - return null; - } - - }).FirstOrDefault(i => i != null); + var item = EntityResolvers.Select(r => Resolve(args, r)) + .FirstOrDefault(i => i != null); if (item != null) { @@ -491,6 +479,19 @@ namespace MediaBrowser.Server.Implementations.Library return item; } + private BaseItem Resolve(ItemResolveArgs args, IItemResolver resolver) + { + try + { + return resolver.ResolvePath(args); + } + catch (Exception ex) + { + _logger.ErrorException("Error in {0} resolving {1}", ex, resolver.GetType().Name, args.Path); + return null; + } + } + public Guid GetNewItemId(string key, Type type) { if (string.IsNullOrWhiteSpace(key)) @@ -565,7 +566,7 @@ namespace MediaBrowser.Server.Implementations.Library return ResolvePath(fileInfo, new DirectoryService(_logger), parent, collectionType); } - public BaseItem ResolvePath(FileSystemInfo fileInfo, IDirectoryService directoryService, Folder parent = null, string collectionType = null) + private BaseItem ResolvePath(FileSystemInfo fileInfo, IDirectoryService directoryService, Folder parent = null, string collectionType = null) { if (fileInfo == null) { @@ -645,23 +646,50 @@ namespace MediaBrowser.Server.Implementations.Library return !args.ContainsFileSystemEntryByName(".ignore"); } - public List ResolvePaths(IEnumerable files, IDirectoryService directoryService, Folder parent, string collectionType = null) - where T : BaseItem + public IEnumerable ResolvePaths(IEnumerable files, IDirectoryService directoryService, Folder parent, string collectionType) + { + var fileList = files.ToList(); + + if (parent != null) + { + var multiItemResolvers = EntityResolvers.OfType(); + + foreach (var resolver in multiItemResolvers) + { + var result = resolver.ResolveMultiple(parent, fileList, collectionType, directoryService); + + if (result != null && result.Items.Count > 0) + { + var items = new List(); + items.AddRange(result.Items); + + foreach (var item in items) + { + ResolverHelper.SetInitialItemValues(item, parent, _fileSystem, this, directoryService); + } + items.AddRange(ResolveFileList(result.ExtraFiles, directoryService, parent, collectionType)); + return items; + } + } + } + + return ResolveFileList(fileList, directoryService, parent, collectionType); + } + + private IEnumerable ResolveFileList(IEnumerable fileList, IDirectoryService directoryService, Folder parent, string collectionType) { - return files.Select(f => + return fileList.Select(f => { try { - return ResolvePath(f, directoryService, parent, collectionType) as T; + return ResolvePath(f, directoryService, parent, collectionType); } catch (Exception ex) { _logger.ErrorException("Error resolving path {0}", ex, f.FullName); return null; } - - }).Where(i => i != null) - .ToList(); + }).Where(i => i != null); } /// @@ -1724,7 +1752,9 @@ namespace MediaBrowser.Server.Implementations.Library files.AddRange(currentVideo.Extras.Where(i => string.Equals(i.ExtraType, "trailer", StringComparison.OrdinalIgnoreCase)).Select(i => new FileInfo(i.Path))); } - return ResolvePaths public static class ResolverHelper { + /// + /// Sets the initial item values. + /// + /// The item. + /// The parent. + /// The file system. + /// The library manager. + /// The directory service. + /// Item must have a path + public static void SetInitialItemValues(BaseItem item, Folder parent, IFileSystem fileSystem, ILibraryManager libraryManager, IDirectoryService directoryService) + { + // This version of the below method has no ItemResolveArgs, so we have to require the path already being set + if (string.IsNullOrWhiteSpace(item.Path)) + { + throw new ArgumentException("Item must have a Path"); + } + + // If the resolver didn't specify this + if (parent != null) + { + item.Parent = parent; + } + + item.Id = libraryManager.GetNewItemId(item.Path, item.GetType()); + + // If the resolver didn't specify this + if (string.IsNullOrEmpty(item.DisplayMediaType)) + { + item.DisplayMediaType = item.GetType().Name; + } + + item.IsLocked = item.Path.IndexOf("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) != -1 || + item.Parents.Any(i => i.IsLocked); + + // Make sure DateCreated and DateModified have values + var fileInfo = directoryService.GetFile(item.Path); + item.DateModified = fileSystem.GetLastWriteTimeUtc(fileInfo); + SetDateCreated(item, fileSystem, fileInfo); + + EnsureName(item, fileInfo); + } + /// /// Sets the initial item values. /// /// The item. /// The args. /// The file system. + /// The library manager. public static void SetInitialItemValues(BaseItem item, ItemResolveArgs args, IFileSystem fileSystem, ILibraryManager libraryManager) { // If the resolver didn't specify this @@ -42,27 +86,26 @@ namespace MediaBrowser.Server.Implementations.Library } // Make sure the item has a name - EnsureName(item, args); + EnsureName(item, args.FileInfo); item.IsLocked = item.Path.IndexOf("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) != -1 || item.Parents.Any(i => i.IsLocked); // Make sure DateCreated and DateModified have values - EntityResolutionHelper.EnsureDates(fileSystem, item, args, true); + EnsureDates(fileSystem, item, args, true); } /// /// Ensures the name. /// /// The item. - /// The arguments. - private static void EnsureName(BaseItem item, ItemResolveArgs args) + /// The file information. + private static void EnsureName(BaseItem item, FileSystemInfo fileInfo) { // If the subclass didn't supply a name, add it here if (string.IsNullOrEmpty(item.Name) && !string.IsNullOrEmpty(item.Path)) { - //we use our resolve args name here to get the name of the containg folder, not actual video file - item.Name = GetDisplayName(args.FileInfo.Name, (args.FileInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory); + item.Name = GetDisplayName(fileInfo.Name, (fileInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory); } } @@ -74,10 +117,7 @@ namespace MediaBrowser.Server.Implementations.Library /// System.String. private static string GetDisplayName(string path, bool isDirectory) { - //first just get the file or directory name - var fn = isDirectory ? Path.GetFileName(path) : Path.GetFileNameWithoutExtension(path); - - return fn; + return isDirectory ? Path.GetFileName(path) : Path.GetFileNameWithoutExtension(path); } /// @@ -90,5 +130,79 @@ namespace MediaBrowser.Server.Implementations.Library var output = MbNameRegex.Replace(inputString, string.Empty).Trim(); return Regex.Replace(output, @"\s+", " "); } + + /// + /// Ensures DateCreated and DateModified have values + /// + /// The file system. + /// The item. + /// The args. + /// if set to true [include creation time]. + private static void EnsureDates(IFileSystem fileSystem, BaseItem item, ItemResolveArgs args, bool includeCreationTime) + { + if (fileSystem == null) + { + throw new ArgumentNullException("fileSystem"); + } + if (item == null) + { + throw new ArgumentNullException("item"); + } + if (args == null) + { + throw new ArgumentNullException("args"); + } + + // See if a different path came out of the resolver than what went in + if (!string.Equals(args.Path, item.Path, StringComparison.OrdinalIgnoreCase)) + { + var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null; + + if (childData != null) + { + if (includeCreationTime) + { + SetDateCreated(item, fileSystem, childData); + } + + item.DateModified = fileSystem.GetLastWriteTimeUtc(childData); + } + else + { + var fileData = fileSystem.GetFileSystemInfo(item.Path); + + if (fileData.Exists) + { + if (includeCreationTime) + { + SetDateCreated(item, fileSystem, fileData); + } + item.DateModified = fileSystem.GetLastWriteTimeUtc(fileData); + } + } + } + else + { + if (includeCreationTime) + { + SetDateCreated(item, fileSystem, args.FileInfo); + } + item.DateModified = fileSystem.GetLastWriteTimeUtc(args.FileInfo); + } + } + + private static void SetDateCreated(BaseItem item, IFileSystem fileSystem, FileSystemInfo info) + { + var config = BaseItem.ConfigurationManager.GetMetadataConfiguration(); + + if (config.UseFileCreationTimeForDateAdded) + { + item.DateCreated = fileSystem.GetCreationTimeUtc(info); + } + else + { + item.DateCreated = DateTime.UtcNow; + } + } } } diff --git a/MediaBrowser.Server.Implementations/Library/Resolvers/BaseVideoResolver.cs b/MediaBrowser.Server.Implementations/Library/Resolvers/BaseVideoResolver.cs index f8cd209a1..a423ea3f1 100644 --- a/MediaBrowser.Server.Implementations/Library/Resolvers/BaseVideoResolver.cs +++ b/MediaBrowser.Server.Implementations/Library/Resolvers/BaseVideoResolver.cs @@ -1,10 +1,12 @@ using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Providers; using MediaBrowser.Model.Entities; using MediaBrowser.Naming.Common; using MediaBrowser.Naming.Video; using System; using System.IO; +using System.Linq; namespace MediaBrowser.Server.Implementations.Library.Resolvers { @@ -29,7 +31,7 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers /// `0. protected override T Resolve(ItemResolveArgs args) { - return ResolveVideo(args, true); + return ResolveVideo(args, false); } /// @@ -96,14 +98,9 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers if (video != null) { - if (parseName) - { - video.Name = videoInfo.Name; - } - else - { - video.Name = Path.GetFileName(args.Path); - } + video.Name = parseName ? + videoInfo.Name : + Path.GetFileName(args.Path); Set3DFormat(video, videoInfo); } @@ -119,50 +116,22 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers return null; } - var isShortcut = string.Equals(videoInfo.Container, "strm", StringComparison.OrdinalIgnoreCase); - - if (LibraryManager.IsVideoFile(args.Path) || videoInfo.IsStub || isShortcut) + if (LibraryManager.IsVideoFile(args.Path) || videoInfo.IsStub) { - var type = string.Equals(videoInfo.Container, "iso", StringComparison.OrdinalIgnoreCase) || string.Equals(videoInfo.Container, "img", StringComparison.OrdinalIgnoreCase) ? - VideoType.Iso : - VideoType.VideoFile; - var path = args.Path; var video = new TVideoType { - VideoType = type, Path = path, IsInMixedFolder = true, - IsPlaceHolder = videoInfo.IsStub, - IsShortcut = isShortcut, ProductionYear = videoInfo.Year }; - if (parseName) - { - video.Name = videoInfo.Name; - } - else - { - video.Name = Path.GetFileNameWithoutExtension(path); - } - - if (videoInfo.IsStub) - { - if (string.Equals(videoInfo.StubType, "dvd", StringComparison.OrdinalIgnoreCase)) - { - video.VideoType = VideoType.Dvd; - } - else if (string.Equals(videoInfo.StubType, "hddvd", StringComparison.OrdinalIgnoreCase)) - { - video.VideoType = VideoType.HdDvd; - } - else if (string.Equals(videoInfo.StubType, "bluray", StringComparison.OrdinalIgnoreCase)) - { - video.VideoType = VideoType.BluRay; - } - } + SetVideoType(video, videoInfo); + + video.Name = parseName ? + videoInfo.Name : + Path.GetFileNameWithoutExtension(args.Path); Set3DFormat(video, videoInfo); @@ -173,41 +142,82 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers return null; } - private void Set3DFormat(Video video, VideoFileInfo videoInfo) + protected void SetVideoType(Video video, VideoFileInfo videoInfo) { - if (videoInfo.Is3D) + var extension = Path.GetExtension(video.Path); + video.VideoType = string.Equals(extension, ".iso", StringComparison.OrdinalIgnoreCase) || + string.Equals(extension, ".img", StringComparison.OrdinalIgnoreCase) ? + VideoType.Iso : + VideoType.VideoFile; + + video.IsShortcut = string.Equals(extension, ".strm", StringComparison.OrdinalIgnoreCase); + video.IsPlaceHolder = videoInfo.IsStub; + + if (videoInfo.IsStub) { - if (string.Equals(videoInfo.Format3D, "fsbs", StringComparison.OrdinalIgnoreCase)) + if (string.Equals(videoInfo.StubType, "dvd", StringComparison.OrdinalIgnoreCase)) + { + video.VideoType = VideoType.Dvd; + } + else if (string.Equals(videoInfo.StubType, "hddvd", StringComparison.OrdinalIgnoreCase)) + { + video.VideoType = VideoType.HdDvd; + } + else if (string.Equals(videoInfo.StubType, "bluray", StringComparison.OrdinalIgnoreCase)) + { + video.VideoType = VideoType.BluRay; + } + } + } + + protected void Set3DFormat(Video video, bool is3D, string format3D) + { + if (is3D) + { + if (string.Equals(format3D, "fsbs", StringComparison.OrdinalIgnoreCase)) { video.Video3DFormat = Video3DFormat.FullSideBySide; } - else if (string.Equals(videoInfo.Format3D, "ftab", StringComparison.OrdinalIgnoreCase)) + else if (string.Equals(format3D, "ftab", StringComparison.OrdinalIgnoreCase)) { video.Video3DFormat = Video3DFormat.FullTopAndBottom; } - else if (string.Equals(videoInfo.Format3D, "hsbs", StringComparison.OrdinalIgnoreCase)) + else if (string.Equals(format3D, "hsbs", StringComparison.OrdinalIgnoreCase)) { video.Video3DFormat = Video3DFormat.HalfSideBySide; } - else if (string.Equals(videoInfo.Format3D, "htab", StringComparison.OrdinalIgnoreCase)) + else if (string.Equals(format3D, "htab", StringComparison.OrdinalIgnoreCase)) { video.Video3DFormat = Video3DFormat.HalfTopAndBottom; } - else if (string.Equals(videoInfo.Format3D, "sbs", StringComparison.OrdinalIgnoreCase)) + else if (string.Equals(format3D, "sbs", StringComparison.OrdinalIgnoreCase)) { video.Video3DFormat = Video3DFormat.HalfSideBySide; } - else if (string.Equals(videoInfo.Format3D, "sbs3d", StringComparison.OrdinalIgnoreCase)) + else if (string.Equals(format3D, "sbs3d", StringComparison.OrdinalIgnoreCase)) { video.Video3DFormat = Video3DFormat.HalfSideBySide; } - else if (string.Equals(videoInfo.Format3D, "tab", StringComparison.OrdinalIgnoreCase)) + else if (string.Equals(format3D, "tab", StringComparison.OrdinalIgnoreCase)) { video.Video3DFormat = Video3DFormat.HalfTopAndBottom; } } } + protected void Set3DFormat(Video video, VideoFileInfo videoInfo) + { + Set3DFormat(video, videoInfo.Is3D, videoInfo.Format3D); + } + + protected void Set3DFormat(Video video) + { + var resolver = new Format3DParser(new ExtendedNamingOptions(), new Naming.Logging.NullLogger()); + var result = resolver.Parse(video.Path); + + Set3DFormat(video, result.Is3D, result.Format3D); + } + /// /// Determines whether [is DVD directory] [the specified directory name]. /// @@ -227,5 +237,15 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers { return string.Equals(directoryName, "bdmv", StringComparison.OrdinalIgnoreCase); } + + protected bool IsBluRayContainer(string path, IDirectoryService directoryService) + { + return directoryService.GetDirectories(path).Any(i => IsBluRayDirectory(i.Name)); + } + + protected bool IsDvdContainer(string path, IDirectoryService directoryService) + { + return directoryService.GetDirectories(path).Any(i => IsDvdDirectory(i.Name)); + } } } diff --git a/MediaBrowser.Server.Implementations/Library/Resolvers/Movies/BoxSetResolver.cs b/MediaBrowser.Server.Implementations/Library/Resolvers/Movies/BoxSetResolver.cs index 7f9b16d95..cc261c3e7 100644 --- a/MediaBrowser.Server.Implementations/Library/Resolvers/Movies/BoxSetResolver.cs +++ b/MediaBrowser.Server.Implementations/Library/Resolvers/Movies/BoxSetResolver.cs @@ -1,5 +1,4 @@ -using MediaBrowser.Common.Extensions; -using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Movies; using MediaBrowser.Controller.Library; using MediaBrowser.Model.Entities; @@ -38,7 +37,8 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Movies return null; } - if (filename.IndexOf("[boxset]", StringComparison.OrdinalIgnoreCase) != -1 || args.ContainsFileSystemEntryByName("collection.xml")) + if (filename.IndexOf("[boxset]", StringComparison.OrdinalIgnoreCase) != -1 || + args.ContainsFileSystemEntryByName("collection.xml")) { return new BoxSet { diff --git a/MediaBrowser.Server.Implementations/Library/Resolvers/Movies/MovieResolver.cs b/MediaBrowser.Server.Implementations/Library/Resolvers/Movies/MovieResolver.cs index 6d396ad9c..f48b8329e 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 /// /// Class MovieResolver /// - public class MovieResolver : BaseVideoResolver