DirectoryService.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using MediaBrowser.Model.IO;
  7. namespace MediaBrowser.Controller.Providers
  8. {
  9. public class DirectoryService : IDirectoryService
  10. {
  11. // TODO make static and switch to FastConcurrentLru.
  12. private readonly ConcurrentDictionary<string, FileSystemMetadata[]> _cache = new(StringComparer.Ordinal);
  13. private readonly ConcurrentDictionary<string, FileSystemMetadata> _fileCache = new(StringComparer.Ordinal);
  14. private readonly ConcurrentDictionary<string, List<string>> _filePathCache = new(StringComparer.Ordinal);
  15. private readonly IFileSystem _fileSystem;
  16. public DirectoryService(IFileSystem fileSystem)
  17. {
  18. _fileSystem = fileSystem;
  19. }
  20. public FileSystemMetadata[] GetFileSystemEntries(string path)
  21. {
  22. return _cache.GetOrAdd(path, static (p, fileSystem) => fileSystem.GetFileSystemEntries(p).ToArray(), _fileSystem);
  23. }
  24. public List<FileSystemMetadata> GetDirectories(string path)
  25. {
  26. var list = new List<FileSystemMetadata>();
  27. var items = GetFileSystemEntries(path);
  28. for (var i = 0; i < items.Length; i++)
  29. {
  30. var item = items[i];
  31. if (item.IsDirectory)
  32. {
  33. list.Add(item);
  34. }
  35. }
  36. return list;
  37. }
  38. public List<FileSystemMetadata> GetFiles(string path)
  39. {
  40. var list = new List<FileSystemMetadata>();
  41. var items = GetFileSystemEntries(path);
  42. for (var i = 0; i < items.Length; i++)
  43. {
  44. var item = items[i];
  45. if (!item.IsDirectory)
  46. {
  47. list.Add(item);
  48. }
  49. }
  50. return list;
  51. }
  52. public FileSystemMetadata? GetFile(string path)
  53. {
  54. var entry = GetFileSystemEntry(path);
  55. return entry is not null && !entry.IsDirectory ? entry : null;
  56. }
  57. public FileSystemMetadata? GetDirectory(string path)
  58. {
  59. var entry = GetFileSystemEntry(path);
  60. return entry is not null && entry.IsDirectory ? entry : null;
  61. }
  62. public FileSystemMetadata? GetFileSystemEntry(string path)
  63. {
  64. if (!_fileCache.TryGetValue(path, out var result))
  65. {
  66. var file = _fileSystem.GetFileSystemInfo(path);
  67. if (file?.Exists ?? false)
  68. {
  69. result = file;
  70. _fileCache.TryAdd(path, result);
  71. }
  72. }
  73. return result;
  74. }
  75. public IReadOnlyList<string> GetFilePaths(string path)
  76. => GetFilePaths(path, false);
  77. public IReadOnlyList<string> GetFilePaths(string path, bool clearCache, bool sort = false)
  78. {
  79. if (clearCache)
  80. {
  81. _filePathCache.TryRemove(path, out _);
  82. }
  83. var filePaths = _filePathCache.GetOrAdd(path, static (p, fileSystem) => fileSystem.GetFilePaths(p).ToList(), _fileSystem);
  84. if (sort)
  85. {
  86. filePaths.Sort();
  87. }
  88. return filePaths;
  89. }
  90. public bool IsAccessible(string path)
  91. {
  92. return _fileSystem.GetFileSystemEntryPaths(path).Any();
  93. }
  94. }
  95. }