first round of refactoring

This commit is contained in:
Søren Gregersen
2019-11-13 21:00:54 +01:00
parent 149d128d03
commit 7b19ab005b
51 changed files with 388 additions and 242 deletions

View File

@@ -11,6 +11,8 @@ namespace Umbraco.Core.IO
{
public class PhysicalFileSystem : IFileSystem
{
private readonly IIOHelper _ioHelper;
// the rooted, filesystem path, using directory separator chars, NOT ending with a separator
// eg "c:" or "c:\path\to\site" or "\\server\path"
private readonly string _rootPath;
@@ -26,28 +28,32 @@ namespace Umbraco.Core.IO
// virtualRoot should be "~/path/to/root" eg "~/Views"
// the "~/" is mandatory.
public PhysicalFileSystem(string virtualRoot)
public PhysicalFileSystem(string virtualRoot, IIOHelper ioHelper)
{
if (virtualRoot == null) throw new ArgumentNullException("virtualRoot");
if (virtualRoot.StartsWith("~/") == false)
throw new ArgumentException("The virtualRoot argument must be a virtual path and start with '~/'");
_rootPath = EnsureDirectorySeparatorChar(Current.IOHelper.MapPath(virtualRoot)).TrimEnd(Path.DirectorySeparatorChar);
_ioHelper = ioHelper;
_rootPath = EnsureDirectorySeparatorChar(ioHelper.MapPath(virtualRoot)).TrimEnd(Path.DirectorySeparatorChar);
_rootPathFwd = EnsureUrlSeparatorChar(_rootPath);
_rootUrl = EnsureUrlSeparatorChar(Current.IOHelper.ResolveUrl(virtualRoot)).TrimEnd('/');
_rootUrl = EnsureUrlSeparatorChar(ioHelper.ResolveUrl(virtualRoot)).TrimEnd('/');
}
public PhysicalFileSystem(string rootPath, string rootUrl)
public PhysicalFileSystem(string rootPath, string rootUrl, IIOHelper ioHelper)
{
if (string.IsNullOrEmpty(rootPath)) throw new ArgumentNullOrEmptyException(nameof(rootPath));
if (string.IsNullOrEmpty(rootUrl)) throw new ArgumentNullOrEmptyException(nameof(rootUrl));
if (rootPath.StartsWith("~/")) throw new ArgumentException("The rootPath argument cannot be a virtual path and cannot start with '~/'");
_ioHelper = ioHelper;
// rootPath should be... rooted, as in, it's a root path!
if (Path.IsPathRooted(rootPath) == false)
{
// but the test suite App.config cannot really "root" anything so we have to do it here
var localRoot = Current.IOHelper.GetRootDirectorySafe();
var localRoot = _ioHelper.GetRootDirectorySafe();
rootPath = Path.Combine(localRoot, rootPath);
}
@@ -257,12 +263,12 @@ namespace Umbraco.Core.IO
// if it starts with the root url, strip it and trim the starting slash to make it relative
// eg "/Media/1234/img.jpg" => "1234/img.jpg"
if (Current.IOHelper.PathStartsWith(path, _rootUrl, '/'))
if (_ioHelper.PathStartsWith(path, _rootUrl, '/'))
return path.Substring(_rootUrl.Length).TrimStart('/');
// if it starts with the root path, strip it and trim the starting slash to make it relative
// eg "c:/websites/test/root/Media/1234/img.jpg" => "1234/img.jpg"
if (Current.IOHelper.PathStartsWith(path, _rootPathFwd, '/'))
if (_ioHelper.PathStartsWith(path, _rootPathFwd, '/'))
return path.Substring(_rootPathFwd.Length).TrimStart('/');
// unchanged - what else?
@@ -292,7 +298,7 @@ namespace Umbraco.Core.IO
path = GetRelativePath(path);
// if not already rooted, combine with the root path
if (Current.IOHelper.PathStartsWith(path, _rootPath, Path.DirectorySeparatorChar) == false)
if (_ioHelper.PathStartsWith(path, _rootPath, Path.DirectorySeparatorChar) == false)
path = Path.Combine(_rootPath, path);
// sanitize - GetFullPath will take care of any relative
@@ -303,7 +309,7 @@ namespace Umbraco.Core.IO
// at that point, path is within legal parts of the filesystem, ie we have
// permissions to reach that path, but it may nevertheless be outside of
// our root path, due to relative segments, so better check
if (Current.IOHelper.PathStartsWith(path, _rootPath, Path.DirectorySeparatorChar))
if (_ioHelper.PathStartsWith(path, _rootPath, Path.DirectorySeparatorChar))
{
// this says that 4.7.2 supports long paths - but Windows does not
// https://docs.microsoft.com/en-us/dotnet/api/system.io.pathtoolongexception?view=netframework-4.7.2