diff options
Diffstat (limited to 'MediaBrowser.Server.Implementations/Library')
15 files changed, 801 insertions, 62 deletions
diff --git a/MediaBrowser.Server.Implementations/Library/CoreResolutionIgnoreRule.cs b/MediaBrowser.Server.Implementations/Library/CoreResolutionIgnoreRule.cs index d3d71e0f9..0fc04c504 100644 --- a/MediaBrowser.Server.Implementations/Library/CoreResolutionIgnoreRule.cs +++ b/MediaBrowser.Server.Implementations/Library/CoreResolutionIgnoreRule.cs @@ -14,10 +14,12 @@ namespace MediaBrowser.Server.Implementations.Library public class CoreResolutionIgnoreRule : IResolverIgnoreRule { private readonly IFileSystem _fileSystem; + private readonly ILibraryManager _libraryManager; - public CoreResolutionIgnoreRule(IFileSystem fileSystem) + public CoreResolutionIgnoreRule(IFileSystem fileSystem, ILibraryManager libraryManager) { _fileSystem = fileSystem; + _libraryManager = libraryManager; } /// <summary> @@ -89,7 +91,7 @@ namespace MediaBrowser.Server.Implementations.Library if (args.Parent != null) { // Don't resolve these into audio files - if (string.Equals(_fileSystem.GetFileNameWithoutExtension(filename), BaseItem.ThemeSongFilename) && EntityResolutionHelper.IsAudioFile(filename)) + if (string.Equals(_fileSystem.GetFileNameWithoutExtension(filename), BaseItem.ThemeSongFilename) && _libraryManager.IsAudioFile(filename)) { return true; } diff --git a/MediaBrowser.Server.Implementations/Library/EntityResolutionHelper.cs b/MediaBrowser.Server.Implementations/Library/EntityResolutionHelper.cs new file mode 100644 index 000000000..fdb0b35a1 --- /dev/null +++ b/MediaBrowser.Server.Implementations/Library/EntityResolutionHelper.cs @@ -0,0 +1,104 @@ +using MediaBrowser.Common.IO; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; +using System; +using System.Collections.Generic; +using System.IO; + +namespace MediaBrowser.Server.Implementations.Library +{ + /// <summary> + /// Class EntityResolutionHelper + /// </summary> + public static class EntityResolutionHelper + { + /// <summary> + /// Any folder named in this list will be ignored - can be added to at runtime for extensibility + /// </summary> + public static readonly List<string> IgnoreFolders = new List<string> + { + "metadata", + "ps3_update", + "ps3_vprm", + "extrafanart", + "extrathumbs", + ".actors", + ".wd_tv" + + }; + + /// <summary> + /// Ensures DateCreated and DateModified have values + /// </summary> + /// <param name="fileSystem">The file system.</param> + /// <param name="item">The item.</param> + /// <param name="args">The args.</param> + /// <param name="includeCreationTime">if set to <c>true</c> [include creation time].</param> + 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 9f8712384..3ed682002 100644 --- a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs +++ b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs @@ -15,6 +15,10 @@ using MediaBrowser.Controller.Sorting; using MediaBrowser.Model.Configuration; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Logging; +using MediaBrowser.Naming.Audio; +using MediaBrowser.Naming.IO; +using MediaBrowser.Naming.Video; +using MediaBrowser.Server.Implementations.Library.Resolvers.TV; using MediaBrowser.Server.Implementations.Library.Validators; using MediaBrowser.Server.Implementations.ScheduledTasks; using MoreLinq; @@ -1628,5 +1632,49 @@ namespace MediaBrowser.Server.Implementations.Library return item; } + + public bool IsVideoFile(string path) + { + var parser = new VideoFileParser(new ExpandedVideoOptions(), new Naming.Logging.NullLogger()); + return parser.IsVideoFile(path); + } + + public bool IsAudioFile(string path) + { + var parser = new AudioFileParser(new AudioOptions()); + return parser.IsAudioFile(path); + } + + public bool IsMultiPartFile(string path) + { + var parser = new MultiPartParser(new ExpandedVideoOptions(), new Naming.Logging.NullLogger()); + return parser.Parse(path, FileInfoType.File).IsMultiPart; + } + + public bool IsMultiPartFolder(string path) + { + var parser = new MultiPartParser(new ExpandedVideoOptions(), new Naming.Logging.NullLogger()); + return parser.Parse(path, FileInfoType.Directory).IsMultiPart; + } + + public int? GetSeasonNumberFromPath(string path) + { + return SeriesResolver.GetSeasonNumberFromPath(path); + } + + public int? GetSeasonNumberFromEpisodeFile(string path) + { + return SeriesResolver.GetSeasonNumberFromEpisodeFile(path); + } + + public int? GetEndingEpisodeNumberFromFile(string path) + { + return SeriesResolver.GetEndingEpisodeNumberFromFile(path); + } + + public int? GetEpisodeNumberFromFile(string path, bool considerSeasonless) + { + return SeriesResolver.GetEpisodeNumberFromFile(path, considerSeasonless); + } } } diff --git a/MediaBrowser.Server.Implementations/Library/ResolverHelper.cs b/MediaBrowser.Server.Implementations/Library/ResolverHelper.cs index 10c9cf733..b9fbd6f8c 100644 --- a/MediaBrowser.Server.Implementations/Library/ResolverHelper.cs +++ b/MediaBrowser.Server.Implementations/Library/ResolverHelper.cs @@ -2,7 +2,6 @@ using MediaBrowser.Common.IO; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; -using MediaBrowser.Controller.Resolvers; using System; using System.IO; using System.Linq; @@ -63,14 +62,14 @@ namespace MediaBrowser.Server.Implementations.Library 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 = GetMBName(args.FileInfo.Name, (args.FileInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory); + item.Name = GetMbName(args.FileInfo.Name, (args.FileInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory); } } /// <summary> /// The MB name regex /// </summary> - private static readonly Regex MBNameRegex = new Regex(@"(\[.*?\])", RegexOptions.Compiled); + private static readonly Regex MbNameRegex = new Regex(@"(\[.*?\])", RegexOptions.Compiled); /// <summary> /// Strip out attribute items and return just the name we will use for items @@ -78,7 +77,7 @@ namespace MediaBrowser.Server.Implementations.Library /// <param name="path">Assumed to be a file or directory path</param> /// <param name="isDirectory">if set to <c>true</c> [is directory].</param> /// <returns>The cleaned name</returns> - private static string GetMBName(string path, bool isDirectory) + private static string GetMbName(string path, bool isDirectory) { //first just get the file or directory name var fn = isDirectory ? Path.GetFileName(path) : Path.GetFileNameWithoutExtension(path); @@ -89,8 +88,9 @@ namespace MediaBrowser.Server.Implementations.Library return fn; } - public static string StripBrackets(string inputString) { - var output = MBNameRegex.Replace(inputString, string.Empty).Trim(); + private static string StripBrackets(string inputString) + { + var output = MbNameRegex.Replace(inputString, string.Empty).Trim(); return Regex.Replace(output, @"\s+", " "); } diff --git a/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/AudioResolver.cs b/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/AudioResolver.cs index dec6908f9..62eb1f47d 100644 --- a/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/AudioResolver.cs +++ b/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/AudioResolver.cs @@ -10,6 +10,13 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Audio /// </summary> public class AudioResolver : ItemResolver<Controller.Entities.Audio.Audio> { + private readonly ILibraryManager _libraryManager; + + public AudioResolver(ILibraryManager libraryManager) + { + _libraryManager = libraryManager; + } + /// <summary> /// Gets the priority. /// </summary> @@ -30,15 +37,14 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Audio if (!args.IsDirectory) { - if (EntityResolutionHelper.IsAudioFile(args.Path)) + if (_libraryManager.IsAudioFile(args.Path)) { var collectionType = args.GetCollectionType(); var isStandalone = args.Parent == null; if (isStandalone || - string.Equals(collectionType, CollectionType.Music, StringComparison.OrdinalIgnoreCase) || - string.IsNullOrEmpty(collectionType)) + string.Equals(collectionType, CollectionType.Music, StringComparison.OrdinalIgnoreCase)) { return new Controller.Entities.Audio.Audio(); } diff --git a/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/MusicAlbumResolver.cs b/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/MusicAlbumResolver.cs index 1f9dc56f9..a50059461 100644 --- a/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/MusicAlbumResolver.cs +++ b/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/MusicAlbumResolver.cs @@ -8,6 +8,7 @@ using MediaBrowser.Controller.Providers; using MediaBrowser.Controller.Resolvers; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Logging; +using MediaBrowser.Naming.Audio; using System; using System.Collections.Generic; using System.IO; @@ -21,11 +22,13 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Audio { private readonly ILogger _logger; private readonly IFileSystem _fileSystem; + private readonly ILibraryManager _libraryManager; - public MusicAlbumResolver(ILogger logger, IFileSystem fileSystem) + public MusicAlbumResolver(ILogger logger, IFileSystem fileSystem, ILibraryManager libraryManager) { _logger = logger; _fileSystem = fileSystem; + _libraryManager = libraryManager; } /// <summary> @@ -68,7 +71,7 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Audio return null; } - return IsMusicAlbum(args, isMusicMediaFolder) ? new MusicAlbum() : null; + return IsMusicAlbum(args) ? new MusicAlbum() : null; } @@ -76,29 +79,29 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Audio /// Determine if the supplied file data points to a music album /// </summary> /// <param name="path">The path.</param> - /// <param name="isMusicMediaFolder">if set to <c>true</c> [is music media folder].</param> /// <param name="directoryService">The directory service.</param> /// <param name="logger">The logger.</param> /// <param name="fileSystem">The file system.</param> + /// <param name="libraryManager">The library manager.</param> /// <returns><c>true</c> if [is music album] [the specified data]; otherwise, <c>false</c>.</returns> - public static bool IsMusicAlbum(string path, bool isMusicMediaFolder, IDirectoryService directoryService, ILogger logger, IFileSystem fileSystem) + public static bool IsMusicAlbum(string path, IDirectoryService directoryService, ILogger logger, IFileSystem fileSystem, + ILibraryManager libraryManager) { - return ContainsMusic(directoryService.GetFileSystemEntries(path), isMusicMediaFolder, true, directoryService, logger, fileSystem); + return ContainsMusic(directoryService.GetFileSystemEntries(path), true, directoryService, logger, fileSystem, libraryManager); } /// <summary> /// Determine if the supplied resolve args should be considered a music album /// </summary> /// <param name="args">The args.</param> - /// <param name="isMusicMediaFolder">if set to <c>true</c> [is music media folder].</param> /// <returns><c>true</c> if [is music album] [the specified args]; otherwise, <c>false</c>.</returns> - private bool IsMusicAlbum(ItemResolveArgs args, bool isMusicMediaFolder) + private bool IsMusicAlbum(ItemResolveArgs args) { // Args points to an album if parent is an Artist folder or it directly contains music if (args.IsDirectory) { //if (args.Parent is MusicArtist) return true; //saves us from testing children twice - if (ContainsMusic(args.FileSystemChildren, isMusicMediaFolder, true, args.DirectoryService, _logger, _fileSystem)) return true; + if (ContainsMusic(args.FileSystemChildren, true, args.DirectoryService, _logger, _fileSystem, _libraryManager)) return true; } return false; @@ -108,41 +111,34 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Audio /// Determine if the supplied list contains what we should consider music /// </summary> /// <param name="list">The list.</param> - /// <param name="isMusicMediaFolder">if set to <c>true</c> [is music media folder].</param> /// <param name="allowSubfolders">if set to <c>true</c> [allow subfolders].</param> /// <param name="directoryService">The directory service.</param> /// <param name="logger">The logger.</param> /// <param name="fileSystem">The file system.</param> + /// <param name="libraryManager">The library manager.</param> /// <returns><c>true</c> if the specified list contains music; otherwise, <c>false</c>.</returns> private static bool ContainsMusic(IEnumerable<FileSystemInfo> list, - bool isMusicMediaFolder, bool allowSubfolders, IDirectoryService directoryService, ILogger logger, - IFileSystem fileSystem) + IFileSystem fileSystem, + ILibraryManager libraryManager) { - // If list contains at least 2 audio files or at least one and no video files consider it to contain music - var foundAudio = 0; - var discSubfolderCount = 0; foreach (var fileSystemInfo in list) { if ((fileSystemInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory) { - if (isMusicMediaFolder && allowSubfolders && IsAlbumSubfolder(fileSystemInfo, true, directoryService, logger, fileSystem)) + if (allowSubfolders && IsAlbumSubfolder(fileSystemInfo, directoryService, logger, fileSystem, libraryManager)) { discSubfolderCount++; } - if (!IsAdditionalSubfolderAllowed(fileSystemInfo)) - { - return false; - } } var fullName = fileSystemInfo.FullName; - if (EntityResolutionHelper.IsAudioFile(fullName)) + if (libraryManager.IsAudioFile(fullName)) { // Don't resolve these into audio files if (string.Equals(fileSystem.GetFileNameWithoutExtension(fullName), BaseItem.ThemeSongFilename)) @@ -150,22 +146,14 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Audio continue; } - foundAudio++; - } - else if (EntityResolutionHelper.IsVideoFile(fullName)) return false; - else if (EntityResolutionHelper.IsVideoPlaceHolder(fullName)) return false; - - if (foundAudio >= 2) - { return true; } } - // or a single audio file and no video files - return foundAudio > 0 || discSubfolderCount > 0; + return discSubfolderCount > 0; } - private static bool IsAlbumSubfolder(FileSystemInfo directory, bool isMusicMediaFolder, IDirectoryService directoryService, ILogger logger, IFileSystem fileSystem) + private static bool IsAlbumSubfolder(FileSystemInfo directory, IDirectoryService directoryService, ILogger logger, IFileSystem fileSystem, ILibraryManager libraryManager) { var path = directory.FullName; @@ -173,7 +161,7 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Audio { logger.Debug("Found multi-disc folder: " + path); - return ContainsMusic(directoryService.GetFileSystemEntries(path), isMusicMediaFolder, false, directoryService, logger, fileSystem); + return ContainsMusic(directoryService.GetFileSystemEntries(path), false, directoryService, logger, fileSystem, libraryManager); } return false; @@ -181,13 +169,20 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Audio public static bool IsMultiDiscFolder(string path) { - return EntityResolutionHelper.IsMultiDiscAlbumFolder(path); + return IsMultiDiscAlbumFolder(path); } - private static bool IsAdditionalSubfolderAllowed(FileSystemInfo directory) + /// <summary> + /// Determines whether [is multi disc album folder] [the specified path]. + /// </summary> + /// <param name="path">The path.</param> + /// <returns><c>true</c> if [is multi disc album folder] [the specified path]; otherwise, <c>false</c>.</returns> + private static bool IsMultiDiscAlbumFolder(string path) { - // Resolver will ignore them based on rules engine - return true; + var parser = new AlbumParser(new AudioOptions(), new Naming.Logging.NullLogger()); + var result = parser.ParseMultiPart(path); + + return result.IsMultiPart; } } } diff --git a/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/MusicArtistResolver.cs b/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/MusicArtistResolver.cs index 2417d5dcb..53063b35e 100644 --- a/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/MusicArtistResolver.cs +++ b/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/MusicArtistResolver.cs @@ -19,11 +19,13 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Audio { private readonly ILogger _logger; private readonly IFileSystem _fileSystem; + private readonly ILibraryManager _libraryManager; - public MusicArtistResolver(ILogger logger, IFileSystem fileSystem) + public MusicArtistResolver(ILogger logger, IFileSystem fileSystem, ILibraryManager libraryManager) { _logger = logger; _fileSystem = fileSystem; + _libraryManager = libraryManager; } /// <summary> @@ -74,7 +76,7 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Audio var directoryService = args.DirectoryService; // If we contain an album assume we are an artist folder - return args.FileSystemChildren.Where(i => (i.Attributes & FileAttributes.Directory) == FileAttributes.Directory).Any(i => MusicAlbumResolver.IsMusicAlbum(i.FullName, isMusicMediaFolder, directoryService, _logger, _fileSystem)) ? new MusicArtist() : null; + return args.FileSystemChildren.Where(i => (i.Attributes & FileAttributes.Directory) == FileAttributes.Directory).Any(i => MusicAlbumResolver.IsMusicAlbum(i.FullName, directoryService, _logger, _fileSystem, _libraryManager)) ? new MusicArtist() : null; } } diff --git a/MediaBrowser.Server.Implementations/Library/Resolvers/BaseVideoResolver.cs b/MediaBrowser.Server.Implementations/Library/Resolvers/BaseVideoResolver.cs new file mode 100644 index 000000000..9ff1223b0 --- /dev/null +++ b/MediaBrowser.Server.Implementations/Library/Resolvers/BaseVideoResolver.cs @@ -0,0 +1,96 @@ +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; +using MediaBrowser.Model.Entities; +using MediaBrowser.Naming.Video; +using System; + +namespace MediaBrowser.Server.Implementations.Library.Resolvers +{ + /// <summary> + /// Resolves a Path into a Video or Video subclass + /// </summary> + /// <typeparam name="T"></typeparam> + public abstract class BaseVideoResolver<T> : Controller.Resolvers.ItemResolver<T> + where T : Video, new() + { + protected readonly ILibraryManager LibraryManager; + + protected BaseVideoResolver(ILibraryManager libraryManager) + { + LibraryManager = libraryManager; + } + + /// <summary> + /// Resolves the specified args. + /// </summary> + /// <param name="args">The args.</param> + /// <returns>`0.</returns> + protected override T Resolve(ItemResolveArgs args) + { + return ResolveVideo<T>(args); + } + + /// <summary> + /// Resolves the video. + /// </summary> + /// <typeparam name="TVideoType">The type of the T video type.</typeparam> + /// <param name="args">The args.</param> + /// <returns>``0.</returns> + protected TVideoType ResolveVideo<TVideoType>(ItemResolveArgs args) + where TVideoType : Video, new() + { + // If the path is a file check for a matching extensions + if (!args.IsDirectory) + { + var parser = new VideoFileParser(new ExpandedVideoOptions(), new Naming.Logging.NullLogger()); + var videoInfo = parser.ParseFile(args.Path); + + if (videoInfo == null) + { + return null; + } + + var isShortcut = string.Equals(videoInfo.Container, "strm", StringComparison.OrdinalIgnoreCase); + + if (LibraryManager.IsVideoFile(args.Path) || videoInfo.IsStub || isShortcut) + { + 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, + Name = videoInfo.Name + }; + + 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; + } + } + + return video; + } + } + + return null; + } + } +} diff --git a/MediaBrowser.Server.Implementations/Library/Resolvers/LocalTrailerResolver.cs b/MediaBrowser.Server.Implementations/Library/Resolvers/LocalTrailerResolver.cs index 662a9e87c..dfeefb74f 100644 --- a/MediaBrowser.Server.Implementations/Library/Resolvers/LocalTrailerResolver.cs +++ b/MediaBrowser.Server.Implementations/Library/Resolvers/LocalTrailerResolver.cs @@ -16,7 +16,7 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers { private readonly IFileSystem _fileSystem; - public LocalTrailerResolver(IFileSystem fileSystem) + public LocalTrailerResolver(ILibraryManager libraryManager, IFileSystem fileSystem) : base(libraryManager) { _fileSystem = fileSystem; } diff --git a/MediaBrowser.Server.Implementations/Library/Resolvers/Movies/MovieResolver.cs b/MediaBrowser.Server.Implementations/Library/Resolvers/Movies/MovieResolver.cs index 231b807d8..b7a414b9d 100644 --- a/MediaBrowser.Server.Implementations/Library/Resolvers/Movies/MovieResolver.cs +++ b/MediaBrowser.Server.Implementations/Library/Resolvers/Movies/MovieResolver.cs @@ -21,14 +21,12 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Movies public class MovieResolver : BaseVideoResolver<Video> { private readonly IServerApplicationPaths _applicationPaths; - private readonly ILibraryManager _libraryManager; private readonly ILogger _logger; private readonly IFileSystem _fileSystem; - public MovieResolver(IServerApplicationPaths appPaths, ILibraryManager libraryManager, ILogger logger, IFileSystem fileSystem) + public MovieResolver(ILibraryManager libraryManager, IServerApplicationPaths applicationPaths, ILogger logger, IFileSystem fileSystem) : base(libraryManager) { - _applicationPaths = appPaths; - _libraryManager = libraryManager; + _applicationPaths = applicationPaths; _logger = logger; _fileSystem = fileSystem; } @@ -221,7 +219,7 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Movies }; } - if (EntityResolutionHelper.IsMultiPartFolder(filename)) + if (LibraryManager.IsMultiPartFolder(filename)) { multiDiscFolders.Add(child); } @@ -235,7 +233,7 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Movies continue; } - var childArgs = new ItemResolveArgs(_applicationPaths, _libraryManager, directoryService) + var childArgs = new ItemResolveArgs(_applicationPaths, LibraryManager, directoryService) { FileInfo = child, Path = child.FullName, @@ -378,7 +376,7 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Movies var firstMovie = sortedMovies[0]; // They must all be part of the sequence if we're going to consider it a multi-part movie - if (sortedMovies.All(i => EntityResolutionHelper.IsMultiPartFile(i.Path))) + if (sortedMovies.All(i => LibraryManager.IsMultiPartFile(i.Path))) { // Only support up to 8 (matches Plex), to help avoid incorrect detection if (sortedMovies.Count <= 8) diff --git a/MediaBrowser.Server.Implementations/Library/Resolvers/TV/EpisodeResolver.cs b/MediaBrowser.Server.Implementations/Library/Resolvers/TV/EpisodeResolver.cs index 5b9e73996..3082872fd 100644 --- a/MediaBrowser.Server.Implementations/Library/Resolvers/TV/EpisodeResolver.cs +++ b/MediaBrowser.Server.Implementations/Library/Resolvers/TV/EpisodeResolver.cs @@ -11,6 +11,10 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.TV /// </summary> public class EpisodeResolver : BaseVideoResolver<Episode> { + public EpisodeResolver(ILibraryManager libraryManager) : base(libraryManager) + { + } + /// <summary> /// Resolves the specified args. /// </summary> @@ -73,7 +77,7 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.TV if (episode.ParentIndexNumber == null) { - episode.ParentIndexNumber = TVUtils.GetSeasonNumberFromEpisodeFile(args.Path); + episode.ParentIndexNumber = SeriesResolver.GetSeasonNumberFromEpisodeFile(args.Path); } } diff --git a/MediaBrowser.Server.Implementations/Library/Resolvers/TV/SeasonResolver.cs b/MediaBrowser.Server.Implementations/Library/Resolvers/TV/SeasonResolver.cs index 70280d8d3..d3580aaf8 100644 --- a/MediaBrowser.Server.Implementations/Library/Resolvers/TV/SeasonResolver.cs +++ b/MediaBrowser.Server.Implementations/Library/Resolvers/TV/SeasonResolver.cs @@ -34,9 +34,9 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.TV { var season = new Season { - IndexNumber = TVUtils.GetSeasonNumberFromPath(args.Path) + IndexNumber = SeriesResolver.GetSeasonNumberFromPath(args.Path) }; - + if (season.IndexNumber.HasValue && season.IndexNumber.Value == 0) { season.Name = _config.Configuration.SeasonZeroDisplayName; diff --git a/MediaBrowser.Server.Implementations/Library/Resolvers/TV/SeriesResolver.cs b/MediaBrowser.Server.Implementations/Library/Resolvers/TV/SeriesResolver.cs index d3aad582a..30eaf3198 100644 --- a/MediaBrowser.Server.Implementations/Library/Resolvers/TV/SeriesResolver.cs +++ b/MediaBrowser.Server.Implementations/Library/Resolvers/TV/SeriesResolver.cs @@ -1,13 +1,19 @@ using MediaBrowser.Common.Extensions; using MediaBrowser.Common.IO; +using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Audio; 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 System; +using System.Collections.Generic; +using System.Globalization; using System.IO; +using System.Linq; +using System.Text.RegularExpressions; namespace MediaBrowser.Server.Implementations.Library.Resolvers.TV { @@ -18,11 +24,13 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.TV { private readonly IFileSystem _fileSystem; private readonly ILogger _logger; + private readonly ILibraryManager _libraryManager; - public SeriesResolver(IFileSystem fileSystem, ILogger logger) + public SeriesResolver(IFileSystem fileSystem, ILogger logger, ILibraryManager libraryManager) { _fileSystem = fileSystem; _logger = logger; + _libraryManager = libraryManager; } /// <summary> @@ -71,7 +79,7 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.TV return null; } - if (TVUtils.IsSeriesFolder(args.Path, isTvShowsFolder, args.FileSystemChildren, args.DirectoryService, _fileSystem, _logger)) + if (IsSeriesFolder(args.Path, isTvShowsFolder, args.FileSystemChildren, args.DirectoryService, _fileSystem, _logger, _libraryManager)) { return new Series(); } @@ -81,6 +89,454 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.TV } /// <summary> + /// Determines whether [is series folder] [the specified path]. + /// </summary> + /// <param name="path">The path.</param> + /// <param name="considerSeasonlessEntries">if set to <c>true</c> [consider seasonless entries].</param> + /// <param name="fileSystemChildren">The file system children.</param> + /// <param name="directoryService">The directory service.</param> + /// <param name="fileSystem">The file system.</param> + /// <returns><c>true</c> if [is series folder] [the specified path]; otherwise, <c>false</c>.</returns> + public static bool IsSeriesFolder(string path, bool considerSeasonlessEntries, IEnumerable<FileSystemInfo> fileSystemChildren, IDirectoryService directoryService, IFileSystem fileSystem, ILogger logger, ILibraryManager libraryManager) + { + // A folder with more than 3 non-season folders in will not becounted as a series + var nonSeriesFolders = 0; + + foreach (var child in fileSystemChildren) + { + var attributes = child.Attributes; + + if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden) + { + //logger.Debug("Igoring series file or folder marked hidden: {0}", child.FullName); + continue; + } + + // Can't enforce this because files saved by Bitcasa are always marked System + //if ((attributes & FileAttributes.System) == FileAttributes.System) + //{ + // logger.Debug("Igoring series subfolder marked system: {0}", child.FullName); + // continue; + //} + + if ((attributes & FileAttributes.Directory) == FileAttributes.Directory) + { + if (IsSeasonFolder(child.FullName, directoryService, fileSystem)) + { + //logger.Debug("{0} is a series because of season folder {1}.", path, child.FullName); + return true; + } + + if (IsBadFolder(child.Name)) + { + logger.Debug("Invalid folder under series: {0}", child.FullName); + + nonSeriesFolders++; + } + + if (nonSeriesFolders >= 3) + { + logger.Debug("{0} not a series due to 3 or more invalid folders.", path); + return false; + } + } + else + { + var fullName = child.FullName; + + if (libraryManager.IsVideoFile(fullName) || IsVideoPlaceHolder(fullName)) + { + if (GetEpisodeNumberFromFile(fullName, considerSeasonlessEntries).HasValue) + { + return true; + } + } + } + } + + logger.Debug("{0} is not a series folder.", path); + return false; + } + + /// <summary> + /// Determines whether [is place holder] [the specified path]. + /// </summary> + /// <param name="path">The path.</param> + /// <returns><c>true</c> if [is place holder] [the specified path]; otherwise, <c>false</c>.</returns> + /// <exception cref="System.ArgumentNullException">path</exception> + private static bool IsVideoPlaceHolder(string path) + { + if (string.IsNullOrEmpty(path)) + { + throw new ArgumentNullException("path"); + } + + var extension = Path.GetExtension(path); + + return string.Equals(extension, ".disc", StringComparison.OrdinalIgnoreCase); + } + + private static bool IsBadFolder(string name) + { + if (string.Equals(name, BaseItem.ThemeSongsFolderName, StringComparison.OrdinalIgnoreCase)) + { + return false; + } + if (string.Equals(name, BaseItem.ThemeVideosFolderName, StringComparison.OrdinalIgnoreCase)) + { + return false; + } + if (string.Equals(name, BaseItem.TrailerFolderName, StringComparison.OrdinalIgnoreCase)) + { + return false; + } + + return !EntityResolutionHelper.IgnoreFolders.Contains(name, StringComparer.OrdinalIgnoreCase); + } + + /// <summary> + /// Determines whether [is season folder] [the specified path]. + /// </summary> + /// <param name="path">The path.</param> + /// <param name="directoryService">The directory service.</param> + /// <param name="fileSystem">The file system.</param> + /// <returns><c>true</c> if [is season folder] [the specified path]; otherwise, <c>false</c>.</returns> + private static bool IsSeasonFolder(string path, IDirectoryService directoryService, IFileSystem fileSystem) + { + var seasonNumber = GetSeasonNumberFromPath(path); + var hasSeasonNumber = seasonNumber != null; + + if (!hasSeasonNumber) + { + return false; + } + + //// It's a season folder if it's named as such and does not contain any audio files, apart from theme.mp3 + //foreach (var fileSystemInfo in directoryService.GetFileSystemEntries(path)) + //{ + // var attributes = fileSystemInfo.Attributes; + + // if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden) + // { + // continue; + // } + + // // Can't enforce this because files saved by Bitcasa are always marked System + // //if ((attributes & FileAttributes.System) == FileAttributes.System) + // //{ + // // continue; + // //} + + // if ((attributes & FileAttributes.Directory) == FileAttributes.Directory) + // { + // //if (IsBadFolder(fileSystemInfo.Name)) + // //{ + // // return false; + // //} + // } + // else + // { + // if (EntityResolutionHelper.IsAudioFile(fileSystemInfo.FullName) && + // !string.Equals(fileSystem.GetFileNameWithoutExtension(fileSystemInfo), BaseItem.ThemeSongFilename)) + // { + // return false; + // } + // } + //} + + return true; + } + + /// <summary> + /// A season folder must contain one of these somewhere in the name + /// </summary> + private static readonly string[] SeasonFolderNames = + { + "season", + "sæson", + "temporada", + "saison", + "staffel", + "series", + "сезон" + }; + + /// <summary> + /// Used to detect paths that represent episodes, need to make sure they don't also + /// match movie titles like "2001 A Space..." + /// Currently we limit the numbers here to 2 digits to try and avoid this + /// </summary> + private static readonly Regex[] EpisodeExpressions = + { + new Regex( + @".*(\\|\/)[sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3})[^\\\/]*$", + RegexOptions.Compiled), + new Regex( + @".*(\\|\/)[sS](?<seasonnumber>\d{1,4})[x,X]?[eE](?<epnumber>\d{1,3})[^\\\/]*$", + RegexOptions.Compiled), + new Regex( + @".*(\\|\/)(?<seriesname>((?![sS]?\d{1,4}[xX]\d{1,3})[^\\\/])*)?([sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3}))[^\\\/]*$", + RegexOptions.Compiled), + new Regex( + @".*(\\|\/)(?<seriesname>[^\\\/]*)[sS](?<seasonnumber>\d{1,4})[xX\.]?[eE](?<epnumber>\d{1,3})[^\\\/]*$", + RegexOptions.Compiled) + }; + private static readonly Regex[] MultipleEpisodeExpressions = + { + new Regex( + @".*(\\|\/)[sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3})((-| - )\d{1,4}[eExX](?<endingepnumber>\d{1,3}))+[^\\\/]*$", + RegexOptions.Compiled), + new Regex( + @".*(\\|\/)[sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3})((-| - )\d{1,4}[xX][eE](?<endingepnumber>\d{1,3}))+[^\\\/]*$", + RegexOptions.Compiled), + new Regex( + @".*(\\|\/)[sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3})((-| - )?[xXeE](?<endingepnumber>\d{1,3}))+[^\\\/]*$", + RegexOptions.Compiled), + new Regex( + @".*(\\|\/)[sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3})(-[xE]?[eE]?(?<endingepnumber>\d{1,3}))+[^\\\/]*$", + RegexOptions.Compiled), + new Regex( + @".*(\\|\/)(?<seriesname>((?![sS]?\d{1,4}[xX]\d{1,3})[^\\\/])*)?([sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3}))((-| - )\d{1,4}[xXeE](?<endingepnumber>\d{1,3}))+[^\\\/]*$", + RegexOptions.Compiled), + new Regex( + @".*(\\|\/)(?<seriesname>((?![sS]?\d{1,4}[xX]\d{1,3})[^\\\/])*)?([sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3}))((-| - )\d{1,4}[xX][eE](?<endingepnumber>\d{1,3}))+[^\\\/]*$", + RegexOptions.Compiled), + new Regex( + @".*(\\|\/)(?<seriesname>((?![sS]?\d{1,4}[xX]\d{1,3})[^\\\/])*)?([sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3}))((-| - )?[xXeE](?<endingepnumber>\d{1,3}))+[^\\\/]*$", + RegexOptions.Compiled), + new Regex( + @".*(\\|\/)(?<seriesname>((?![sS]?\d{1,4}[xX]\d{1,3})[^\\\/])*)?([sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3}))(-[xX]?[eE]?(?<endingepnumber>\d{1,3}))+[^\\\/]*$", + RegexOptions.Compiled), + new Regex( + @".*(\\|\/)(?<seriesname>[^\\\/]*)[sS](?<seasonnumber>\d{1,4})[xX\.]?[eE](?<epnumber>\d{1,3})((-| - )?[xXeE](?<endingepnumber>\d{1,3}))+[^\\\/]*$", + RegexOptions.Compiled), + new Regex( + @".*(\\|\/)(?<seriesname>[^\\\/]*)[sS](?<seasonnumber>\d{1,4})[xX\.]?[eE](?<epnumber>\d{1,3})(-[xX]?[eE]?(?<endingepnumber>\d{1,3}))+[^\\\/]*$", + RegexOptions.Compiled) + }; + + /// <summary> + /// To avoid the following matching movies they are only valid when contained in a folder which has been matched as a being season, or the media type is TV series + /// </summary> + private static readonly Regex[] EpisodeExpressionsWithoutSeason = + { + new Regex( + @".*[\\\/](?<epnumber>\d{1,3})(-(?<endingepnumber>\d{2,3}))*\.\w+$", + RegexOptions.Compiled), + // "01.avi" + new Regex( + @".*(\\|\/)(?<epnumber>\d{1,3})(-(?<endingepnumber>\d{2,3}))*\s?-\s?[^\\\/]*$", + RegexOptions.Compiled), + // "01 - blah.avi", "01-blah.avi" + new Regex( + @".*(\\|\/)(?<epnumber>\d{1,3})(-(?<endingepnumber>\d{2,3}))*\.[^\\\/]+$", + RegexOptions.Compiled), + // "01.blah.avi" + new Regex( + @".*[\\\/][^\\\/]* - (?<epnumber>\d{1,3})(-(?<endingepnumber>\d{2,3}))*[^\\\/]*$", + RegexOptions.Compiled), + // "blah - 01.avi", "blah 2 - 01.avi", "blah - 01 blah.avi", "blah 2 - 01 blah", "blah - 01 - blah.avi", "blah 2 - 01 - blah" + }; + + /// <summary> + /// Gets the season number from path. + /// </summary> + /// <param name="path">The path.</param> + /// <returns>System.Nullable{System.Int32}.</returns> + public static int? GetSeasonNumberFromPath(string path) + { + var filename = Path.GetFileName(path); + + if (string.Equals(filename, "specials", StringComparison.OrdinalIgnoreCase)) + { + return 0; + } + + int val; + if (int.TryParse(filename, NumberStyles.Integer, CultureInfo.InvariantCulture, out val)) + { + return val; + } + + if (filename.StartsWith("s", StringComparison.OrdinalIgnoreCase)) + { + var testFilename = filename.Substring(1); + + if (int.TryParse(testFilename, NumberStyles.Integer, CultureInfo.InvariantCulture, out val)) + { + return val; + } + } + + // Look for one of the season folder names + foreach (var name in SeasonFolderNames) + { + var index = filename.IndexOf(name, StringComparison.OrdinalIgnoreCase); + + if (index != -1) + { + return GetSeasonNumberFromPathSubstring(filename.Substring(index + name.Length)); + } + } + + return null; + } + + /// <summary> + /// Extracts the season number from the second half of the Season folder name (everything after "Season", or "Staffel") + /// </summary> + /// <param name="path">The path.</param> + /// <returns>System.Nullable{System.Int32}.</returns> + private static int? GetSeasonNumberFromPathSubstring(string path) + { + var numericStart = -1; + var length = 0; + + // Find out where the numbers start, and then keep going until they end + for (var i = 0; i < path.Length; i++) + { + if (char.IsNumber(path, i)) + { + if (numericStart == -1) + { + numericStart = i; + } + length++; + } + else if (numericStart != -1) + { + break; + } + } + + if (numericStart == -1) + { + return null; + } + + return int.Parse(path.Substring(numericStart, length), CultureInfo.InvariantCulture); + } + + /// <summary> + /// Episodes the number from file. + /// </summary> + /// <param name="fullPath">The full path.</param> + /// <param name="considerSeasonlessNames">if set to <c>true</c> [is in season].</param> + /// <returns>System.String.</returns> + public static int? GetEpisodeNumberFromFile(string fullPath, bool considerSeasonlessNames) + { + string fl = fullPath.ToLower(); + foreach (var r in EpisodeExpressions) + { + Match m = r.Match(fl); + if (m.Success) + return ParseEpisodeNumber(m.Groups["epnumber"].Value); + } + if (considerSeasonlessNames) + { + var match = EpisodeExpressionsWithoutSeason.Select(r => r.Match(fl)) + .FirstOrDefault(m => m.Success); + + if (match != null) + { + return ParseEpisodeNumber(match.Groups["epnumber"].Value); + } + } + + return null; + } + + public static int? GetEndingEpisodeNumberFromFile(string fullPath) + { + var fl = fullPath.ToLower(); + foreach (var r in MultipleEpisodeExpressions) + { + var m = r.Match(fl); + if (m.Success && !string.IsNullOrEmpty(m.Groups["endingepnumber"].Value)) + return ParseEpisodeNumber(m.Groups["endingepnumber"].Value); + } + foreach (var r in EpisodeExpressionsWithoutSeason) + { + var m = r.Match(fl); + if (m.Success && !string.IsNullOrEmpty(m.Groups["endingepnumber"].Value)) + return ParseEpisodeNumber(m.Groups["endingepnumber"].Value); + } + return null; + } + + /// <summary> + /// Seasons the number from episode file. + /// </summary> + /// <param name="fullPath">The full path.</param> + /// <returns>System.String.</returns> + public static int? GetSeasonNumberFromEpisodeFile(string fullPath) + { + string fl = fullPath.ToLower(); + foreach (var r in EpisodeExpressions) + { + Match m = r.Match(fl); + if (m.Success) + { + Group g = m.Groups["seasonnumber"]; + if (g != null) + { + var val = g.Value; + + if (!string.IsNullOrWhiteSpace(val)) + { + int num; + + if (int.TryParse(val, NumberStyles.Integer, UsCulture, out num)) + { + return num; + } + } + } + return null; + } + } + return null; + } + + public static string GetSeriesNameFromEpisodeFile(string fullPath) + { + var fl = fullPath.ToLower(); + foreach (var r in EpisodeExpressions) + { + var m = r.Match(fl); + if (m.Success) + { + var g = m.Groups["seriesname"]; + if (g != null) + { + var val = g.Value; + + if (!string.IsNullOrWhiteSpace(val)) + { + return val; + } + } + return null; + } + } + return null; + } + + private static readonly CultureInfo UsCulture = new CultureInfo("en-US"); + + private static int? ParseEpisodeNumber(string val) + { + int num; + + if (!string.IsNullOrEmpty(val) && int.TryParse(val, NumberStyles.Integer, UsCulture, out num)) + { + return num; + } + + return null; + } + + /// <summary> /// Sets the initial item values. /// </summary> /// <param name="item">The item.</param> diff --git a/MediaBrowser.Server.Implementations/Library/Resolvers/VideoResolver.cs b/MediaBrowser.Server.Implementations/Library/Resolvers/VideoResolver.cs index 391a3d948..cde75aea2 100644 --- a/MediaBrowser.Server.Implementations/Library/Resolvers/VideoResolver.cs +++ b/MediaBrowser.Server.Implementations/Library/Resolvers/VideoResolver.cs @@ -1,5 +1,9 @@ using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Resolvers; +using MediaBrowser.Model.Entities; +using System; +using System.Linq; namespace MediaBrowser.Server.Implementations.Library.Resolvers { @@ -8,6 +12,31 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers /// </summary> public class VideoResolver : BaseVideoResolver<Video> { + public VideoResolver(ILibraryManager libraryManager) + : base(libraryManager) + { + } + + protected override Video Resolve(ItemResolveArgs args) + { + if (args.Parent != null) + { + var collectionType = args.GetCollectionType() ?? string.Empty; + var accepted = new[] + { + string.Empty, + CollectionType.HomeVideos + }; + + if (!accepted.Contains(collectionType, StringComparer.OrdinalIgnoreCase)) + { + return null; + } + } + + return base.Resolve(args); + } + /// <summary> /// Gets the priority. /// </summary> diff --git a/MediaBrowser.Server.Implementations/Library/UserManager.cs b/MediaBrowser.Server.Implementations/Library/UserManager.cs index 803fbd454..54c584d47 100644 --- a/MediaBrowser.Server.Implementations/Library/UserManager.cs +++ b/MediaBrowser.Server.Implementations/Library/UserManager.cs @@ -19,7 +19,6 @@ using MediaBrowser.Model.Events; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Serialization; using MediaBrowser.Model.Users; -using MediaBrowser.Server.Implementations.Security; using System; using System.Collections.Generic; using System.Globalization; |
