DirectoryService.cs 3.6 KB

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