-
Notifications
You must be signed in to change notification settings - Fork 927
/
Copy pathAspNetFileSystem.cs
47 lines (43 loc) · 1.51 KB
/
AspNetFileSystem.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
/*
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
using System.IO;
using Microsoft.AspNetCore.Hosting;
namespace React.AspNet
{
/// <summary>
/// Handles file system functionality, such as reading files. Maps all paths from
/// application-relative (~/...) to full paths using ASP.NET's MapPath method.
/// </summary>
public class AspNetFileSystem : FileSystemBase
{
private readonly IHostingEnvironment _hostingEnv;
/// <summary>
/// Initializes a new instance of the <see cref="AspNetFileSystem"/> class.
/// </summary>
/// <param name="hostingEnv">The ASP.NET 5 hosting environment</param>
public AspNetFileSystem(IHostingEnvironment hostingEnv)
{
_hostingEnv = hostingEnv;
}
/// <summary>
/// Converts a path from an application relative path (~/...) to a full filesystem path
/// </summary>
/// <param name="relativePath">App-relative path of the file</param>
/// <returns>Full path of the file</returns>
public override string MapPath(string relativePath)
{
if (relativePath.StartsWith(_hostingEnv.WebRootPath))
{
return relativePath;
}
relativePath = relativePath.TrimStart('~').TrimStart('/');
return Path.Combine(_hostingEnv.WebRootPath, relativePath);
}
}
}