2
0

DirectoryService.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // using EnumerateFileSystemInfos doesn't handle reparse points (symlinks)
  49. var list = new DirectoryInfo(path).EnumerateDirectories("*", SearchOption.TopDirectoryOnly)
  50. .Concat<FileSystemInfo>(new DirectoryInfo(path).EnumerateFiles("*", SearchOption.TopDirectoryOnly));
  51. // Seeing dupes on some users file system for some reason
  52. foreach (var item in list)
  53. {
  54. entries[item.FullName] = item;
  55. }
  56. }
  57. catch (DirectoryNotFoundException)
  58. {
  59. }
  60. //var group = entries.ToLookup(i => Path.GetDirectoryName(i.FullName)).ToList();
  61. _cache.TryAdd(path, entries);
  62. }
  63. return entries;
  64. }
  65. private IEnumerable<FileSystemInfo> GetFileSystemEntries(string path, bool clearCache)
  66. {
  67. return GetFileSystemDictionary(path, clearCache).Values;
  68. }
  69. public IEnumerable<FileSystemInfo> GetFiles(string path)
  70. {
  71. return GetFiles(path, false);
  72. }
  73. public IEnumerable<FileSystemInfo> GetFiles(string path, bool clearCache)
  74. {
  75. return GetFileSystemEntries(path, clearCache).Where(i => (i.Attributes & FileAttributes.Directory) != FileAttributes.Directory);
  76. }
  77. public FileSystemInfo GetFile(string path)
  78. {
  79. var directory = Path.GetDirectoryName(path);
  80. var dict = GetFileSystemDictionary(directory, false);
  81. FileSystemInfo entry;
  82. dict.TryGetValue(path, out entry);
  83. return entry;
  84. }
  85. public IEnumerable<FileSystemInfo> GetDirectories(string path)
  86. {
  87. return GetFileSystemEntries(path, false).Where(i => (i.Attributes & FileAttributes.Directory) == FileAttributes.Directory);
  88. }
  89. }
  90. }