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