From a9c7a7a405c735b0da188314de67822397fd06f2 Mon Sep 17 00:00:00 2001 From: Stephan Date: Mon, 20 Mar 2017 15:26:06 +0100 Subject: [PATCH] U4-9640 - use IFileSystem --- .../Trees/FileSystemTreeController.cs | 107 +++++++----------- .../Trees/PartialViewMacrosTreeController.cs | 15 +-- .../Trees/PartialViewsTreeController.cs | 20 ++-- src/Umbraco.Web/Trees/ScriptTreeController.cs | 15 +-- 4 files changed, 60 insertions(+), 97 deletions(-) diff --git a/src/Umbraco.Web/Trees/FileSystemTreeController.cs b/src/Umbraco.Web/Trees/FileSystemTreeController.cs index 88f322971d..e268aeae2a 100644 --- a/src/Umbraco.Web/Trees/FileSystemTreeController.cs +++ b/src/Umbraco.Web/Trees/FileSystemTreeController.cs @@ -1,92 +1,69 @@ using System; using System.IO; +using System.Linq; using System.Net.Http.Formatting; using umbraco.BusinessLogic.Actions; using Umbraco.Core; using Umbraco.Core.IO; using Umbraco.Core.Services; using Umbraco.Web.Models.Trees; +using System.Web; namespace Umbraco.Web.Trees { public abstract class FileSystemTreeController : TreeController { - protected abstract string FilePath { get; } - protected abstract string FileSearchPattern { get; } + protected abstract IFileSystem2 FileSystem { get; } + protected abstract string[] Extensions { get; } protected abstract string FileIcon { get; } /// /// Inheritors can override this method to modify the file node that is created. /// - /// + /// protected virtual void OnRenderFileNode(ref TreeNode treeNode) { } /// /// Inheritors can override this method to modify the folder node that is created. /// - /// + /// protected virtual void OnRenderFolderNode(ref TreeNode treeNode) { } protected override TreeNodeCollection GetTreeNodes(string id, FormDataCollection queryStrings) { - string orgPath = ""; - string path = ""; - if (!string.IsNullOrEmpty(id) && id != "-1") - { - orgPath = System.Web.HttpUtility.UrlDecode(id); - path = IOHelper.MapPath(FilePath + "/" + orgPath); - orgPath += "/"; - } - else - { - path = IOHelper.MapPath(FilePath); - } + var path = string.IsNullOrEmpty(id) == false && id != Constants.System.Root.ToInvariantString() + ? HttpUtility.UrlDecode(id).TrimStart("/") + : ""; - DirectoryInfo dirInfo = new DirectoryInfo(path); - DirectoryInfo[] dirInfos = dirInfo.GetDirectories(); + var directories = FileSystem.GetDirectories(path); var nodes = new TreeNodeCollection(); - foreach (DirectoryInfo dir in dirInfos) + foreach (var directory in directories) { - if ((dir.Attributes & FileAttributes.Hidden) == 0) - { - var HasChildren = dir.GetFiles().Length > 0 || dir.GetDirectories().Length > 0; - var node = CreateTreeNode(System.Web.HttpUtility.UrlEncode(orgPath + dir.Name), orgPath, queryStrings, dir.Name, "icon-folder", HasChildren); + var hasChildren = FileSystem.GetFiles(directory).Any() || FileSystem.GetDirectories(directory).Any(); - OnRenderFolderNode(ref node); - if(node != null) - nodes.Add(node); - } + var name = Path.GetFileName(directory); + var node = CreateTreeNode(HttpUtility.UrlEncode(directory), path, queryStrings, name, "icon-folder", hasChildren); + OnRenderFolderNode(ref node); + if(node != null) + nodes.Add(node); } //this is a hack to enable file system tree to support multiple file extension look-up //so the pattern both support *.* *.xml and xml,js,vb for lookups - string[] allowedExtensions = new string[0]; - bool filterByMultipleExtensions = FileSearchPattern.Contains(","); - FileInfo[] fileInfo; - - if (filterByMultipleExtensions) + var files = FileSystem.GetFiles(path).Where(x => { - fileInfo = dirInfo.GetFiles(); - allowedExtensions = FileSearchPattern.ToLower().Split(','); - } - else - fileInfo = dirInfo.GetFiles(FileSearchPattern); + var extension = Path.GetExtension(x); + return extension != null && Extensions.Contains(extension.Trim('.'), StringComparer.InvariantCultureIgnoreCase); + }); - foreach (FileInfo file in fileInfo) + foreach (var file in files) { - if ((file.Attributes & FileAttributes.Hidden) == 0) - { - if (filterByMultipleExtensions && Array.IndexOf(allowedExtensions, file.Extension.ToLower().Trim('.')) < 0) - continue; - - var node = CreateTreeNode(System.Web.HttpUtility.UrlEncode(orgPath + file.Name), orgPath, queryStrings, file.Name, FileIcon, false); - - OnRenderFileNode(ref node); - - if(node != null) - nodes.Add(node); - } + var name = Path.GetFileName(file); + var node = CreateTreeNode(HttpUtility.UrlEncode(file), path, queryStrings, name, FileIcon, false); + OnRenderFileNode(ref node); + if (node != null) + nodes.Add(node); } return nodes; @@ -109,27 +86,22 @@ namespace Umbraco.Web.Trees return menu; } - string path; - if (string.IsNullOrEmpty(id) == false) - { - var orgPath = System.Web.HttpUtility.UrlDecode(id); - path = IOHelper.MapPath(FilePath + "/" + orgPath); - } - else - { - path = IOHelper.MapPath(FilePath); - } + var path = string.IsNullOrEmpty(id) == false && id != Constants.System.Root.ToInvariantString() + ? System.Web.HttpUtility.UrlDecode(id).TrimStart("/") + : ""; - var dirInfo = new DirectoryInfo(path); - //check if it's a directory - if (dirInfo.Attributes == FileAttributes.Directory) + var isFile = FileSystem.FileExists(path); + var isDirectory = FileSystem.DirectoryExists(path); + + if (isDirectory) { //set the default to create menu.DefaultMenuAlias = ActionNew.Instance.Alias; //create action menu.Items.Add(Services.TextService.Localize(string.Format("actions/{0}", ActionNew.Instance.Alias))); - - var hasChildren = dirInfo.GetFiles().Length > 0 || dirInfo.GetDirectories().Length > 0; + + var hasChildren = FileSystem.GetFiles(path).Any() || FileSystem.GetDirectories(path).Any(); + //We can only delete folders if it doesn't have any children (folders or files) if (hasChildren == false) { @@ -140,10 +112,9 @@ namespace Umbraco.Web.Trees //refresh action menu.Items.Add(Services.TextService.Localize(string.Format("actions/{0}", ActionRefresh.Instance.Alias)), true); } - //if it's not a directory then we only allow to delete the item - else + else if (isFile) { - //delete action + //if it's not a directory then we only allow to delete the item menu.Items.Add(Services.TextService.Localize(string.Format("actions/{0}", ActionDelete.Instance.Alias))); } diff --git a/src/Umbraco.Web/Trees/PartialViewMacrosTreeController.cs b/src/Umbraco.Web/Trees/PartialViewMacrosTreeController.cs index 9643823e7b..b4cc2eb2fd 100644 --- a/src/Umbraco.Web/Trees/PartialViewMacrosTreeController.cs +++ b/src/Umbraco.Web/Trees/PartialViewMacrosTreeController.cs @@ -10,14 +10,16 @@ namespace Umbraco.Web.Trees [Tree(Constants.Applications.Developer, "partialViewMacros", "Partial View Macro Files", sortOrder: 6)] public class PartialViewMacrosTreeController : FileSystemTreeController { - protected override string FilePath + protected override IFileSystem2 FileSystem { - get { return SystemDirectories.MacroPartials; } + get { return FileSystemProviderManager.Current.MacroPartialsFileSystem; } } - protected override string FileSearchPattern + private static readonly string[] ExtensionsStatic = { "cshtml" }; + + protected override string[] Extensions { - get { return "*.cshtml"; } + get { return ExtensionsStatic; } } protected override string FileIcon @@ -25,11 +27,6 @@ namespace Umbraco.Web.Trees get { return "icon-article"; } } - protected override void OnRenderFileNode(ref TreeNode treeNode) - { - base.OnRenderFileNode(ref treeNode); - } - protected override void OnRenderFolderNode(ref TreeNode treeNode) { //TODO: This isn't the best way to ensure a noop process for clicking a node but it works for now. diff --git a/src/Umbraco.Web/Trees/PartialViewsTreeController.cs b/src/Umbraco.Web/Trees/PartialViewsTreeController.cs index 07a0a557af..264b44ad73 100644 --- a/src/Umbraco.Web/Trees/PartialViewsTreeController.cs +++ b/src/Umbraco.Web/Trees/PartialViewsTreeController.cs @@ -10,25 +10,23 @@ namespace Umbraco.Web.Trees [Tree(Constants.Applications.Settings, "partialViews", "Partial Views", sortOrder: 2)] public class PartialViewsTreeController : FileSystemTreeController { - protected override string FilePath + protected override IFileSystem2 FileSystem { - get { return SystemDirectories.PartialViews; } + get { return FileSystemProviderManager.Current.PartialViewsFileSystem; } } - protected override string FileSearchPattern - { - get { return "*.cshtml"; } - } + private static readonly string[] ExtensionsStatic = { "cshtml" }; + + protected override string[] Extensions + { + get { return ExtensionsStatic; } + } + protected override string FileIcon { get { return "icon-article"; } } - protected override void OnRenderFileNode(ref TreeNode treeNode) - { - base.OnRenderFileNode(ref treeNode); - } - protected override void OnRenderFolderNode(ref TreeNode treeNode) { //TODO: This isn't the best way to ensure a noop process for clicking a node but it works for now. diff --git a/src/Umbraco.Web/Trees/ScriptTreeController.cs b/src/Umbraco.Web/Trees/ScriptTreeController.cs index f491989228..616106a625 100644 --- a/src/Umbraco.Web/Trees/ScriptTreeController.cs +++ b/src/Umbraco.Web/Trees/ScriptTreeController.cs @@ -7,14 +7,16 @@ namespace Umbraco.Web.Trees [Tree(Constants.Applications.Settings, "scripts", "Scripts", sortOrder: 4)] public class ScriptTreeController : FileSystemTreeController { - protected override string FilePath + protected override IFileSystem2 FileSystem { - get { return SystemDirectories.Scripts; } + get { return FileSystemProviderManager.Current.ScriptsFileSystem; } } - protected override string FileSearchPattern + private static readonly string[] ExtensionsStatic = { "js" }; + + protected override string[] Extensions { - get { return "*.js"; } + get { return ExtensionsStatic; } } protected override string FileIcon { @@ -26,10 +28,5 @@ namespace Umbraco.Web.Trees //TODO: This isn't the best way to ensure a noop process for clicking a node but it works for now. treeNode.AdditionalData["jsClickCallback"] = "javascript:void(0);"; } - - protected override void OnRenderFileNode(ref TreeNode treeNode) - { - base.OnRenderFileNode(ref treeNode); - } } }