DirectoryService.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 Dictionary<string, FileSystemMetadata[]> _cache = new Dictionary<string, FileSystemMetadata[]>(StringComparer.OrdinalIgnoreCase);
  16. private readonly Dictionary<string, FileSystemMetadata> _fileCache = new Dictionary<string, FileSystemMetadata>(StringComparer.OrdinalIgnoreCase);
  17. private readonly Dictionary<string, List<string>> _filePathCache = new Dictionary<string, List<string>>(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 FileSystemMetadata[] GetFileSystemEntries(string path)
  28. {
  29. if (string.IsNullOrWhiteSpace(path))
  30. {
  31. throw new ArgumentNullException("path");
  32. }
  33. FileSystemMetadata[] entries;
  34. if (!_cache.TryGetValue(path, out entries))
  35. {
  36. //_logger.Debug("Getting files for " + path);
  37. try
  38. {
  39. // using EnumerateFileSystemInfos doesn't handle reparse points (symlinks)
  40. entries = _fileSystem.GetFileSystemEntries(path).ToArray();
  41. }
  42. catch (IOException)
  43. {
  44. entries = new FileSystemMetadata[] { };
  45. }
  46. //_cache.TryAdd(path, entries);
  47. _cache[path] = entries;
  48. }
  49. return entries;
  50. }
  51. public List<FileSystemMetadata> GetFiles(string path)
  52. {
  53. var list = new List<FileSystemMetadata>();
  54. var items = GetFileSystemEntries(path);
  55. foreach (var item in items)
  56. {
  57. if (!item.IsDirectory)
  58. {
  59. list.Add(item);
  60. }
  61. }
  62. return list;
  63. }
  64. public FileSystemMetadata GetFile(string path)
  65. {
  66. FileSystemMetadata file;
  67. if (!_fileCache.TryGetValue(path, out file))
  68. {
  69. file = _fileSystem.GetFileInfo(path);
  70. if (file != null && file.Exists)
  71. {
  72. //_fileCache.TryAdd(path, file);
  73. _fileCache[path] = file;
  74. }
  75. else
  76. {
  77. return null;
  78. }
  79. }
  80. return file;
  81. //return _fileSystem.GetFileInfo(path);
  82. }
  83. public List<string> GetFilePaths(string path)
  84. {
  85. return GetFilePaths(path, false);
  86. }
  87. public List<string> GetFilePaths(string path, bool clearCache)
  88. {
  89. List<string> result;
  90. if (clearCache || !_filePathCache.TryGetValue(path, out result))
  91. {
  92. result = _fileSystem.GetFilePaths(path).ToList();
  93. _filePathCache[path] = result;
  94. }
  95. return result;
  96. }
  97. }
  98. }