DirectoryService.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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> GetDirectories(string path);
  14. IEnumerable<FileSystemInfo> GetFiles(string path, bool clearCache);
  15. FileSystemInfo GetFile(string path);
  16. Dictionary<string, FileSystemInfo> GetFileSystemDictionary(string path);
  17. }
  18. public class DirectoryService : IDirectoryService
  19. {
  20. private readonly ILogger _logger;
  21. private readonly ConcurrentDictionary<string, Dictionary<string,FileSystemInfo>> _cache =
  22. new ConcurrentDictionary<string, Dictionary<string, FileSystemInfo>>(StringComparer.OrdinalIgnoreCase);
  23. public DirectoryService(ILogger logger)
  24. {
  25. _logger = logger;
  26. }
  27. public DirectoryService()
  28. : this(new NullLogger())
  29. {
  30. }
  31. public IEnumerable<FileSystemInfo> GetFileSystemEntries(string path)
  32. {
  33. return GetFileSystemEntries(path, false);
  34. }
  35. public Dictionary<string, FileSystemInfo> GetFileSystemDictionary(string path)
  36. {
  37. return GetFileSystemDictionary(path, false);
  38. }
  39. private Dictionary<string, FileSystemInfo> GetFileSystemDictionary(string path, bool clearCache)
  40. {
  41. if (string.IsNullOrWhiteSpace(path))
  42. {
  43. throw new ArgumentNullException("path");
  44. }
  45. Dictionary<string, FileSystemInfo> entries;
  46. if (clearCache)
  47. {
  48. Dictionary<string, FileSystemInfo> removed;
  49. _cache.TryRemove(path, out removed);
  50. }
  51. if (!_cache.TryGetValue(path, out entries))
  52. {
  53. //_logger.Debug("Getting files for " + path);
  54. entries = new Dictionary<string, FileSystemInfo>(StringComparer.OrdinalIgnoreCase);
  55. try
  56. {
  57. var list = new DirectoryInfo(path)
  58. .EnumerateFileSystemInfos("*", SearchOption.TopDirectoryOnly);
  59. // Seeing dupes on some users file system for some reason
  60. foreach (var item in list)
  61. {
  62. entries[item.FullName] = item;
  63. }
  64. }
  65. catch (DirectoryNotFoundException)
  66. {
  67. }
  68. //var group = entries.ToLookup(i => Path.GetDirectoryName(i.FullName)).ToList();
  69. _cache.TryAdd(path, entries);
  70. }
  71. return entries;
  72. }
  73. private IEnumerable<FileSystemInfo> GetFileSystemEntries(string path, bool clearCache)
  74. {
  75. return GetFileSystemDictionary(path, clearCache).Values;
  76. }
  77. public IEnumerable<FileSystemInfo> GetFiles(string path)
  78. {
  79. return GetFiles(path, false);
  80. }
  81. public IEnumerable<FileSystemInfo> GetFiles(string path, bool clearCache)
  82. {
  83. return GetFileSystemEntries(path, clearCache).Where(i => (i.Attributes & FileAttributes.Directory) != FileAttributes.Directory);
  84. }
  85. public FileSystemInfo GetFile(string path)
  86. {
  87. var directory = Path.GetDirectoryName(path);
  88. var dict = GetFileSystemDictionary(directory, false);
  89. FileSystemInfo entry;
  90. dict.TryGetValue(path, out entry);
  91. return entry;
  92. }
  93. public IEnumerable<FileSystemInfo> GetDirectories(string path)
  94. {
  95. return GetFileSystemEntries(path, false).Where(i => (i.Attributes & FileAttributes.Directory) == FileAttributes.Directory);
  96. }
  97. }
  98. }