DirectoryService.cs 3.5 KB

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