forked from statiqdev/Statiq.Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatiqRazorProjectFileSystem.cs
More file actions
101 lines (89 loc) · 4.26 KB
/
StatiqRazorProjectFileSystem.cs
File metadata and controls
101 lines (89 loc) · 4.26 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
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation;
using Microsoft.Extensions.FileProviders;
using Statiq.Common;
namespace Statiq.Razor
{
/// <summary>
/// A RazorProjectFileSystem that lets us use the Statiq file provider while
/// allowing replacement of the stream with document content.
/// </summary>
// See https://github.com/aspnet/AspNetCore/blob/dfba024c7888e9357ce40687e1771953074340fd/src/Mvc/Mvc.Razor.RuntimeCompilation/src/FileProviderRazorProjectFileSystem.cs
internal class StatiqRazorProjectFileSystem : RazorProjectFileSystem
{
private const string RazorFileExtension = ".cshtml";
private readonly Microsoft.Extensions.FileProviders.IFileProvider _fileProvider;
private readonly IWebHostEnvironment _hostingEnvironment;
public StatiqRazorProjectFileSystem(Microsoft.Extensions.FileProviders.IFileProvider fileProvider, IWebHostEnvironment hostingEnviroment)
{
_fileProvider = fileProvider.ThrowIfNull(nameof(fileProvider));
_hostingEnvironment = hostingEnviroment.ThrowIfNull(nameof(hostingEnviroment));
}
public override RazorProjectItem GetItem(string path)
{
return GetItem(path, fileKind: null);
}
public override RazorProjectItem GetItem(string path, string fileKind)
{
path = NormalizeAndEnsureValidPath(path);
IFileInfo fileInfo = _fileProvider.GetFileInfo(path);
return new FileProviderRazorProjectItem(fileInfo, basePath: string.Empty, filePath: path, root: _hostingEnvironment.ContentRootPath, fileKind);
}
public RazorProjectItem GetItem(string path, IDocument document)
{
FileProviderRazorProjectItem projectItem = (FileProviderRazorProjectItem)GetItem(path, fileKind: null);
return new FileProviderRazorProjectItem(
new DocumentFileInfo(projectItem.FileInfo, document),
projectItem.BasePath,
projectItem.FilePath,
_hostingEnvironment.ContentRootPath);
}
public override IEnumerable<RazorProjectItem> EnumerateItems(string path)
{
path = NormalizeAndEnsureValidPath(path);
return EnumerateFiles(_fileProvider.GetDirectoryContents(path), path, prefix: string.Empty);
}
private IEnumerable<RazorProjectItem> EnumerateFiles(IDirectoryContents directory, string basePath, string prefix)
{
if (directory.Exists)
{
foreach (IFileInfo fileInfo in directory)
{
if (fileInfo.IsDirectory)
{
string relativePath = prefix + "/" + fileInfo.Name;
IDirectoryContents subDirectory = _fileProvider.GetDirectoryContents(JoinPath(basePath, relativePath));
IEnumerable<RazorProjectItem> children = EnumerateFiles(subDirectory, basePath, relativePath);
foreach (RazorProjectItem child in children)
{
yield return child;
}
}
else if (string.Equals(RazorFileExtension, Path.GetExtension(fileInfo.Name), StringComparison.OrdinalIgnoreCase))
{
string filePath = prefix + "/" + fileInfo.Name;
yield return new FileProviderRazorProjectItem(fileInfo, basePath, filePath: filePath, root: _hostingEnvironment.ContentRootPath);
}
}
}
}
private static string JoinPath(string path1, string path2)
{
bool hasTrailingSlash = path1.EndsWith("/", StringComparison.Ordinal);
bool hasLeadingSlash = path2.StartsWith("/", StringComparison.Ordinal);
if (hasLeadingSlash && hasTrailingSlash)
{
return path1 + path2.Substring(1);
}
else if (hasLeadingSlash || hasTrailingSlash)
{
return path1 + path2;
}
return path1 + "/" + path2;
}
}
}