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
92 lines (74 loc) · 2.8 KB
/
AbstractVirtualFileBase.cs
File metadata and controls
92 lines (74 loc) · 2.8 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
using System;
using System.IO;
using System.Security.Cryptography;
namespace ServiceStack.Razor.VirtualPath
{
public abstract class AbstractVirtualFileBase : IVirtualFile
{
public IVirtualPathProvider VirtualPathProvider { get; set; }
protected IVirtualDirectory ParentDirectory;
public virtual bool IsDirectory { get { return false; } }
public virtual string VirtualPath { get { return GetVirtualPathToRoot(); } }
public virtual string RealPath { get { return GetRealPathToRoot(); } }
public abstract string Name { get; }
public abstract DateTime LastModified { get; }
protected AbstractVirtualFileBase(IVirtualPathProvider owningProvider, IVirtualDirectory parentDirectory)
{
if (owningProvider == null)
throw new ArgumentNullException("owningProvider");
if (parentDirectory == null)
throw new ArgumentNullException("parentDirectory");
this.VirtualPathProvider = owningProvider;
this.ParentDirectory = parentDirectory;
}
public virtual string GetFileHash()
{
using (var stream = OpenRead())
{
return MD5.Create().ComputeHash(stream).ToString();
}
}
public virtual StreamReader OpenText()
{
return new StreamReader(OpenRead());
}
public virtual string ReadAllText()
{
using (var reader = OpenText())
{
return reader.ReadToEnd();
}
}
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 = ParentDirectory != null ? pathSel(ParentDirectory) : string.Empty;
if (parentPath == separator)
parentPath = string.Empty;
return 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 string.Format("{0} -> {1}", RealPath, VirtualPath);
}
}
}