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