-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathMemoryFileProvider.cs
More file actions
225 lines (175 loc) · 6.64 KB
/
Copy pathMemoryFileProvider.cs
File metadata and controls
225 lines (175 loc) · 6.64 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System.Collections;
using System.Text;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Primitives;
namespace Steeltoe.Common.TestResources;
public sealed class MemoryFileProvider : IFileProvider
{
private static readonly char[] DirectorySeparators =
[
Path.DirectorySeparatorChar,
Path.AltDirectorySeparatorChar
];
private readonly MemoryFileSystemEntry _root = MemoryFileSystemEntry.CreateDirectory("file-system-root");
private ConfigurationReloadToken _changeToken = new();
public void IncludeDirectory(string path)
{
ArgumentException.ThrowIfNullOrEmpty(path);
IEnumerable<string> pathSegments = PathToSegments(path);
_ = GetOrCreateDirectories(pathSegments);
}
public void IncludeFile(string path, string contents)
{
ArgumentException.ThrowIfNullOrEmpty(path);
ArgumentNullException.ThrowIfNull(contents);
byte[] bytes = Encoding.UTF8.GetBytes(contents);
IncludeFile(path, bytes);
}
public void IncludeFile(string path, byte[] contents)
{
ArgumentException.ThrowIfNullOrEmpty(path);
ArgumentNullException.ThrowIfNull(contents);
string[] pathSegments = PathToSegments(path);
string[] parentDirectories = pathSegments[..^1];
string fileName = pathSegments[^1];
MemoryFileSystemEntry parentDirectory = GetOrCreateDirectories(parentDirectories);
var file = MemoryFileSystemEntry.CreateFile(fileName, contents);
parentDirectory.Children.Add(file.Name, file);
}
private MemoryFileSystemEntry GetOrCreateDirectories(IEnumerable<string> pathSegments)
{
MemoryFileSystemEntry currentDirectory = _root;
foreach (string segment in pathSegments)
{
if (!currentDirectory.Children.TryGetValue(segment, out MemoryFileSystemEntry? child))
{
child = MemoryFileSystemEntry.CreateDirectory(segment);
currentDirectory.Children.Add(segment, child);
}
if (!child.IsDirectory)
{
throw new InvalidOperationException($"Unable to create directory '{segment}' because a file with the same name already exists.");
}
currentDirectory = child;
}
return currentDirectory;
}
public void ReplaceFile(string path, string contents)
{
ArgumentException.ThrowIfNullOrEmpty(path);
ArgumentNullException.ThrowIfNull(contents);
byte[] bytes = Encoding.UTF8.GetBytes(contents);
ReplaceFile(path, bytes);
}
public void ReplaceFile(string path, byte[] contents)
{
ArgumentException.ThrowIfNullOrEmpty(path);
ArgumentNullException.ThrowIfNull(contents);
MemoryFileSystemEntry? entry = Find(path);
if (entry == null || entry.IsDirectory)
{
throw new InvalidOperationException($"File '{path}' does not exist.");
}
entry.ReplaceContents(contents);
}
public IFileInfo GetFileInfo(string subpath)
{
ArgumentException.ThrowIfNullOrEmpty(subpath);
MemoryFileSystemEntry? entry = Find(subpath);
if (entry == null || entry.IsDirectory)
{
return new NotFoundFileInfo(subpath);
}
return entry;
}
public IDirectoryContents GetDirectoryContents(string subpath)
{
ArgumentException.ThrowIfNullOrEmpty(subpath);
MemoryFileSystemEntry? entry = Find(subpath);
if (entry is not { IsDirectory: true })
{
return NotFoundDirectoryContents.Singleton;
}
return entry;
}
private MemoryFileSystemEntry? Find(string path)
{
string[] pathSegments = PathToSegments(path);
MemoryFileSystemEntry current = _root;
foreach (string segment in pathSegments)
{
if (!current.IsDirectory || !current.Children.TryGetValue(segment, out MemoryFileSystemEntry? child))
{
return null;
}
current = child;
}
return current;
}
private static string[] PathToSegments(string path)
{
return path.TrimEnd(DirectorySeparators).Split(DirectorySeparators, StringSplitOptions.RemoveEmptyEntries);
}
public IChangeToken Watch(string filter)
{
return _changeToken;
}
public void NotifyChanged()
{
ConfigurationReloadToken previousToken = Interlocked.Exchange(ref _changeToken, new ConfigurationReloadToken());
previousToken.OnReload();
}
private sealed class MemoryFileSystemEntry : IDirectoryContents, IFileInfo
{
private byte[]? _fileContents;
public bool Exists => true;
public bool IsDirectory => _fileContents == null;
public string Name { get; }
public string PhysicalPath => null!;
public long Length => _fileContents?.Length ?? -1;
public DateTimeOffset LastModified => default;
public Dictionary<string, MemoryFileSystemEntry> Children { get; } = new(StringComparer.OrdinalIgnoreCase);
private MemoryFileSystemEntry(string name, byte[]? fileContents)
{
Name = name;
_fileContents = fileContents;
}
public static MemoryFileSystemEntry CreateFile(string name, byte[] contents)
{
return new MemoryFileSystemEntry(name, contents);
}
public static MemoryFileSystemEntry CreateDirectory(string name)
{
return new MemoryFileSystemEntry(name, null);
}
public Stream CreateReadStream()
{
if (IsDirectory)
{
throw new InvalidOperationException($"Unable to read file contents of '{Name}' because it is a directory.");
}
return new MemoryStream(_fileContents!);
}
public void ReplaceContents(byte[] contents)
{
ArgumentNullException.ThrowIfNull(contents);
if (IsDirectory)
{
throw new InvalidOperationException($"Unable to replace file contents of '{Name}' because it is a directory.");
}
_fileContents = contents;
}
public IEnumerator<IFileInfo> GetEnumerator()
{
return Children.Values.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}