DirectoryService.cs 3.9 KB

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