#86 - Implement IWebRootFileSystemProvider
This commit is contained in:
parent
d85580649f
commit
b883968eef
|
|
@ -10,9 +10,8 @@ namespace Microsoft.AspNet.Hosting
|
||||||
{
|
{
|
||||||
private const string DefaultEnvironmentName = "Development";
|
private const string DefaultEnvironmentName = "Development";
|
||||||
|
|
||||||
public HostingEnvironment(IApplicationEnvironment appEnv, IEnumerable<IConfigureHostingEnvironment> configures)
|
public HostingEnvironment(IEnumerable<IConfigureHostingEnvironment> configures)
|
||||||
{
|
{
|
||||||
WebRoot = HostingUtilities.GetWebRoot(appEnv.ApplicationBasePath);
|
|
||||||
EnvironmentName = DefaultEnvironmentName;
|
EnvironmentName = DefaultEnvironmentName;
|
||||||
foreach (var configure in configures)
|
foreach (var configure in configures)
|
||||||
{
|
{
|
||||||
|
|
@ -21,7 +20,5 @@ namespace Microsoft.AspNet.Hosting
|
||||||
}
|
}
|
||||||
|
|
||||||
public string EnvironmentName { get; set; }
|
public string EnvironmentName { get; set; }
|
||||||
|
|
||||||
public string WebRoot { get; set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -35,6 +35,8 @@ namespace Microsoft.Framework.DependencyInjection
|
||||||
|
|
||||||
services.TryAdd(describer.Instance<IApplicationLifetime>(new ApplicationLifetime()));
|
services.TryAdd(describer.Instance<IApplicationLifetime>(new ApplicationLifetime()));
|
||||||
|
|
||||||
|
services.TryAdd(describer.Singleton<IWebRootFileSystemProvider, WebRootFileSystemProvider>());
|
||||||
|
|
||||||
services.AddTypeActivator(configuration);
|
services.AddTypeActivator(configuration);
|
||||||
// TODO: Do we expect this to be provide by the runtime eventually?
|
// TODO: Do we expect this to be provide by the runtime eventually?
|
||||||
services.AddLogging(configuration);
|
services.AddLogging(configuration);
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,5 @@ namespace Microsoft.AspNet.Hosting
|
||||||
public interface IHostingEnvironment
|
public interface IHostingEnvironment
|
||||||
{
|
{
|
||||||
string EnvironmentName { get; set; }
|
string EnvironmentName { get; set; }
|
||||||
|
|
||||||
string WebRoot { get; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
"version": "1.0.0-*",
|
"version": "1.0.0-*",
|
||||||
"description": "ASP.NET 5 core hosting infrastructure and startup logic for web applications.",
|
"description": "ASP.NET 5 core hosting infrastructure and startup logic for web applications.",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"Microsoft.AspNet.FileSystems": "1.0.0-*",
|
||||||
"Microsoft.AspNet.PipelineCore": "1.0.0-*",
|
"Microsoft.AspNet.PipelineCore": "1.0.0-*",
|
||||||
"Microsoft.Framework.Logging": "1.0.0-*",
|
"Microsoft.Framework.Logging": "1.0.0-*",
|
||||||
"Microsoft.Framework.OptionsModel": "1.0.0-*",
|
"Microsoft.Framework.OptionsModel": "1.0.0-*",
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Builder;
|
using Microsoft.AspNet.Builder;
|
||||||
using Microsoft.AspNet.Hosting.Server;
|
using Microsoft.AspNet.Hosting.Server;
|
||||||
|
|
@ -52,6 +53,15 @@ namespace Microsoft.AspNet.Hosting
|
||||||
Assert.Equal(1, _startInstances[0].DisposeCalls);
|
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)
|
public void Initialize(IApplicationBuilder builder)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
A text file.
|
||||||
|
|
@ -53,12 +53,12 @@ namespace Microsoft.AspNet.TestHost
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void WebRootCanBeResolvedFromProjectJson()
|
public void WebRootCanBeResolvedWhenNotInTheProjectJson()
|
||||||
{
|
{
|
||||||
TestServer server = TestServer.Create(app =>
|
TestServer server = TestServer.Create(app =>
|
||||||
{
|
{
|
||||||
var env = app.ApplicationServices.GetRequiredService<IHostingEnvironment>();
|
var provider = app.ApplicationServices.GetRequiredService<IWebRootFileSystemProvider>();
|
||||||
Assert.Equal(Path.GetFullPath("testroot"), env.WebRoot);
|
Assert.Equal(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar, provider.WebRoot);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,5 @@
|
||||||
},
|
},
|
||||||
"frameworks": {
|
"frameworks": {
|
||||||
"aspnet50": { }
|
"aspnet50": { }
|
||||||
},
|
}
|
||||||
"webroot": "testroot"
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue