-
Notifications
You must be signed in to change notification settings - Fork 9.9k
/
FileProviderRazorProjectItem.cs
72 lines (58 loc) · 2.45 KB
/
FileProviderRazorProjectItem.cs
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.Extensions.FileProviders;
namespace Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
{
public class FileProviderRazorProjectItem : RazorProjectItem
{
private string _root;
private string _relativePhysicalPath;
private bool _isRelativePhysicalPathSet;
public FileProviderRazorProjectItem(IFileInfo fileInfo, string basePath, string filePath, string root) : this(fileInfo, basePath, filePath, root, fileKind: null)
{
}
public FileProviderRazorProjectItem(IFileInfo fileInfo, string basePath, string filePath, string root, string fileKind)
{
FileInfo = fileInfo;
BasePath = basePath;
FilePath = filePath;
FileKind = fileKind ?? FileKinds.GetFileKindFromFilePath(filePath);
_root = root;
}
public IFileInfo FileInfo { get; }
public override string BasePath { get; }
public override string FilePath { get; }
public override string FileKind { get; }
public override bool Exists => FileInfo.Exists;
public override string PhysicalPath => FileInfo.PhysicalPath;
public override string RelativePhysicalPath
{
get
{
if (!_isRelativePhysicalPathSet)
{
_isRelativePhysicalPathSet = true;
if (Exists)
{
if (_root != null &&
!string.IsNullOrEmpty(PhysicalPath) &&
PhysicalPath.StartsWith(_root, StringComparison.OrdinalIgnoreCase) &&
PhysicalPath.Length > _root.Length &&
(PhysicalPath[_root.Length] == Path.DirectorySeparatorChar || PhysicalPath[_root.Length] == Path.AltDirectorySeparatorChar))
{
_relativePhysicalPath = PhysicalPath.Substring(_root.Length + 1); // Include leading separator
}
}
}
return _relativePhysicalPath;
}
}
public override Stream Read()
{
return FileInfo.CreateReadStream();
}
}
}