-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualFileProvider.cs
More file actions
246 lines (185 loc) · 8.39 KB
/
VirtualFileProvider.cs
File metadata and controls
246 lines (185 loc) · 8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using System.Collections;
using System.Collections.Concurrent;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.FileSystemGlobbing;
using Microsoft.Extensions.Primitives;
namespace Brics.FileProviders;
/// <summary>
/// Implements <c>Microsoft.Extensions.FileProviders.IFileProvider</c> for using virtual files.
/// </summary>
public class VirtualFileProvider<TVirtualFile> : IFileProvider, IEnumerable<KeyValuePair<string, IVirtualFileInfo<TVirtualFile>>> where TVirtualFile : IVirtualFile {
private readonly ConcurrentDictionary<string, CancellationTokenSource> _listeners = new();
private readonly ConcurrentDictionary<string, IVirtualFileInfo<TVirtualFile>> _files;
private readonly bool _caseSensitive;
private readonly IEqualityComparer<TVirtualFile> _virtualFileComparer;
private readonly StringComparer _sorter;
public VirtualFileProvider(bool caseSensitive = true, IEqualityComparer<TVirtualFile>? virtualFileComparer = default) {
_caseSensitive = caseSensitive;
_virtualFileComparer = virtualFileComparer ?? EqualityComparer<TVirtualFile>.Default;
_sorter = caseSensitive ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal;
_files = new(_sorter);
}
public IChangeToken Watch(string filter) {
var cts = _listeners.GetOrAdd(filter, x => new());
return new CancellationChangeToken(cts.Token);
}
public bool TryGetData(string path, out TVirtualFile? virtualFile) {
if(!_files.TryGetValue(path, out var vf)) {
virtualFile = default;
return false;
}
virtualFile = vf.Source;
return true;
}
public void Clear(bool skipNotify = false) {
if (skipNotify) {
var keys = _files.Keys.ToArray();
if (keys.Length <= 0) return;
_files.Clear();
NotifyListeners(keys);
} else {
_files.Clear();
}
}
public bool Add(string path, TVirtualFile virtualFile, bool skipNotify = false) {
var file = CreateFileInfo(path, virtualFile);
var resultFile = _files.AddOrUpdate(path, file, (x, y) => {
return _virtualFileComparer.Equals(y.Source, virtualFile) ? y : file;
});
var updated = file == resultFile;
if (!skipNotify && updated) {
NotifyListeners(new[] {path});
}
return updated;
}
public bool Remove(string path, bool skipNotify = false) {
var removed = _files.TryRemove(path, out _);
var notify = removed && !skipNotify;
if (notify) {
NotifyListeners(new[] {path});
}
return removed;
}
public bool RemoveMany(IEnumerable<string> paths, bool skipNotify = false) {
var removedFiles = new HashSet<string>(_caseSensitive ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase);
foreach (var path in paths) {
if (_files.TryRemove(path, out _)) {
removedFiles.Add(path);
}
}
if (!skipNotify && removedFiles.Count > 0) {
NotifyListeners(removedFiles);
}
return removedFiles.Count > 0;
}
public void NotifyListeners(ICollection<string> paths) {
var keys = _listeners.Keys.ToArray();
foreach (var key in keys) {
var matcher = new Matcher(_caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase);
matcher.AddInclude(key);
if (matcher.Match("/", paths).HasMatches) {
if (_listeners.TryRemove(key, out var value)) {
value.Cancel();
}
}
}
}
public IDirectoryContents GetDirectoryContents(string subpath) {
subpath = subpath.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
if (string.IsNullOrWhiteSpace(subpath)) {
subpath = Path.DirectorySeparatorChar.ToString();
}
if (subpath[0] != Path.DirectorySeparatorChar && subpath[0] != Path.AltDirectorySeparatorChar) {
subpath = Path.AltDirectorySeparatorChar + subpath;
}
if (subpath[subpath.Length - 1] != Path.DirectorySeparatorChar && subpath[subpath.Length - 1] != Path.AltDirectorySeparatorChar) {
subpath += Path.AltDirectorySeparatorChar;
}
var subPathParts = subpath.Split(new[] {Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar}, StringSplitOptions.RemoveEmptyEntries);
var files = new List<IFileInfo>();
var dirs = new HashSet<string>(_caseSensitive ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase);
foreach (var entry in _files.OrderBy(x => x.Key, _sorter)) {
if (!entry.Key.StartsWith(subpath, _caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase)) {
continue;
}
var partialName = entry.Key.Substring(subpath.Length - 1);
var partialNameParts = partialName.Split(new[] {Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar}, StringSplitOptions.RemoveEmptyEntries);
var folders = new Span<string>(partialNameParts, 0, partialNameParts.Length - 1);
if (folders.Length == 0) {
files.Add(entry.Value);
continue;
}
var subFolder = string.Join(Path.DirectorySeparatorChar.ToString(), subPathParts.Union(new[] {folders[0]}));
if (dirs.Contains(subFolder)) {
continue;
}
dirs.Add(subFolder);
files.Add(new VirtualDirectoryFileInfo(folders[0], true));
}
return new VirtualDirectoryContents(files);
}
public IFileInfo GetFileInfo(string subpath) {
subpath = subpath.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
if (subpath[0] != Path.DirectorySeparatorChar && subpath[0] != Path.AltDirectorySeparatorChar) {
subpath = Path.AltDirectorySeparatorChar + subpath;
}
if (_files.TryGetValue(subpath, out var fileInfo)) {
return fileInfo;
}
return new VirtualFileInfoNotFound(subpath);
}
protected virtual IVirtualFileInfo<TVirtualFile> CreateFileInfo(string path, TVirtualFile virtualFile) {
return new DefaultVirtualFileInfo<TVirtualFile>(path, virtualFile);
}
public IEnumerator<KeyValuePair<string, IVirtualFileInfo<TVirtualFile>>> GetEnumerator() {
return _files.OrderBy(x => x.Key, _sorter).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
private class VirtualFileInfoNotFound : IFileInfo {
public VirtualFileInfoNotFound(string path) {
Name = path;
LastModified = DateTimeOffset.MinValue;
IsDirectory = true;
}
public Stream CreateReadStream() {
throw new NotSupportedException();
}
public bool Exists { get; } = false;
public long Length { get; } = 0;
public string? PhysicalPath { get; } = default;
public string Name { get; }
public DateTimeOffset LastModified { get; }
public bool IsDirectory { get; }
}
private class VirtualDirectoryFileInfo : IFileInfo {
public VirtualDirectoryFileInfo(string name, bool exists) {
Name = name;
Exists = exists;
}
public bool Exists { get; }
public bool IsDirectory { get; } = true;
public DateTimeOffset LastModified { get; } = default;
public long Length { get; } = -1;
public string Name { get; }
public string? PhysicalPath { get; } = default;
public Stream CreateReadStream() {
throw new NotSupportedException();
}
}
private class VirtualDirectoryContents : IDirectoryContents {
private readonly List<IFileInfo> _matchingFiles;
public VirtualDirectoryContents(List<IFileInfo> matchingFiles) {
_matchingFiles = matchingFiles;
Exists = matchingFiles.Count > 0;
}
public bool Exists { get; }
IEnumerator<IFileInfo> IEnumerable<IFileInfo>.GetEnumerator() {
return _matchingFiles.AsEnumerable().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return _matchingFiles.AsEnumerable().GetEnumerator();
}
}
}