using System; using System.Collections.Generic; using System.IO; using System.Linq; using MediaBrowser.Model.Logging; namespace MediaBrowser.Controller.Providers { public class DirectoryService { private readonly ILogger _logger; private readonly Dictionary> _cache = new Dictionary>(StringComparer.OrdinalIgnoreCase); public DirectoryService(ILogger logger) { _logger = logger; } public List GetFileSystemEntries(string path) { List entries; if (!_cache.TryGetValue(path, out entries)) { //_logger.Debug("Getting files for " + path); entries = new DirectoryInfo(path).EnumerateFileSystemInfos("*", SearchOption.TopDirectoryOnly).ToList(); _cache.Add(path, entries); } return entries; } public IEnumerable GetFiles(string path) { return GetFileSystemEntries(path).OfType(); } public IEnumerable GetDirectories(string path) { return GetFileSystemEntries(path).OfType(); } public FileInfo GetFile(string path) { var directory = Path.GetDirectoryName(path); var filename = Path.GetFileName(path); return GetFiles(directory).FirstOrDefault(i => string.Equals(i.Name, filename, StringComparison.OrdinalIgnoreCase)); } public DirectoryInfo GetDirectory(string path) { var directory = Path.GetDirectoryName(path); var name = Path.GetFileName(path); return GetDirectories(directory).FirstOrDefault(i => string.Equals(i.Name, name, StringComparison.OrdinalIgnoreCase)); } } }