Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
99 lines (85 sloc) 3.84 KB
// 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.Collections.Generic;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.Extensions.FileProviders;
namespace Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
{
internal class FileProviderRazorProjectFileSystem : RazorProjectFileSystem
{
private const string RazorFileExtension = ".cshtml";
private readonly RuntimeCompilationFileProvider _fileProvider;
private readonly IWebHostEnvironment _hostingEnvironment;
public FileProviderRazorProjectFileSystem(RuntimeCompilationFileProvider fileProvider, IWebHostEnvironment hostingEnvironment)
{
if (fileProvider == null)
{
throw new ArgumentNullException(nameof(fileProvider));
}
if (hostingEnvironment == null)
{
throw new ArgumentNullException(nameof(hostingEnvironment));
}
_fileProvider = fileProvider;
_hostingEnvironment = hostingEnvironment;
}
public IFileProvider FileProvider => _fileProvider.FileProvider;
[Obsolete("Use GetItem(string path, string fileKind) instead.")]
public override RazorProjectItem GetItem(string path)
{
return GetItem(path, fileKind: null);
}
public override RazorProjectItem GetItem(string path, string fileKind)
{
path = NormalizeAndEnsureValidPath(path);
var fileInfo = FileProvider.GetFileInfo(path);
return new FileProviderRazorProjectItem(fileInfo, basePath: string.Empty, filePath: path, root: _hostingEnvironment.ContentRootPath, fileKind);
}
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 (var fileInfo in directory)
{
if (fileInfo.IsDirectory)
{
var relativePath = prefix + "/" + fileInfo.Name;
var subDirectory = FileProvider.GetDirectoryContents(JoinPath(basePath, relativePath));
var children = EnumerateFiles(subDirectory, basePath, relativePath);
foreach (var child in children)
{
yield return child;
}
}
else if (string.Equals(RazorFileExtension, Path.GetExtension(fileInfo.Name), StringComparison.OrdinalIgnoreCase))
{
var filePath = prefix + "/" + fileInfo.Name;
yield return new FileProviderRazorProjectItem(fileInfo, basePath, filePath: filePath, root: _hostingEnvironment.ContentRootPath);
}
}
}
}
private static string JoinPath(string path1, string path2)
{
var hasTrailingSlash = path1.EndsWith("/", StringComparison.Ordinal);
var hasLeadingSlash = path2.StartsWith("/", StringComparison.Ordinal);
if (hasLeadingSlash && hasTrailingSlash)
{
return path1 + path2.Substring(1);
}
else if (hasLeadingSlash || hasTrailingSlash)
{
return path1 + path2;
}
return path1 + "/" + path2;
}
}
}
You can’t perform that action at this time.