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;
|
2020-08-21 14:52:47 +01:00
|
|
|
using Microsoft.Extensions.Options;
|
2020-02-18 08:32:06 +01:00
|
|
|
using Umbraco.Core;
|
|
|
|
|
using Umbraco.Core.Configuration;
|
2020-08-21 14:52:47 +01:00
|
|
|
using Umbraco.Core.Configuration.Models;
|
2020-02-18 08:32:06 +01:00
|
|
|
|
2020-03-27 11:39:17 +01:00
|
|
|
namespace Umbraco.Web.Common.AspNetCore
|
2020-02-18 08:32:06 +01:00
|
|
|
{
|
2020-08-21 14:52:47 +01:00
|
|
|
public class AspNetCoreHostingEnvironment : Core.Hosting.IHostingEnvironment
|
2020-02-18 08:32:06 +01:00
|
|
|
{
|
2020-08-21 14:52:47 +01:00
|
|
|
private readonly HostingSettings _hostingSettings;
|
2020-02-20 07:55:24 +01:00
|
|
|
private readonly IWebHostEnvironment _webHostEnvironment;
|
2020-03-27 11:39:17 +01:00
|
|
|
|
2020-02-25 09:45:56 +01:00
|
|
|
private string _localTempPath;
|
2020-02-18 08:32:06 +01:00
|
|
|
|
2020-08-23 23:36:48 +02:00
|
|
|
public AspNetCoreHostingEnvironment(IOptions<HostingSettings> hostingSettings, IWebHostEnvironment webHostEnvironment)
|
2020-08-21 14:52:47 +01:00
|
|
|
: this(hostingSettings.Value, webHostEnvironment)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AspNetCoreHostingEnvironment(HostingSettings 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));
|
2020-03-31 17:27:51 +11:00
|
|
|
_webHostEnvironment = webHostEnvironment ?? throw new ArgumentNullException(nameof(webHostEnvironment));
|
2020-02-20 07:55:24 +01:00
|
|
|
|
|
|
|
|
SiteName = webHostEnvironment.ApplicationName;
|
2020-02-25 09:45:56 +01:00
|
|
|
ApplicationId = AppDomain.CurrentDomain.Id.ToString();
|
2020-02-20 07:55:24 +01:00
|
|
|
ApplicationPhysicalPath = webHostEnvironment.ContentRootPath;
|
2020-02-25 09:45:56 +01:00
|
|
|
|
2020-08-07 00:48:32 +10:00
|
|
|
//TODO how to find this, This is a server thing, not application thing.
|
|
|
|
|
ApplicationVirtualPath = hostingSettings.ApplicationVirtualPath?.EnsureStartsWith('/')
|
|
|
|
|
?? "/";
|
|
|
|
|
|
2020-03-25 15:06:22 +11:00
|
|
|
IISVersion = new Version(0, 0); // TODO not necessary IIS
|
2020-02-18 08:32:06 +01:00
|
|
|
}
|
2020-03-25 15:06:22 +11:00
|
|
|
|
2020-02-27 11:10:43 +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; }
|
2020-05-01 08:26:05 +02:00
|
|
|
public string ApplicationServerAddress { get; }
|
2020-02-27 11:10:43 +01:00
|
|
|
|
|
|
|
|
public string ApplicationVirtualPath { get; }
|
2020-03-31 15:36:25 +02:00
|
|
|
public bool IsDebugMode => _hostingSettings.DebugMode;
|
2020-02-27 11:10:43 +01:00
|
|
|
|
2020-02-27 11:48:38 +01:00
|
|
|
public Version IISVersion { get; }
|
2020-02-25 09:45:56 +01:00
|
|
|
public string LocalTempPath
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_localTempPath != null)
|
|
|
|
|
return _localTempPath;
|
|
|
|
|
|
|
|
|
|
switch (_hostingSettings.LocalTempStorageLocation)
|
|
|
|
|
{
|
|
|
|
|
case LocalTempStorage.AspNetTemp:
|
2020-03-25 15:06:22 +11:00
|
|
|
|
|
|
|
|
// 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");
|
2020-02-25 09:45:56 +01:00
|
|
|
|
|
|
|
|
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:
|
2020-05-04 14:40:11 +02:00
|
|
|
return _localTempPath = MapPathContentRoot("~/App_Data/TEMP");
|
2020-02-25 09:45:56 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-27 11:10:43 +01:00
|
|
|
|
2020-05-04 14:40:11 +02:00
|
|
|
public string MapPathWebRoot(string path)
|
2020-03-25 15:06:22 +11:00
|
|
|
{
|
|
|
|
|
var newPath = path.TrimStart('~', '/').Replace('/', Path.DirectorySeparatorChar);
|
|
|
|
|
return Path.Combine(_webHostEnvironment.WebRootPath, newPath);
|
|
|
|
|
}
|
2020-02-18 08:32:06 +01:00
|
|
|
|
2020-05-04 14:40:11 +02:00
|
|
|
public string MapPathContentRoot(string path)
|
|
|
|
|
{
|
|
|
|
|
var newPath = path.TrimStart('~', '/').Replace('/', Path.DirectorySeparatorChar);
|
|
|
|
|
return Path.Combine(_webHostEnvironment.ContentRootPath, newPath);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 09:17:40 +11:00
|
|
|
public string ToAbsolute(string virtualPath)
|
2020-02-18 08:32:06 +01:00
|
|
|
{
|
2020-04-03 09:13:55 +11: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 /");
|
2020-04-03 09:13:55 +11:00
|
|
|
|
|
|
|
|
// will occur if it starts with "/"
|
|
|
|
|
if (Uri.IsWellFormedUriString(virtualPath, UriKind.Absolute))
|
2020-02-27 11:48:38 +01:00
|
|
|
return virtualPath;
|
|
|
|
|
|
2020-04-03 11:03:06 +11:00
|
|
|
var fullPath = ApplicationVirtualPath.EnsureEndsWith('/') + virtualPath.TrimStart("~/");
|
2020-02-27 11:48:38 +01:00
|
|
|
|
2020-04-03 09:13:55 +11:00
|
|
|
return fullPath;
|
2020-02-18 08:32:06 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|