forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplatePages.cs
More file actions
311 lines (252 loc) · 11.8 KB
/
Copy pathTemplatePages.cs
File metadata and controls
311 lines (252 loc) · 11.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
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
using System;
using System.Collections.Concurrent;
using System.Linq;
using ServiceStack.IO;
using ServiceStack.VirtualPath;
namespace ServiceStack.Templates
{
public interface ITemplatePages
{
TemplatePage ResolveLayoutPage(TemplatePage page, string layout);
TemplatePage AddPage(string virtualPath, IVirtualFile file);
TemplatePage GetPage(string virtualPath);
TemplatePage TryGetPage(string path);
TemplatePage OneTimePage(string contents, string ext);
TemplatePage ResolveLayoutPage(TemplateCodePage page, string layout);
TemplateCodePage GetCodePage(string virtualPath);
DateTime GetLastModified(TemplatePage page);
}
public class TemplatePages : ITemplatePages
{
public TemplateContext Context { get; }
public TemplatePages(TemplateContext context) => this.Context = context;
public static string Layout = "layout";
readonly ConcurrentDictionary<string, TemplatePage> pageMap = new ConcurrentDictionary<string, TemplatePage>();
public virtual TemplatePage ResolveLayoutPage(TemplatePage 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 TemplatePage 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 TemplatePage ResolveLayoutPage(TemplateCodePage 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 TemplatePage 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 TemplateCodePage GetCodePage(string virtualPath) => Context.GetCodePage(virtualPath)
?? (virtualPath?.Length > 0 && virtualPath[virtualPath.Length - 1] != '/' ? Context.GetCodePage(virtualPath + '/') : null);
public virtual TemplatePage AddPage(string virtualPath, IVirtualFile file)
{
return pageMap[virtualPath] = new TemplatePage(Context, file);
}
public virtual TemplatePage TryGetPage(string path)
{
var santizePath = path.Replace('\\','/').TrimPrefixes("/").LastLeftPart('.');
if (pageMap.TryGetValue(santizePath, out TemplatePage page))
return page;
return null;
}
public virtual TemplatePage GetPage(string pathInfo)
{
if (string.IsNullOrEmpty(pathInfo))
return null;
var santizePath = pathInfo.Replace('\\','/').TrimPrefixes("/");
var isDirectory = santizePath.Length == 0 || santizePath[santizePath.Length - 1] == '/';
TemplatePage page = null;
var mappedPath = Context.GetPathMapping(nameof(TemplatePages), santizePath);
if (mappedPath != null)
{
page = TryGetPage(mappedPath);
if (page != null)
return page;
Context.RemovePathMapping(nameof(TemplatePages), mappedPath);
}
var fileNameParts = santizePath.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 = santizePath.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(TemplatePages), santizePath, 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(TemplatePages), santizePath, pageVirtualPath);
return AddPage(pageVirtualPath, file);
}
}
}
return null;
}
private static readonly MemoryVirtualFiles TempFiles = new MemoryVirtualFiles();
private static readonly InMemoryVirtualDirectory TempDir = new InMemoryVirtualDirectory(TempFiles, TemplateConstants.TempFilePath);
public virtual TemplatePage OneTimePage(string contents, string ext)
{
var memFile = new InMemoryVirtualFile(TempFiles, TempDir)
{
FilePath = Guid.NewGuid().ToString("n") + "." + ext,
TextContents = contents,
};
var page = new TemplatePage(Context, memFile);
try
{
page.Init().Wait(); // Safe as Memory Files are non-blocking
return page;
}
catch (AggregateException e)
{
throw e.UnwrapIfSingleException();
}
}
public DateTime GetLastModified(TemplatePage 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(TemplatePage 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.GetPage(page.VirtualPath, partialPath, out TemplatePage 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.ProtectedFilters.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.GetPage(page.VirtualPath, partialArg, out TemplatePage 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;
}
}
}