DirectoryService.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. private readonly IFileSystem _fileSystem;
  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. public DirectoryService(IFileSystem fileSystem)
  16. {
  17. _fileSystem = fileSystem;
  18. }
  19. public FileSystemMetadata[] GetFileSystemEntries(string path)
  20. {
  21. return _cache.GetOrAdd(path, static (p, fileSystem) => fileSystem.GetFileSystemEntries(p).ToArray(), _fileSystem);
  22. }
  23. public List<FileSystemMetadata> GetDirectories(string path)
  24. {
  25. var list = new List<FileSystemMetadata>();
  26. var items = GetFileSystemEntries(path);
  27. for (var i = 0; i < items.Length; i++)
  28. {
  29. var item = items[i];
  30. if (item.IsDirectory)
  31. {
  32. list.Add(item);
  33. }
  34. }
  35. return list;
  36. }
  37. public List<FileSystemMetadata> GetFiles(string path)
  38. {
  39. var list = new List<FileSystemMetadata>();
  40. var items = GetFileSystemEntries(path);
  41. for (var i = 0; i < items.Length; i++)
  42. {
  43. var item = items[i];
  44. if (!item.IsDirectory)
  45. {
  46. list.Add(item);
  47. }
  48. }
  49. return list;
  50. }
  51. public FileSystemMetadata? GetFile(string path)
  52. {
  53. var entry = GetFileSystemEntry(path);
  54. return entry != null && !entry.IsDirectory ? entry : null;
  55. }
  56. public FileSystemMetadata? GetDirectory(string path)
  57. {
  58. var entry = GetFileSystemEntry(path);
  59. return entry != null && entry.IsDirectory ? entry : null;
  60. }
  61. public FileSystemMetadata? GetFileSystemEntry(string path)
  62. {
  63. if (!_fileCache.TryGetValue(path, out var result))
  64. {
  65. var file = _fileSystem.GetFileSystemInfo(path);
  66. if (file.Exists)
  67. {
  68. result = file;
  69. _fileCache.TryAdd(path, result);
  70. }
  71. }
  72. return result;
  73. }
  74. public IReadOnlyList<string> GetFilePaths(string path)
  75. => GetFilePaths(path, false);
  76. public IReadOnlyList<string> GetFilePaths(string path, bool clearCache, bool sort = false)
  77. {
  78. if (clearCache)
  79. {
  80. _filePathCache.TryRemove(path, out _);
  81. }
  82. var filePaths = _filePathCache.GetOrAdd(path, static (p, fileSystem) => fileSystem.GetFilePaths(p).ToList(), _fileSystem);
  83. if (sort)
  84. {
  85. filePaths.Sort();
  86. }
  87. return filePaths;
  88. }
  89. public bool IsAccessible(string path)
  90. {
  91. return _fileSystem.GetFileSystemEntryPaths(path).Any();
  92. }
  93. }
  94. }