forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystemVirtualFiles.cs
More file actions
209 lines (181 loc) · 7.1 KB
/
Copy pathFileSystemVirtualFiles.cs
File metadata and controls
209 lines (181 loc) · 7.1 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using ServiceStack.Text;
using ServiceStack.VirtualPath;
namespace ServiceStack.IO
{
public class FileSystemVirtualFiles
: AbstractVirtualPathProviderBase, IVirtualFiles
{
public DirectoryInfo RootDirInfo { get; protected set; }
protected FileSystemVirtualDirectory RootDir;
public override IVirtualDirectory RootDirectory => RootDir;
public override string VirtualPathSeparator => "/";
public override string RealPathSeparator => Convert.ToString(Path.DirectorySeparatorChar);
public FileSystemVirtualFiles(string rootDirectoryPath)
: this(new DirectoryInfo(rootDirectoryPath))
{
}
public FileSystemVirtualFiles(DirectoryInfo rootDirInfo)
{
this.RootDirInfo = rootDirInfo ?? throw new ArgumentNullException(nameof(rootDirInfo));
Initialize();
}
protected sealed override void Initialize()
{
if (!RootDirInfo.Exists)
throw new Exception($"RootDir '{RootDirInfo.FullName}' for virtual path does not exist");
RootDir = new FileSystemVirtualDirectory(this, null, RootDirInfo);
}
public override bool DirectoryExists(string virtualPath)
{
var isDirectory = Directory.Exists(RootDirectory.RealPath.CombineWith(SanitizePath(virtualPath)));
return isDirectory;
}
public override bool FileExists(string virtualPath)
{
var isFile = File.Exists(RootDirectory.RealPath.CombineWith(SanitizePath(virtualPath)));
return isFile;
}
public string EnsureDirectory(string dirPath)
{
if (!Directory.Exists(dirPath))
Directory.CreateDirectory(dirPath);
return dirPath;
}
public void WriteFile(string filePath, string textContents)
{
var realFilePath = RootDir.RealPath.CombineWith(filePath);
EnsureDirectory(Path.GetDirectoryName(realFilePath));
File.WriteAllText(realFilePath, textContents);
}
public void WriteFile(string filePath, Stream stream)
{
var realFilePath = RootDir.RealPath.CombineWith(filePath);
EnsureDirectory(Path.GetDirectoryName(realFilePath));
using var fs = File.Open(realFilePath, FileMode.Create, FileAccess.Write);
stream.WriteTo(fs);
}
public override async Task WriteFileAsync(string filePath, object contents, CancellationToken token=default)
{
if (contents == null)
return;
var realFilePath = RootDir.RealPath.CombineWith(filePath);
EnsureDirectory(Path.GetDirectoryName(realFilePath));
using var fs = File.Open(realFilePath, FileMode.Create, FileAccess.Write);
if (contents is IVirtualFile vfile)
await WriteFileAsync(filePath, vfile.GetContents(), token).ConfigAwait();
else if (contents is string textContents)
await fs.WriteAsync(textContents, token).ConfigAwait();
else if (contents is ReadOnlyMemory<char> romChars)
await fs.WriteAsync(romChars.Span, token).ConfigAwait();
else if (contents is byte[] binaryContents)
await fs.WriteAsync(binaryContents, token: token).ConfigAwait();
else if (contents is ReadOnlyMemory<byte> romBytes)
await fs.WriteAsync(romBytes, token).ConfigAwait();
else if (contents is Stream stream)
await stream.CopyToAsync(fs).ConfigAwait();
else
throw CreateContentNotSupportedException(contents);
}
public void WriteFiles(IEnumerable<IVirtualFile> files, Func<IVirtualFile, string> toPath = null)
{
this.CopyFrom(files, toPath);
}
public void AppendFile(string filePath, string textContents)
{
var realFilePath = RootDir.RealPath.CombineWith(filePath);
EnsureDirectory(Path.GetDirectoryName(realFilePath));
File.AppendAllText(realFilePath, textContents);
}
public void AppendFile(string filePath, Stream stream)
{
var realFilePath = RootDir.RealPath.CombineWith(filePath);
EnsureDirectory(Path.GetDirectoryName(realFilePath));
using (var fs = new FileStream(realFilePath, FileMode.Append))
{
stream.WriteTo(fs);
}
}
public void DeleteFile(string filePath)
{
var realFilePath = RootDir.RealPath.CombineWith(filePath);
try
{
File.Delete(realFilePath);
}
catch (Exception /*ignore*/)
{
}
}
public void DeleteFiles(IEnumerable<string> filePaths)
{
filePaths.Each(DeleteFile);
}
public void DeleteFolder(string dirPath)
{
var realPath = RootDir.RealPath.CombineWith(dirPath);
#if NETCORE
// Doesn't properly recursively delete nested dirs/files on .NET Core (win at least)
if (Directory.Exists(realPath))
DeleteDirectoryRecursive(realPath);
#else
if (Directory.Exists(realPath))
Directory.Delete(realPath, recursive: true);
#endif
}
public static void DeleteDirectoryRecursive(string path)
{
//modified from https://stackoverflow.com/a/1703799/85785
foreach (var directory in Directory.GetDirectories(path))
{
var files = Directory.GetFiles(directory);
foreach (var file in files)
{
File.SetAttributes(file, FileAttributes.Normal);
}
DeleteDirectoryRecursive(directory);
}
try
{
Directory.Delete(path, true);
}
catch (IOException)
{
Directory.Delete(path, true);
}
catch (UnauthorizedAccessException)
{
Directory.Delete(path, true);
}
}
public static string AssertDirectory(string dirPath, int timeoutMs=1000)
{
if (string.IsNullOrEmpty(dirPath))
return null;
try
{
ExecUtils.RetryOnException(() => {
if (!Directory.Exists(dirPath))
Directory.CreateDirectory(dirPath);
}, TimeSpan.FromMilliseconds(timeoutMs));
return dirPath;
}
catch (TimeoutException e)
{
throw e.InnerException ?? e;
}
}
public static void RecreateDirectory(string dirPath, int timeoutMs = 1000)
{
if (Directory.Exists(dirPath))
{
DeleteDirectoryRecursive(dirPath);
}
AssertDirectory(dirPath, timeoutMs);
}
}
}