#86 - Implement IWebRootFileSystemProvider

This commit is contained in:
Chris Ross 2014-12-04 14:44:16 -08:00
parent d85580649f
commit b883968eef
10 changed files with 87 additions and 11 deletions

View File

@ -10,9 +10,8 @@ namespace Microsoft.AspNet.Hosting
{
private const string DefaultEnvironmentName = "Development";
public HostingEnvironment(IApplicationEnvironment appEnv, IEnumerable<IConfigureHostingEnvironment> configures)
public HostingEnvironment(IEnumerable<IConfigureHostingEnvironment> configures)
{
WebRoot = HostingUtilities.GetWebRoot(appEnv.ApplicationBasePath);
EnvironmentName = DefaultEnvironmentName;
foreach (var configure in configures)
{
@ -21,7 +20,5 @@ namespace Microsoft.AspNet.Hosting
}
public string EnvironmentName { get; set; }
public string WebRoot { get; set; }
}
}

View File

@ -35,6 +35,8 @@ namespace Microsoft.Framework.DependencyInjection
services.TryAdd(describer.Instance<IApplicationLifetime>(new ApplicationLifetime()));
services.TryAdd(describer.Singleton<IWebRootFileSystemProvider, WebRootFileSystemProvider>());
services.AddTypeActivator(configuration);
// TODO: Do we expect this to be provide by the runtime eventually?
services.AddLogging(configuration);

View File

@ -9,7 +9,5 @@ namespace Microsoft.AspNet.Hosting
public interface IHostingEnvironment
{
string EnvironmentName { get; set; }
string WebRoot { get; }
}
}

View File

@ -0,0 +1,18 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.FileSystems;
using Microsoft.Framework.Runtime;
namespace Microsoft.AspNet.Hosting
{
[AssemblyNeutral]
public interface IWebRootFileSystemProvider
{
string WebRoot { get; }
IFileSystem GetFileSystem();
string MapPath(string path);
}
}

View File

@ -0,0 +1,50 @@
// Copyright (c) Microsoft Open Technologies, Inc. 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.AspNet.FileSystems;
using Microsoft.Framework.Runtime;
namespace Microsoft.AspNet.Hosting
{
public class WebRootFileSystemProvider : IWebRootFileSystemProvider
{
private readonly IFileSystem _fileSystem;
public WebRootFileSystemProvider(IApplicationEnvironment appEnvironment)
{
var root = HostingUtilities.GetWebRoot(appEnvironment.ApplicationBasePath);
if (!string.IsNullOrEmpty(root) &&
root[root.Length - 1] != Path.DirectorySeparatorChar)
{
root += Path.DirectorySeparatorChar;
}
WebRoot = root;
_fileSystem = new PhysicalFileSystem(WebRoot);
}
public string WebRoot { get; private set; }
public IFileSystem GetFileSystem()
{
return _fileSystem;
}
public string MapPath(string path)
{
var fullPath = Path.GetFullPath(Path.Combine(WebRoot, path));
// Don't allow MapPath to escape the base root directory
if (!fullPath.StartsWith(WebRoot, StringComparison.OrdinalIgnoreCase))
{
throw new ArgumentException("Invalid path: " + path, nameof(path));
}
return fullPath;
}
}
}

View File

@ -2,6 +2,7 @@
"version": "1.0.0-*",
"description": "ASP.NET 5 core hosting infrastructure and startup logic for web applications.",
"dependencies": {
"Microsoft.AspNet.FileSystems": "1.0.0-*",
"Microsoft.AspNet.PipelineCore": "1.0.0-*",
"Microsoft.Framework.Logging": "1.0.0-*",
"Microsoft.Framework.OptionsModel": "1.0.0-*",

View File

@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting.Server;
@ -52,6 +53,15 @@ namespace Microsoft.AspNet.Hosting
Assert.Equal(1, _startInstances[0].DisposeCalls);
}
[Fact]
public void WebRootCanBeResolvedFromTheProjectJson()
{
var services = HostingServices.Create().BuildServiceProvider();
var provider = services.GetRequiredService<IWebRootFileSystemProvider>();
Assert.Equal(Path.GetFullPath("testroot") + Path.DirectorySeparatorChar, provider.WebRoot);
Assert.True(provider.GetFileSystem().GetFileInfo("TextFile.txt").Exists);
}
public void Initialize(IApplicationBuilder builder)
{

View File

@ -0,0 +1 @@
A text file.

View File

@ -53,12 +53,12 @@ namespace Microsoft.AspNet.TestHost
}
[Fact]
public void WebRootCanBeResolvedFromProjectJson()
public void WebRootCanBeResolvedWhenNotInTheProjectJson()
{
TestServer server = TestServer.Create(app =>
{
var env = app.ApplicationServices.GetRequiredService<IHostingEnvironment>();
Assert.Equal(Path.GetFullPath("testroot"), env.WebRoot);
var provider = app.ApplicationServices.GetRequiredService<IWebRootFileSystemProvider>();
Assert.Equal(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar, provider.WebRoot);
});
}

View File

@ -8,6 +8,5 @@
},
"frameworks": {
"aspnet50": { }
},
"webroot": "testroot"
}
}