DirectoryService.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. namespace MediaBrowser.Controller.Providers
  8. {
  9. public interface IDirectoryService
  10. {
  11. IEnumerable<FileSystemInfo> GetFileSystemEntries(string path);
  12. IEnumerable<FileSystemInfo> GetFiles(string path);
  13. IEnumerable<FileSystemInfo> GetFiles(string path, bool clearCache);
  14. FileSystemInfo GetFile(string path);
  15. Dictionary<string, FileSystemInfo> GetFileSystemDictionary(string path);
  16. }
  17. public class DirectoryService : IDirectoryService
  18. {
  19. private readonly ILogger _logger;
  20. private readonly ConcurrentDictionary<string, Dictionary<string,FileSystemInfo>> _cache =
  21. new ConcurrentDictionary<string, Dictionary<string, FileSystemInfo>>(StringComparer.OrdinalIgnoreCase);
  22. public DirectoryService(ILogger logger)
  23. {
  24. _logger = logger;
  25. }
  26. public DirectoryService()
  27. : this(new NullLogger())
  28. {
  29. }
  30. public IEnumerable<FileSystemInfo> GetFileSystemEntries(string path)
  31. {
  32. return GetFileSystemEntries(path, false);
  33. }
  34. public Dictionary<string, FileSystemInfo> GetFileSystemDictionary(string path)
  35. {
  36. return GetFileSystemDictionary(path, false);
  37. }
  38. private Dictionary<string, FileSystemInfo> GetFileSystemDictionary(string path, bool clearCache)
  39. {
  40. Dictionary<string, FileSystemInfo> entries;
  41. if (clearCache)
  42. {
  43. Dictionary<string, FileSystemInfo> removed;
  44. _cache.TryRemove(path, out removed);
  45. }
  46. if (!_cache.TryGetValue(path, out entries))
  47. {
  48. //_logger.Debug("Getting files for " + path);
  49. entries = new Dictionary<string, FileSystemInfo>(StringComparer.OrdinalIgnoreCase);
  50. try
  51. {
  52. var list = new DirectoryInfo(path)
  53. .EnumerateFileSystemInfos("*", SearchOption.TopDirectoryOnly);
  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<FileSystemInfo> GetFileSystemEntries(string path, bool clearCache)
  69. {
  70. return GetFileSystemDictionary(path, clearCache).Values;
  71. }
  72. public IEnumerable<FileSystemInfo> GetFiles(string path)
  73. {
  74. return GetFiles(path, false);
  75. }
  76. public IEnumerable<FileSystemInfo> GetFiles(string path, bool clearCache)
  77. {
  78. return GetFileSystemEntries(path, clearCache).Where(i => (i.Attributes & FileAttributes.Directory) != FileAttributes.Directory);
  79. }
  80. public FileSystemInfo GetFile(string path)
  81. {
  82. var directory = Path.GetDirectoryName(path);
  83. var dict = GetFileSystemDictionary(directory, false);
  84. FileSystemInfo entry;
  85. dict.TryGetValue(path, out entry);
  86. return entry;
  87. }
  88. }
  89. }