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
154 lines (133 loc) · 4.92 KB
/
FileSystemVirtualFiles.cs
File metadata and controls
154 lines (133 loc) · 4.92 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
using System;
using System.Collections.Generic;
using System.IO;
using ServiceStack.VirtualPath;
namespace ServiceStack.IO
{
public class FileSystemVirtualFiles
: AbstractVirtualPathProviderBase, IVirtualFiles
{
protected DirectoryInfo RootDirInfo;
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.OpenWrite(realFilePath))
{
stream.WriteTo(fs);
}
}
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 NETSTANDARD2_0
// 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);
}
}
}
}