forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceVirtualFiles.cs
More file actions
111 lines (96 loc) · 4.02 KB
/
ResourceVirtualFiles.cs
File metadata and controls
111 lines (96 loc) · 4.02 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
using System;
using System.Collections.Generic;
using System.Reflection;
using ServiceStack.DataAnnotations;
using ServiceStack.VirtualPath;
namespace ServiceStack.IO
{
public class ResourceVirtualFiles
: AbstractVirtualPathProviderBase
{
protected ResourceVirtualDirectory RootDir;
protected readonly Assembly BackingAssembly;
protected readonly string RootNamespace;
public override IVirtualDirectory RootDirectory => RootDir;
public override string VirtualPathSeparator => "/";
public override string RealPathSeparator => ".";
public DateTime LastModified { get; set; }
public ResourceVirtualFiles(Type baseTypeInAssembly)
: this(baseTypeInAssembly.Assembly, GetNamespace(baseTypeInAssembly)) { }
public ResourceVirtualFiles(Assembly backingAssembly, string rootNamespace=null)
{
this.BackingAssembly = backingAssembly ?? throw new ArgumentNullException(nameof(backingAssembly));
this.RootNamespace = rootNamespace ?? backingAssembly.GetName().Name;
Initialize();
}
//https://docs.microsoft.com/en-us/dotnet/api/system.resources.tools.stronglytypedresourcebuilder.verifyresourcename?redirectedfrom=MSDN&view=netframework-4.8#remarks
static readonly char [] NamespaceSpecialChars = { ' ', '\u00A0', ',', ';', '|', '~', '@', '#', '%', '^', '&',
'*', '+', '-', /*'/', '\\',*/ '<', '>', '?', '[', ']', '(', ')', '{',
'}', '\"', '\'', '!'};
private static string CleanChars(string name)
{
var newChars = new char[name.Length];
var nameChars = name.AsSpan();
for (var i = 0; i < nameChars.Length ;i++)
{
newChars[i] = nameChars[i];
foreach (var c in NamespaceSpecialChars)
{
if (nameChars[i] == c)
{
newChars[i] = '_';
break;
}
}
}
return new string (newChars);
}
public static HashSet<string> PartialFileNames { get; set; } = new HashSet<string>
{
"min.js",
"min.css",
};
public string CleanPath(string filePath)
{
var sanitizedPath = base.SanitizePath(filePath);
if (sanitizedPath == null)
return null;
var lastDirPos = sanitizedPath.LastIndexOf('/');
if (lastDirPos >= 0)
{
var dirPath = sanitizedPath.Substring(0, lastDirPos);
var fileName = sanitizedPath.Substring(lastDirPos + 1);
if (PartialFileNames.Contains(fileName))
{
var partialName = dirPath.LastRightPart('/');
dirPath = dirPath.LastLeftPart('/');
fileName = partialName + '.' + fileName;
}
var cleanDir = CleanChars(dirPath); //only dirs are replaced
var cleanPath = cleanDir + '/' + fileName;
return cleanPath;
}
return sanitizedPath;
}
public override IVirtualFile GetFile(string virtualPath)
{
var virtualFile = RootDirectory.GetFile(CleanPath(virtualPath));
virtualFile?.Refresh();
return virtualFile;
}
private static string GetNamespace(Type type)
{
var attr = type.FirstAttribute<SchemaAttribute>();
return attr != null ? attr.Name : type.Namespace;
}
protected sealed override void Initialize()
{
var asm = BackingAssembly;
RootDir = new ResourceVirtualDirectory(this, null, asm, LastModified, RootNamespace);
}
public override string CombineVirtualPath(string basePath, string relativePath)
{
return string.Concat(basePath, VirtualPathSeparator, relativePath);
}
}
}