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