diff --git a/MediaBrowser.Controller/IO/FileSystem.cs b/MediaBrowser.Controller/IO/FileSystem.cs index 28deca1009..7b031744e7 100644 --- a/MediaBrowser.Controller/IO/FileSystem.cs +++ b/MediaBrowser.Controller/IO/FileSystem.cs @@ -78,16 +78,6 @@ namespace MediaBrowser.Controller.IO return GetFileSystemEntries(path, searchPattern, includeDirectories: false); } - /// - /// Gets all sub-directories within a folder - /// - /// The path. - /// IEnumerable{WIN32_FIND_DATA}. - public static IEnumerable GetDirectories(string path) - { - return GetFileSystemEntries(path, includeFiles: false); - } - /// /// Gets all file system entries within a foler /// diff --git a/MediaBrowser.Controller/Providers/ImagesByNameProvider.cs b/MediaBrowser.Controller/Providers/ImagesByNameProvider.cs index 7b7be660d6..26ef984637 100644 --- a/MediaBrowser.Controller/Providers/ImagesByNameProvider.cs +++ b/MediaBrowser.Controller/Providers/ImagesByNameProvider.cs @@ -1,13 +1,12 @@ -using System.Threading; -using System.Threading.Tasks; -using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.IO; +using MediaBrowser.Model.Logging; using System; -using System.Globalization; using System.IO; using System.Linq; -using MediaBrowser.Model.Logging; +using System.Threading; +using System.Threading.Tasks; namespace MediaBrowser.Controller.Providers { @@ -16,7 +15,8 @@ namespace MediaBrowser.Controller.Providers /// public class ImagesByNameProvider : ImageFromMediaLocationProvider { - public ImagesByNameProvider(ILogManager logManager, IServerConfigurationManager configurationManager) : base(logManager, configurationManager) + public ImagesByNameProvider(ILogManager logManager, IServerConfigurationManager configurationManager) + : base(logManager, configurationManager) { } @@ -88,14 +88,61 @@ namespace MediaBrowser.Controller.Providers return DateTime.MinValue; } - var files = FileSystem.GetFiles(location).ToList(); + var files = new DirectoryInfo(location).EnumerateFiles("*", SearchOption.TopDirectoryOnly).ToList(); if (files.Count == 0) { return DateTime.MinValue; } - return files.Select(f => f.CreationTimeUtc > f.LastWriteTimeUtc ? f.CreationTimeUtc : f.LastWriteTimeUtc).Max(); + return files.Select(f => + { + var lastWriteTime = GetLastWriteTimeUtc(f); + var creationTime = GetCreationTimeUtc(f); + + return creationTime > lastWriteTime ? creationTime : lastWriteTime; + + }).Max(); + } + + /// + /// Gets the creation time UTC. + /// + /// The info. + /// DateTime. + private DateTime GetLastWriteTimeUtc(FileSystemInfo info) + { + // This could throw an error on some file systems that have dates out of range + + try + { + return info.LastAccessTimeUtc; + } + catch (Exception ex) + { + Logger.ErrorException("Error determining LastAccessTimeUtc for {0}", ex, info.FullName); + return DateTime.MinValue; + } + } + + /// + /// Gets the creation time UTC. + /// + /// The info. + /// DateTime. + private DateTime GetCreationTimeUtc(FileSystemInfo info) + { + // This could throw an error on some file systems that have dates out of range + + try + { + return info.CreationTimeUtc; + } + catch (Exception ex) + { + Logger.ErrorException("Error determining CreationTimeUtc for {0}", ex, info.FullName); + return DateTime.MinValue; + } } /// diff --git a/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/AudioResolver.cs b/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/AudioResolver.cs index 42370c17fe..b19ec2815c 100644 --- a/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/AudioResolver.cs +++ b/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/AudioResolver.cs @@ -1,9 +1,8 @@ -using MediaBrowser.Controller.IO; -using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Resolvers; using System; using System.IO; using System.Linq; -using MediaBrowser.Controller.Resolvers; namespace MediaBrowser.Server.Implementations.Library.Resolvers.Audio { @@ -32,7 +31,7 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Audio if (!args.IsDirectory) { - if (IsAudioFile(args)) + if (IsAudioFile(args.Path)) { return new Controller.Entities.Audio.Audio(); } @@ -61,21 +60,11 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Audio /// /// Determines whether [is audio file] [the specified args]. /// - /// The args. + /// The path. /// true if [is audio file] [the specified args]; otherwise, false. - public static bool IsAudioFile(ItemResolveArgs args) - { - return AudioFileExtensions.Contains(Path.GetExtension(args.Path), StringComparer.OrdinalIgnoreCase); - } - - /// - /// Determines whether [is audio file] [the specified file]. - /// - /// The file. - /// true if [is audio file] [the specified file]; otherwise, false. - public static bool IsAudioFile(WIN32_FIND_DATA file) + public static bool IsAudioFile(string path) { - return AudioFileExtensions.Contains(Path.GetExtension(file.Path), StringComparer.OrdinalIgnoreCase); + return AudioFileExtensions.Contains(Path.GetExtension(path), StringComparer.OrdinalIgnoreCase); } } } diff --git a/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/MusicAlbumResolver.cs b/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/MusicAlbumResolver.cs index 6ca4338a41..7d3dab1b1d 100644 --- a/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/MusicAlbumResolver.cs +++ b/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/MusicAlbumResolver.cs @@ -1,4 +1,6 @@ using System.Collections.Generic; +using System.IO; +using System.Linq; using MediaBrowser.Controller.Entities.Audio; using MediaBrowser.Controller.IO; using MediaBrowser.Controller.Library; @@ -40,15 +42,29 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Audio /// /// Determine if the supplied file data points to a music album /// - /// The data. + /// The path. /// true if [is music album] [the specified data]; otherwise, false. - public static bool IsMusicAlbum(WIN32_FIND_DATA data) + public static bool IsMusicAlbum(string path) { - return ContainsMusic(FileSystem.GetFiles(data.Path)); + // If list contains at least 2 audio files or at least one and no video files consider it to contain music + var foundAudio = 0; + + foreach (var fullName in new DirectoryInfo(path).EnumerateFiles("*", SearchOption.TopDirectoryOnly).Select(file => file.FullName)) + { + if (AudioResolver.IsAudioFile(fullName)) foundAudio++; + if (foundAudio >= 2) + { + return true; + } + if (EntityResolutionHelper.IsVideoFile(fullName)) return false; + } + + // or a single audio file and no video files + return foundAudio > 0; } /// - /// Determine if the supplied reslove args should be considered a music album + /// Determine if the supplied resolve args should be considered a music album /// /// The args. /// true if [is music album] [the specified args]; otherwise, false. @@ -74,20 +90,19 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Audio { // 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 foundVideo = 0; + foreach (var file in list) { - if (AudioResolver.IsAudioFile(file)) foundAudio++; + if (AudioResolver.IsAudioFile(file.Path)) foundAudio++; if (foundAudio >= 2) { return true; } - if (EntityResolutionHelper.IsVideoFile(file.Path)) foundVideo++; + if (EntityResolutionHelper.IsVideoFile(file.Path)) return false; } // or a single audio file and no video files - if (foundAudio > 0 && foundVideo == 0) return true; - return false; + return foundAudio > 0; } } } diff --git a/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/MusicArtistResolver.cs b/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/MusicArtistResolver.cs index 78f4b5a271..7ba6df68c1 100644 --- a/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/MusicArtistResolver.cs +++ b/MediaBrowser.Server.Implementations/Library/Resolvers/Audio/MusicArtistResolver.cs @@ -1,7 +1,7 @@ using MediaBrowser.Controller.Entities.Audio; using MediaBrowser.Controller.Library; -using System.Linq; using MediaBrowser.Controller.Resolvers; +using System.Linq; namespace MediaBrowser.Server.Implementations.Library.Resolvers.Audio { @@ -33,7 +33,7 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Audio if (args.Parent.IsRoot) return null; // If we contain an album assume we are an artist folder - return args.FileSystemChildren.Any(MusicAlbumResolver.IsMusicAlbum) ? new MusicArtist() : null; + return args.FileSystemChildren.Any(i => MusicAlbumResolver.IsMusicAlbum(i.Path)) ? new MusicArtist() : null; } }