DirectoryService.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. Dictionary<string, FileSystemInfo> entries;
  42. if (clearCache)
  43. {
  44. Dictionary<string, FileSystemInfo> removed;
  45. _cache.TryRemove(path, out removed);
  46. }
  47. if (!_cache.TryGetValue(path, out entries))
  48. {
  49. //_logger.Debug("Getting files for " + path);
  50. entries = new Dictionary<string, FileSystemInfo>(StringComparer.OrdinalIgnoreCase);
  51. try
  52. {
  53. var list = new DirectoryInfo(path)
  54. .EnumerateFileSystemInfos("*", SearchOption.TopDirectoryOnly);
  55. // Seeing dupes on some users file system for some reason
  56. foreach (var item in list)
  57. {
  58. entries[item.FullName] = item;
  59. }
  60. }
  61. catch (DirectoryNotFoundException)
  62. {
  63. }
  64. //var group = entries.ToLookup(i => Path.GetDirectoryName(i.FullName)).ToList();
  65. _cache.TryAdd(path, entries);
  66. }
  67. return entries;
  68. }
  69. private IEnumerable<FileSystemInfo> GetFileSystemEntries(string path, bool clearCache)
  70. {
  71. return GetFileSystemDictionary(path, clearCache).Values;
  72. }
  73. public IEnumerable<FileSystemInfo> GetFiles(string path)
  74. {
  75. return GetFiles(path, false);
  76. }
  77. public IEnumerable<FileSystemInfo> GetFiles(string path, bool clearCache)
  78. {
  79. return GetFileSystemEntries(path, clearCache).Where(i => (i.Attributes & FileAttributes.Directory) != FileAttributes.Directory);
  80. }
  81. public FileSystemInfo GetFile(string path)
  82. {
  83. var directory = Path.GetDirectoryName(path);
  84. var dict = GetFileSystemDictionary(directory, false);
  85. FileSystemInfo entry;
  86. dict.TryGetValue(path, out entry);
  87. return entry;
  88. }
  89. public IEnumerable<FileSystemInfo> GetDirectories(string path)
  90. {
  91. return GetFileSystemEntries(path, false).Where(i => (i.Attributes & FileAttributes.Directory) == FileAttributes.Directory);
  92. }
  93. }
  94. }