DirectoryService.cs 4.0 KB

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