DirectoryService.cs 4.2 KB

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