forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractVirtualFileBase.cs
More file actions
126 lines (104 loc) · 3.58 KB
/
AbstractVirtualFileBase.cs
File metadata and controls
126 lines (104 loc) · 3.58 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ServiceStack.IO;
using ServiceStack.VirtualPath;
namespace ServiceStack.VirtualPath
{
public abstract class AbstractVirtualFileBase : IVirtualFile
{
public static List<string> ScanSkipPaths { get; set; } = new List<string>();
public IVirtualPathProvider VirtualPathProvider { get; set; }
public string Extension => Name.LastRightPart('.');
public IVirtualDirectory Directory { get; set; }
public abstract string Name { get; }
public virtual string VirtualPath => GetVirtualPathToRoot();
public virtual string RealPath => GetRealPathToRoot();
public virtual bool IsDirectory => false;
public abstract DateTime LastModified { get; }
public abstract long Length { get; }
protected AbstractVirtualFileBase(IVirtualPathProvider owningProvider, IVirtualDirectory directory)
{
this.VirtualPathProvider = owningProvider ?? throw new ArgumentNullException(nameof(owningProvider));
this.Directory = directory ?? throw new ArgumentNullException(nameof(directory));
}
public virtual string GetFileHash()
{
using (var stream = OpenRead())
{
return stream.ToMd5Hash();
}
}
public virtual StreamReader OpenText()
{
return new StreamReader(OpenRead());
}
public virtual string ReadAllText()
{
using (var reader = OpenText())
{
var text = reader.ReadToEnd();
return text;
}
}
public virtual byte[] ReadAllBytes()
{
using (var stream = OpenRead())
{
return stream.ReadFully();
}
}
public abstract Stream OpenRead();
protected virtual string GetVirtualPathToRoot()
{
return GetPathToRoot(VirtualPathProvider.VirtualPathSeparator, p => p.VirtualPath);
}
protected virtual string GetRealPathToRoot()
{
return GetPathToRoot(VirtualPathProvider.RealPathSeparator, p => p.RealPath);
}
protected virtual string GetPathToRoot(string separator, Func<IVirtualDirectory, string> pathSel)
{
var parentPath = Directory != null ? pathSel(Directory) : string.Empty;
if (parentPath == separator)
parentPath = string.Empty;
return parentPath == null
? Name
: string.Concat(parentPath, separator, Name);
}
public override bool Equals(object obj)
{
var other = obj as AbstractVirtualFileBase;
if (other == null)
return false;
return other.VirtualPath == this.VirtualPath;
}
public override int GetHashCode()
{
return VirtualPath.GetHashCode();
}
public override string ToString()
{
return $"{RealPath} -> {VirtualPath}";
}
public virtual void Refresh()
{
}
}
}
namespace ServiceStack
{
public static class VirtualFileExtensions
{
public static bool ShouldSkipPath(this IVirtualNode node)
{
foreach (var skipPath in AbstractVirtualFileBase.ScanSkipPaths)
{
if (node.VirtualPath.StartsWith(skipPath.TrimStart('/'), StringComparison.OrdinalIgnoreCase))
return true;
}
return false;
}
}
}