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

108 lines
4.3 KiB
C#
Raw Normal View History

2020-02-18 08:32:06 +01:00
using System;
using System.Globalization;
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.AspNetCore.Http;
2020-02-18 08:32:06 +01:00
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
2020-02-18 08:32:06 +01:00
namespace Umbraco.Web.Common.AspNetCore
2020-02-18 08:32:06 +01:00
{
public class AspNetCoreHostingEnvironment : Umbraco.Core.Hosting.IHostingEnvironment
{
private readonly IHostingSettings _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(IHostingSettings hostingSettings, IWebHostEnvironment webHostEnvironment)
2020-02-18 08:32:06 +01:00
{
2020-02-20 07:55:24 +01: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;
2020-02-20 07:55:24 +01:00
ApplicationVirtualPath = "/"; //TODO how to find this, This is a server thing, not application thing.
ApplicationServerAddress = "https://localhost:44354"; // TODO how to find this?
IISVersion = new Version(0, 0); // TODO not necessary IIS
2020-02-18 08:32:06 +01:00
}
public bool IsHosted { get; } = true;
2020-02-18 08:32:06 +01:00
public string SiteName { get; }
public string ApplicationId { get; }
public string ApplicationPhysicalPath { get; }
public string ApplicationServerAddress { get; }
public string ApplicationVirtualPath { get; }
public bool IsDebugMode => _hostingSettings.DebugMode;
public Version IISVersion { get; }
public string LocalTempPath
{
get
{
if (_localTempPath != null)
return _localTempPath;
switch (_hostingSettings.LocalTempStorageLocation)
{
case LocalTempStorage.AspNetTemp:
// TODO: I don't think this is correct? but also we probably can remove AspNetTemp as an option entirely
// since this is legacy and we shouldn't use it
return _localTempPath = System.IO.Path.Combine(Path.GetTempPath(), ApplicationId, "UmbracoData");
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?
var hashString = SiteName + "::" + ApplicationId;
var hash = hashString.GenerateHash();
var siteTemp = System.IO.Path.Combine(Environment.ExpandEnvironmentVariables("%temp%"), "UmbracoData", hash);
return _localTempPath = siteTemp;
//case LocalTempStorage.Default:
//case LocalTempStorage.Unknown:
default:
return _localTempPath = MapPath("~/App_Data/TEMP");
}
}
}
public string MapPath(string path)
{
var newPath = path.TrimStart('~', '/').Replace('/', Path.DirectorySeparatorChar);
return Path.Combine(_webHostEnvironment.WebRootPath, newPath);
}
2020-02-18 08:32:06 +01:00
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;
var fullPath = ApplicationVirtualPath.EnsureEndsWith('/') + virtualPath.TrimStart("~/");
return fullPath;
2020-02-18 08:32:06 +01:00
}
}
}