Fixing AspNetCoreHostingEnvironment, cleaning up more IOHelper, adds notes

This commit is contained in:
Shannon
2020-04-03 09:13:55 +11:00
parent 9d320f79da
commit fa2e9c3715
8 changed files with 41 additions and 47 deletions

View File

@@ -9,11 +9,18 @@ namespace Umbraco.Core.IO
char DirSepChar { get; }
string FindFile(string virtualPath);
string ResolveVirtualUrl(string path);
// TODO: This is the same as IHostingEnvironment.ToAbsolute
string ResolveUrl(string virtualPath);
Attempt<string> TryResolveUrl(string virtualPath);
string MapPath(string path);
Attempt<string> TryResolveUrl(string virtualPath);
/// <summary>
/// Maps a virtual path to a physical path in the content root folder (i.e. www)
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
string MapPath(string path);
/// <summary>
/// Verifies that the current filepath matches a directory where the user is allowed to edit a file.
@@ -50,13 +57,7 @@ namespace Umbraco.Core.IO
/// <returns></returns>
string GetRelativePath(string path);
/// <summary>
/// Gets the root path of the application
/// </summary>
/// <remarks>
/// This is (should be) the same as IHostingEnvironment.ApplicationVirtualPath
/// </remarks>
/// </remarks>
// TODO: This is the same as IHostingEnvironment.ApplicationVirtualPath i don't think we want this
string Root
{
get;

View File

@@ -52,21 +52,13 @@ namespace Umbraco.Core.IO
return retval;
}
public string ResolveVirtualUrl(string path)
{
if (string.IsNullOrWhiteSpace(path)) return path;
return path.StartsWith("~/") ? ResolveUrl(path) : path;
}
//Replaces tildes with the root dir
// TODO: This is the same as IHostingEnvironment.ToAbsolute
public string ResolveUrl(string virtualPath)
{
if (virtualPath.StartsWith("~"))
return virtualPath.Replace("~", Root).Replace("//", "/");
else if (Uri.IsWellFormedUriString(virtualPath, UriKind.Absolute))
return virtualPath;
else
return _hostingEnvironment.ToAbsolute(virtualPath, Root);
if (string.IsNullOrWhiteSpace(virtualPath)) return virtualPath;
// TODO: This is a bit odd, the whole "Root" thing is strange, we're passing this into IHostingEnvironment, but it already should know this value in it's ApplicationVirtualPath
return _hostingEnvironment.ToAbsolute(virtualPath, Root);
}
public Attempt<string> TryResolveUrl(string virtualPath)
@@ -77,6 +69,8 @@ namespace Umbraco.Core.IO
return Attempt.Succeed(virtualPath.Replace("~", Root).Replace("//", "/"));
if (Uri.IsWellFormedUriString(virtualPath, UriKind.Absolute))
return Attempt.Succeed(virtualPath);
// TODO: This is a bit odd, the whole "Root" thing is strange, we're passing this into IHostingEnvironment, but it already should know this value in it's ApplicationVirtualPath
return Attempt.Succeed(_hostingEnvironment.ToAbsolute(virtualPath, Root));
}
catch (Exception ex)

View File

@@ -136,14 +136,14 @@ namespace Umbraco.Core.Manifest
// else
// content app can be displayed
return _app ?? (_app = new ContentApp
return _app ??= new ContentApp
{
Alias = _definition.Alias,
Name = _definition.Name,
Icon = _definition.Icon,
View = _ioHelper.ResolveVirtualUrl(_definition.View),
View = _ioHelper.ResolveUrl(_definition.View),
Weight = _definition.Weight
});
};
}
private class ShowRule

View File

@@ -140,7 +140,7 @@ namespace Umbraco.Core.Manifest
private string RewriteVirtualUrl(JValue view)
{
return _ioHelper.ResolveVirtualUrl(view.Value as string);
return _ioHelper.ResolveUrl(view.Value as string);
}
private void PrepareForParameterEditor(JObject jobject, DataEditor target)

View File

