forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharpPages.cs
More file actions
316 lines (255 loc) · 12.1 KB
/
SharpPages.cs
File metadata and controls
316 lines (255 loc) · 12.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
using System;
using System.Collections.Concurrent;
using System.Linq;
using ServiceStack.IO;
namespace ServiceStack.Script
{
public interface ISharpPages
{
SharpPage ResolveLayoutPage(SharpPage page, string layout);
SharpPage AddPage(string virtualPath, IVirtualFile file);
SharpPage GetPage(string virtualPath);
SharpPage TryGetPage(string path);
SharpPage OneTimePage(string contents, string ext);
SharpPage ResolveLayoutPage(SharpCodePage page, string layout);
SharpCodePage GetCodePage(string virtualPath);
DateTime GetLastModified(SharpPage page);
}
public partial class SharpPages : ISharpPages
{
public ScriptContext Context { get; }
public SharpPages(ScriptContext context) => this.Context = context;
public static string Layout = "layout";
readonly ConcurrentDictionary<string, SharpPage> pageMap = new ConcurrentDictionary<string, SharpPage>();
public virtual SharpPage ResolveLayoutPage(SharpPage page, string layout)
{
if (page == null)
throw new ArgumentNullException(nameof(page));
if (!page.HasInit)
throw new ArgumentException($"Page {page.File.VirtualPath} has not been initialized");
if (page.IsLayout)
return null;
var layoutWithoutExt = (layout ?? Context.DefaultLayoutPage).LeftPart('.');
var dir = page.File.Directory;
do
{
var layoutPath = (dir.VirtualPath ?? "").CombineWith(layoutWithoutExt);
if (pageMap.TryGetValue(layoutPath, out SharpPage layoutPage))
return layoutPage;
foreach (var format in Context.PageFormats)
{
var layoutFile = dir.GetFile($"{layoutWithoutExt}.{format.Extension}");
if (layoutFile != null)
return AddPage(layoutPath, layoutFile);
}
if (dir.IsRoot)
break;
dir = dir.ParentDirectory;
} while (dir != null);
return null;
}
public virtual SharpPage ResolveLayoutPage(SharpCodePage page, string layout)
{
if (page == null)
throw new ArgumentNullException(nameof(page));
if (!page.HasInit)
throw new ArgumentException($"Page {page.VirtualPath} has not been initialized");
var layoutWithoutExt = (layout ?? Context.DefaultLayoutPage).LeftPart('.');
var lastDirPos = page.VirtualPath.LastIndexOf('/');
var dirPath = lastDirPos >= 0
? page.VirtualPath.Substring(0, lastDirPos)
: null;
var dir = !string.IsNullOrEmpty(dirPath)
? Context.VirtualFiles.GetDirectory(dirPath)
: Context.VirtualFiles.RootDirectory;
do
{
var layoutPath = (dir.VirtualPath ?? "").CombineWith(layoutWithoutExt);
if (pageMap.TryGetValue(layoutPath, out SharpPage layoutPage))
return layoutPage;
foreach (var format in Context.PageFormats)
{
var layoutFile = dir.GetFile($"{layoutWithoutExt}.{format.Extension}");
if (layoutFile != null)
return AddPage(layoutPath, layoutFile);
}
if (dir.IsRoot)
break;
dir = dir.ParentDirectory;
} while (dir != null);
return null;
}
public SharpCodePage GetCodePage(string virtualPath) => Context.GetCodePage(virtualPath)
?? (virtualPath?.Length > 0 && virtualPath[virtualPath.Length - 1] != '/' ? Context.GetCodePage(virtualPath + '/') : null);
public virtual SharpPage AddPage(string virtualPath, IVirtualFile file)
{
return pageMap[virtualPath] = new SharpPage(Context, file);
}
public virtual SharpPage TryGetPage(string path)
{
var santizePath = path.Replace('\\','/').TrimPrefixes("/").LastLeftPart('.');
if (pageMap.TryGetValue(santizePath, out SharpPage page))
return page;
return null;
}
public virtual SharpPage GetPage(string pathInfo)
{
if (string.IsNullOrEmpty(pathInfo))
return null;
var sanitizePath = pathInfo.Replace('\\','/').TrimPrefixes("/");
var isDirectory = sanitizePath.Length == 0 || sanitizePath[sanitizePath.Length - 1] == '/';
SharpPage page = null;
var mappedPath = Context.GetPathMapping(nameof(SharpPages), sanitizePath);
if (mappedPath != null)
{
page = TryGetPage(mappedPath);
if (page != null)
return page;
Context.RemovePathMapping(nameof(SharpPages), mappedPath);
}
var fileNameParts = sanitizePath.LastRightPart('/').SplitOnLast('.');
var ext = fileNameParts.Length > 1 ? fileNameParts[1] : null;
if (ext != null)
{
var registeredPageExt = Context.PageFormats.Any(x => x.Extension == ext);
if (!registeredPageExt)
return null;
}
var filePath = sanitizePath.LastLeftPart('.');
page = TryGetPage(filePath) ?? (!isDirectory ? TryGetPage(filePath + '/') : null);
if (page != null)
return page;
foreach (var format in Context.PageFormats)
{
var file = !isDirectory
? Context.VirtualFiles.GetFile(filePath + "." + format.Extension)
: Context.VirtualFiles.GetFile(filePath + Context.IndexPage + "." + format.Extension);
if (file != null)
{
var pageVirtualPath = file.VirtualPath.WithoutExtension();
Context.SetPathMapping(nameof(SharpPages), sanitizePath, pageVirtualPath);
return AddPage(pageVirtualPath, file);
}
}
if (!isDirectory)
{
var tryFilePath = filePath + '/';
foreach (var format in Context.PageFormats)
{
var file = Context.VirtualFiles.GetFile(tryFilePath + Context.IndexPage + "." + format.Extension);
if (file != null)
{
var pageVirtualPath = file.VirtualPath.WithoutExtension();
Context.SetPathMapping(nameof(SharpPages), sanitizePath, pageVirtualPath);
return AddPage(pageVirtualPath, file);
}
}
}
return null;
}
private static readonly MemoryVirtualFiles TempFiles = new MemoryVirtualFiles();
private static readonly InMemoryVirtualDirectory TempDir = new InMemoryVirtualDirectory(TempFiles, ScriptConstants.TempFilePath);
public virtual SharpPage OneTimePage(string contents, string ext)
{
var memFile = new InMemoryVirtualFile(TempFiles, TempDir)
{
FilePath = Guid.NewGuid().ToString("n") + "." + ext,
TextContents = contents,
};
var page = new SharpPage(Context, memFile);
try
{
page.Init().Wait(); // Safe as Memory Files are non-blocking
return page;
}
catch (AggregateException e)
{
throw e.UnwrapIfSingleException();
}
}
public DateTime GetLastModified(SharpPage page)
{
if (page == null)
throw new ArgumentNullException(nameof(page));
page.File.Refresh();
var maxLastModified = page.File.LastModified;
var layout = page.IsLayout ? null : page.LayoutPage ?? ResolveLayoutPage(page, null);
if (layout != null)
{
var layoutLastModified = GetLastModifiedPage(layout);
if (layoutLastModified > maxLastModified)
maxLastModified = layoutLastModified;
}
var pageLastModified = GetLastModifiedPage(page);
if (pageLastModified > maxLastModified)
maxLastModified = pageLastModified;
return maxLastModified;
}
public DateTime GetLastModifiedPage(SharpPage page)
{
if (page == null)
throw new ArgumentNullException(nameof(page));
page.File.Refresh();
var maxLastModified = page.File.LastModified;
var varFragments = page.PageFragments.OfType<PageVariableFragment>();
foreach (var fragment in varFragments)
{
var filter = fragment.FilterExpressions?.FirstOrDefault();
if (filter?.Name == "partial")
{
if (fragment.InitialValue is string partialPath)
{
Context.TryGetPage(page.VirtualPath, partialPath, out SharpPage partialPage, out _);
if (partialPage == null && partialPath[0] != '_')
Context.TryGetPage(page.VirtualPath, $"_{partialPath}-partial", out partialPage, out _);
maxLastModified = GetMaxLastModified(partialPage?.File, maxLastModified);
if (partialPage?.HasInit == true)
{
var partialLastModified = GetLastModifiedPage(partialPage);
if (partialLastModified > maxLastModified)
maxLastModified = partialLastModified;
}
}
}
else if (filter?.Name != null && Context.FileFilterNames.Contains(filter?.Name))
{
if (fragment.InitialValue is string filePath)
{
var file = Context.ProtectedMethods.ResolveFile(Context.VirtualFiles, page.VirtualPath, filePath);
maxLastModified = GetMaxLastModified(file, maxLastModified);
}
}
var lastFilter = fragment.FilterExpressions?.LastOrDefault();
if (lastFilter?.Name == "selectPartial")
{
if (lastFilter.Arguments.FirstOrDefault() is JsLiteral argLiteral && argLiteral.Value is string partialArg)
{
if (!string.IsNullOrEmpty(partialArg))
{
Context.TryGetPage(page.VirtualPath, partialArg, out SharpPage partialPage, out _);
if (partialPage == null && partialArg[0] != '_')
Context.TryGetPage(page.VirtualPath, $"_{partialArg}-partial", out partialPage, out _);
maxLastModified = GetMaxLastModified(partialPage?.File, maxLastModified);
if (partialPage?.HasInit == true)
{
var partialLastModified = GetLastModifiedPage(partialPage);
if (partialLastModified > maxLastModified)
maxLastModified = partialLastModified;
}
}
}
}
}
return maxLastModified;
}
private DateTime GetMaxLastModified(IVirtualFile file, DateTime maxLastModified)
{
if (file == null)
return maxLastModified;
file.Refresh();
return file.LastModified > maxLastModified
? file.LastModified
: maxLastModified;
}
}
}