Files
Umbraco-CMS/src/Umbraco.Web.Common/AspNetCore/AspNetCoreHostingEnvironment.cs

132 lines
5.1 KiB
C#
Raw Normal View History

2020-02-18 08:32:06 +01:00
using System;
2020-02-20 07:55:24 +01:00
using System.IO;
2020-02-18 08:32:06 +01:00
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Options;
2020-02-18 08:32:06 +01:00
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.Models;
2020-02-18 08:32:06 +01:00
namespace Umbraco.Web.Common.AspNetCore
2020-02-18 08:32:06 +01:00
{
public class AspNetCoreHostingEnvironment : Core.Hosting.IHostingEnvironment
2020-02-18 08:32:06 +01:00
{
private IOptionsMonitor<HostingSettings> _hostingSettings;
2020-02-20 07:55:24 +01:00
private readonly IWebHostEnvironment _webHostEnvironment;
private string _localTempPath;
2020-02-18 08:32:06 +01:00
public AspNetCoreHostingEnvironment(IOptionsMonitor<HostingSettings> hostingSettings, IWebHostEnvironment webHostEnvironment)
{
2020-09-07 15:28:58 +02:00
_hostingSettings = hostingSettings ?? throw new ArgumentNullException(nameof(hostingSettings));
_webHostEnvironment = webHostEnvironment ?? throw new ArgumentNullException(nameof(webHostEnvironment));
2020-02-20 07:55:24 +01:00
SiteName = webHostEnvironment.ApplicationName;
ApplicationId = AppDomain.CurrentDomain.Id.ToString();
2020-02-20 07:55:24 +01:00
ApplicationPhysicalPath = webHostEnvironment.ContentRootPath;
IISVersion = new Version(0, 0); // TODO not necessary IIS
2020-02-18 08:32:06 +01:00
}
/// <inheritdoc/>
public bool IsHosted { get; } = true;
/// <inheritdoc/>
2020-02-18 08:32:06 +01:00
public string SiteName { get; }
/// <inheritdoc/>
2020-02-18 08:32:06 +01:00
public string ApplicationId { get; }
/// <inheritdoc/>
2020-02-18 08:32:06 +01:00
public string ApplicationPhysicalPath { get; }
public string ApplicationServerAddress { get; }
// TODO how to find this, This is a server thing, not application thing.
2020-09-07 15:28:58 +02:00
public string ApplicationVirtualPath => _hostingSettings.CurrentValue.ApplicationVirtualPath?.EnsureStartsWith('/') ?? "/";
/// <inheritdoc/>
2020-09-07 15:28:58 +02:00
public bool IsDebugMode => _hostingSettings.CurrentValue.Debug;
public Version IISVersion { get; }
public string LocalTempPath
{
get
{
if (_localTempPath != null)
{
return _localTempPath;
}
2020-09-07 15:28:58 +02:00
switch (_hostingSettings.CurrentValue.LocalTempStorageLocation)
{
case LocalTempStorage.EnvironmentTemp:
// environment temp is unique, we need a folder per site
// use a hash
// combine site name and application id
// site name is a Guid on Cloud
// application id is eg /LM/W3SVC/123456/ROOT
// the combination is unique on one server
// and, if a site moves from worker A to B and then back to A...
// hopefully it gets a new Guid or new application id?
string hashString = SiteName + "::" + ApplicationId;
string hash = hashString.GenerateHash();
string siteTemp = Path.Combine(Environment.ExpandEnvironmentVariables("%temp%"), "UmbracoData", hash);
return _localTempPath = siteTemp;
default:
return _localTempPath = MapPathContentRoot(Core.Constants.SystemDirectories.TempData);
}
}
}
/// <inheritdoc/>
public string MapPathWebRoot(string path) => MapPath(_webHostEnvironment.WebRootPath, path);
/// <inheritdoc/>
public string MapPathContentRoot(string path) => MapPath(_webHostEnvironment.ContentRootPath, path);
2020-02-18 08:32:06 +01:00
private string MapPath(string root, string path)
{
var newPath = path.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar);
// TODO: This is a temporary error because we switched from IOHelper.MapPath to HostingEnvironment.MapPathXXX
// IOHelper would check if the path passed in started with the root, and not prepend the root again if it did,
// however if you are requesting a path be mapped, it should always assume the path is relative to the root, not
// absolute in the file system. This error will help us find and fix improper uses, and should be removed once
// all those uses have been found and fixed
if (newPath.StartsWith(root))
{
throw new ArgumentException("The path appears to already be fully qualified. Please remove the call to MapPath");
}
return Path.Combine(root, newPath.TrimStart('~', '/', '\\'));
}
/// <inheritdoc/>
public string ToAbsolute(string virtualPath)
2020-02-18 08:32:06 +01:00
{
if (!virtualPath.StartsWith("~/") && !virtualPath.StartsWith("/"))
{
2020-04-03 13:16:01 +11:00
throw new InvalidOperationException($"The value {virtualPath} for parameter {nameof(virtualPath)} must start with ~/ or /");
}
// will occur if it starts with "/"
if (Uri.IsWellFormedUriString(virtualPath, UriKind.Absolute))
{
return virtualPath;
}
string fullPath = ApplicationVirtualPath.EnsureEndsWith('/') + virtualPath.TrimStart('~', '/');
return fullPath;
2020-02-18 08:32:06 +01:00
}
}
}