@@ -195,21 +195,21 @@ namespace Umbraco.Core.Manifest
// scripts and stylesheets are raw string, must process here
for (var i = 0; i < manifest.Scripts.Length; i++)
manifest.Scripts[i] = _ioHelper.ResolveVirtualUrl(manifest.Scripts[i]);
manifest.Scripts[i] = _ioHelper.ResolveUrl(manifest.Scripts[i]);
for (var i = 0; i < manifest.Stylesheets.Length; i++)
manifest.Stylesheets[i] = _ioHelper.ResolveVirtualUrl(manifest.Stylesheets[i]);
manifest.Stylesheets[i] = _ioHelper.ResolveUrl(manifest.Stylesheets[i]);
foreach (var contentApp in manifest.ContentApps)
{
contentApp.View = _ioHelper.ResolveVirtualUrl(contentApp.View);
contentApp.View = _ioHelper.ResolveUrl(contentApp.View);
}
foreach (var dashboard in manifest.Dashboards)
{
dashboard.View = _ioHelper.ResolveVirtualUrl(dashboard.View);
dashboard.View = _ioHelper.ResolveUrl(dashboard.View);
}
foreach (var gridEditor in manifest.GridEditors)
{
gridEditor.View = _ioHelper.ResolveVirtualUrl(gridEditor.View);
gridEditor.Render = _ioHelper.ResolveVirtualUrl(gridEditor.Render);
gridEditor.View = _ioHelper.ResolveUrl(gridEditor.View);
gridEditor.Render = _ioHelper.ResolveUrl(gridEditor.Render);
}
// add property editors that are also parameter editors, to the parameter editors list

View File

@@ -36,7 +36,7 @@ namespace Umbraco.Core.PropertyEditors
ConfigurationField field;
var attributeView = ioHelper.ResolveVirtualUrl(attribute.View);
var attributeView = ioHelper.ResolveUrl(attribute.View);
// if the field does not have its own type, use the base type
if (attribute.Type == null)
{

View File

@@ -14,15 +14,13 @@ namespace Umbraco.Web.Common.AspNetCore
private readonly IHostingSettings _hostingSettings;
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly IHttpContextAccessor _httpContextAccessor;
private string _localTempPath;
public AspNetCoreHostingEnvironment(IHostingSettings hostingSettings, IWebHostEnvironment webHostEnvironment, IHttpContextAccessor httpContextAccessor)
public AspNetCoreHostingEnvironment(IHostingSettings hostingSettings, IWebHostEnvironment webHostEnvironment)
{
_hostingSettings = hostingSettings ?? throw new ArgumentNullException(nameof(hostingSettings));
_webHostEnvironment = webHostEnvironment ?? throw new ArgumentNullException(nameof(webHostEnvironment));
_httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor));
SiteName = webHostEnvironment.ApplicationName;
ApplicationId = AppDomain.CurrentDomain.Id.ToString();
@@ -88,21 +86,22 @@ namespace Umbraco.Web.Common.AspNetCore
return Path.Combine(_webHostEnvironment.WebRootPath, newPath);
}
// TODO: Need to take into account 'root' here
// TODO: Need to take into account 'root' here, maybe not, Root probably shouldn't be a param, see notes in IOHelper that calls this, we already know ApplicationVirtualPath
public string ToAbsolute(string virtualPath, string root)
{
if (Uri.TryCreate(virtualPath, UriKind.Absolute, out _))
{
if (!virtualPath.StartsWith("~/") && !virtualPath.StartsWith("/"))
throw new InvalidOperationException($"{nameof(virtualPath)} must start with ~/ or /");
if (!root.StartsWith("/"))
throw new InvalidOperationException($"{nameof(virtualPath)} must start with /");
// will occur if it starts with "/"
if (Uri.IsWellFormedUriString(virtualPath, UriKind.Absolute))
return virtualPath;
}
var segment = new PathString(virtualPath.Substring(1));
var applicationPath = _httpContextAccessor.HttpContext.Request.PathBase;
var fullPath = root.EnsureEndsWith('/') + virtualPath.TrimStart("~/");
return applicationPath.Add(segment).Value;
return fullPath;
}
}

View File

@@ -41,7 +41,7 @@ namespace Umbraco.Web.Hosting
return HostingEnvironment.MapPath(path);
}
public string ToAbsolute(string virtualPath, string root) => VirtualPathUtility.ToAbsolute(virtualPath, root);
public string ToAbsolute(string virtualPath, string root) => VirtualPathUtility.ToAbsolute(virtualPath, root.EnsureStartsWith('/'));
public string LocalTempPath