using MediaBrowser.Model.Logging; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; namespace MediaBrowser.Controller.Providers { public interface IDirectoryService { IEnumerable GetFileSystemEntries(string path); IEnumerable GetFiles(string path); IEnumerable GetDirectories(string path); IEnumerable GetFiles(string path, bool clearCache); FileSystemInfo GetFile(string path); Dictionary GetFileSystemDictionary(string path); } public class DirectoryService : IDirectoryService { private readonly ILogger _logger; private readonly ConcurrentDictionary> _cache = new ConcurrentDictionary>(StringComparer.OrdinalIgnoreCase); public DirectoryService(ILogger logger) { _logger = logger; } public DirectoryService() : this(new NullLogger()) { } public IEnumerable GetFileSystemEntries(string path) { return GetFileSystemEntries(path, false); } public Dictionary GetFileSystemDictionary(string path) { return GetFileSystemDictionary(path, false); } private Dictionary GetFileSystemDictionary(string path, bool clearCache) { Dictionary entries; if (clearCache) { Dictionary removed; _cache.TryRemove(path, out removed); } if (!_cache.TryGetValue(path, out entries)) { //_logger.Debug("Getting files for " + path); entries = new Dictionary(StringComparer.OrdinalIgnoreCase); try { var list = new DirectoryInfo(path) .EnumerateFileSystemInfos("*", SearchOption.TopDirectoryOnly); // Seeing dupes on some users file system for some reason foreach (var item in list) { entries[item.FullName] = item; } } catch (DirectoryNotFoundException) { } //var group = entries.ToLookup(i => Path.GetDirectoryName(i.FullName)).ToList(); _cache.TryAdd(path, entries); } return entries; } private IEnumerable GetFileSystemEntries(string path, bool clearCache) { return GetFileSystemDictionary(path, clearCache).Values; } public IEnumerable GetFiles(string path) { return GetFiles(path, false); } public IEnumerable GetFiles(string path, bool clearCache) { return GetFileSystemEntries(path, clearCache).Where(i => (i.Attributes & FileAttributes.Directory) != FileAttributes.Directory); } public FileSystemInfo GetFile(string path) { var directory = Path.GetDirectoryName(path); var dict = GetFileSystemDictionary(directory, false); FileSystemInfo entry; dict.TryGetValue(path, out entry); return entry; } public IEnumerable GetDirectories(string path) { return GetFileSystemEntries(path, false).Where(i => (i.Attributes & FileAttributes.Directory) == FileAttributes.Directory); } } }