DirectoryService.cs 3.6 KB

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