DirectoryService.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using MediaBrowser.Model.Logging;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using CommonIO;
  8. namespace MediaBrowser.Controller.Providers
  9. {
  10. public class DirectoryService : IDirectoryService
  11. {
  12. private readonly ILogger _logger;
  13. private readonly IFileSystem _fileSystem;
  14. private readonly ConcurrentDictionary<string, Dictionary<string, FileSystemMetadata>> _cache =
  15. new ConcurrentDictionary<string, Dictionary<string, FileSystemMetadata>>(StringComparer.OrdinalIgnoreCase);
  16. public DirectoryService(ILogger logger, IFileSystem fileSystem)
  17. {
  18. _logger = logger;
  19. _fileSystem = fileSystem;
  20. }
  21. public DirectoryService(IFileSystem fileSystem)
  22. : this(new NullLogger(), fileSystem)
  23. {
  24. }
  25. public IEnumerable<FileSystemMetadata> GetFileSystemEntries(string path)
  26. {
  27. return GetFileSystemEntries(path, false);
  28. }
  29. public Dictionary<string, FileSystemMetadata> GetFileSystemDictionary(string path)
  30. {
  31. return GetFileSystemDictionary(path, false);
  32. }
  33. private Dictionary<string, FileSystemMetadata> GetFileSystemDictionary(string path, bool clearCache)
  34. {
  35. if (string.IsNullOrWhiteSpace(path))
  36. {
  37. throw new ArgumentNullException("path");
  38. }
  39. Dictionary<string, FileSystemMetadata> entries;
  40. if (clearCache)
  41. {
  42. Dictionary<string, FileSystemMetadata> removed;
  43. _cache.TryRemove(path, out removed);
  44. }
  45. if (!_cache.TryGetValue(path, out entries))
  46. {
  47. //_logger.Debug("Getting files for " + path);
  48. entries = new Dictionary<string, FileSystemMetadata>(StringComparer.OrdinalIgnoreCase);
  49. try
  50. {
  51. // using EnumerateFileSystemInfos doesn't handle reparse points (symlinks)
  52. var list = _fileSystem.GetFileSystemEntries(path)
  53. .ToList();
  54. // Seeing dupes on some users file system for some reason
  55. foreach (var item in list)
  56. {
  57. entries[item.FullName] = item;
  58. }
  59. }
  60. catch (DirectoryNotFoundException)
  61. {
  62. }
  63. //var group = entries.ToLookup(i => Path.GetDirectoryName(i.FullName)).ToList();
  64. _cache.TryAdd(path, entries);
  65. }
  66. return entries;
  67. }
  68. private IEnumerable<FileSystemMetadata> GetFileSystemEntries(string path, bool clearCache)
  69. {
  70. return GetFileSystemDictionary(path, clearCache).Values;
  71. }
  72. public IEnumerable<FileSystemMetadata> GetFiles(string path)
  73. {
  74. return GetFiles(path, false);
  75. }
  76. public IEnumerable<FileSystemMetadata> GetFiles(string path, bool clearCache)
  77. {
  78. return GetFileSystemEntries(path, clearCache).Where(i => !i.IsDirectory);
  79. }
  80. public FileSystemMetadata GetFile(string path)
  81. {
  82. return _fileSystem.GetFileInfo(path);
  83. //var directory = Path.GetDirectoryName(path);
  84. //if (string.IsNullOrWhiteSpace(directory))
  85. //{
  86. // _logger.Debug("Parent path is null for {0}", path);
  87. // return null;
  88. //}
  89. //try
  90. //{
  91. // var dict = GetFileSystemDictionary(directory, false);
  92. // FileSystemMetadata entry;
  93. // dict.TryGetValue(path, out entry);
  94. // return entry;
  95. //}
  96. //catch (Exception ex)
  97. //{
  98. // _logger.ErrorException("Error in GetFileSystemDictionary. Directory: :{0}. Original path: {1}", ex, directory, path);
  99. // return null;
  100. //}
  101. }
  102. public IEnumerable<FileSystemMetadata> GetDirectories(string path)
  103. {
  104. return GetFileSystemEntries(path, false).Where(i => i.IsDirectory);
  105. }
  106. }
  107. }