diff --git a/src/Umbraco.Web/Trees/FileSystemTreeController.cs b/src/Umbraco.Web/Trees/FileSystemTreeController.cs index 0d612b5d4e..4f414a941d 100644 --- a/src/Umbraco.Web/Trees/FileSystemTreeController.cs +++ b/src/Umbraco.Web/Trees/FileSystemTreeController.cs @@ -1,18 +1,21 @@ 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.Web.Models.Trees; 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. @@ -28,64 +31,39 @@ namespace Umbraco.Web.Trees protected override TreeNodeCollection GetTreeNodes(string id, FormDataCollection queryStrings) { - var orgPath = ""; - string path; - if (string.IsNullOrEmpty(id) == false && id != "-1") - { - orgPath = 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("/") + : ""; - var dirInfo = new DirectoryInfo(path); - var dirInfos = dirInfo.GetDirectories(); + var directories = FileSystem.GetDirectories(path); var nodes = new TreeNodeCollection(); - foreach (var dir in dirInfos) + foreach (var directory in directories) { - if ((dir.Attributes & FileAttributes.Hidden) != 0) - continue; - - var hasChildren = dir.GetFiles().Length > 0 || dir.GetDirectories().Length > 0; - var node = CreateTreeNode(orgPath + dir.Name, orgPath, queryStrings, dir.Name, "icon-folder", hasChildren); + var hasChildren = FileSystem.GetFiles(directory).Any() || FileSystem.GetDirectories(directory).Any(); + var name = Path.GetFileName(directory); + var node = CreateTreeNode(HttpUtility.UrlEncode(directory), path, queryStrings, name, "icon-folder", hasChildren); OnRenderFolderNode(ref node); - if (node != null) + 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 - var allowedExtensions = new string[0]; - var 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 (var file in fileInfo) + foreach (var file in files) { - if ((file.Attributes & FileAttributes.Hidden) != 0) - continue; - - if (filterByMultipleExtensions && Array.IndexOf(allowedExtensions, file.Extension.ToLower().Trim('.')) < 0) - continue; - - var withoutExt = Path.GetFileNameWithoutExtension(file.Name); - if (withoutExt.IsNullOrWhiteSpace()) - continue; - - var node = CreateTreeNode(orgPath + file.Name, orgPath, queryStrings, file.Name, "icon-file", false); + var withoutExt = Path.GetFileNameWithoutExtension(file); + if (withoutExt.IsNullOrWhiteSpace()) continue; + 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); @@ -111,27 +89,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() + ? 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) { @@ -142,10 +115,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/FileSystemTreeController2.cs b/src/Umbraco.Web/Trees/FileSystemTreeController2.cs deleted file mode 100644 index d43d68ee77..0000000000 --- a/src/Umbraco.Web/Trees/FileSystemTreeController2.cs +++ /dev/null @@ -1,127 +0,0 @@ -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 FileSystemTreeController2 : TreeController - { - 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) - { - var path = string.IsNullOrEmpty(id) == false && id != Constants.System.Root.ToInvariantString() - ? HttpUtility.UrlDecode(id).TrimStart("/") - : ""; - - var directories = FileSystem.GetDirectories(path); - - var nodes = new TreeNodeCollection(); - foreach (var directory in directories) - { - var hasChildren = FileSystem.GetFiles(directory).Any() || FileSystem.GetDirectories(directory).Any(); - - 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 - var files = FileSystem.GetFiles(path).Where(x => - { - var extension = Path.GetExtension(x); - return extension != null && Extensions.Contains(extension.Trim('.'), StringComparer.InvariantCultureIgnoreCase); - }); - - foreach (var file in files) - { - var withoutExt = Path.GetFileNameWithoutExtension(file); - if (withoutExt.IsNullOrWhiteSpace()) continue; - - 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; - } - - protected override MenuItemCollection GetMenuForNode(string id, FormDataCollection queryStrings) - { - var menu = new MenuItemCollection(); - - //if root node no need to visit the filesystem so lets just create the menu and return it - if (id == Constants.System.Root.ToInvariantString()) - { - //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))); - //refresh action - menu.Items.Add(Services.TextService.Localize(string.Format("actions/{0}", ActionRefresh.Instance.Alias)), true); - - return menu; - } - - var path = string.IsNullOrEmpty(id) == false && id != Constants.System.Root.ToInvariantString() - ? HttpUtility.UrlDecode(id).TrimStart("/") - : ""; - - 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 = 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) - { - //delete action - menu.Items.Add(Services.TextService.Localize(string.Format("actions/{0}", ActionDelete.Instance.Alias)), true); - } - - //refresh action - menu.Items.Add(Services.TextService.Localize(string.Format("actions/{0}", ActionRefresh.Instance.Alias)), true); - } - else if (isFile) - { - //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))); - } - - return menu; - } - } -} diff --git a/src/Umbraco.Web/Trees/PartialViewMacrosTreeController.cs b/src/Umbraco.Web/Trees/PartialViewMacrosTreeController.cs index ba4724aead..54c5150676 100644 --- a/src/Umbraco.Web/Trees/PartialViewMacrosTreeController.cs +++ b/src/Umbraco.Web/Trees/PartialViewMacrosTreeController.cs @@ -8,7 +8,7 @@ namespace Umbraco.Web.Trees /// Tree for displaying partial view macros in the developer app /// [Tree(Constants.Applications.Developer, "partialViewMacros", null, sortOrder: 6)] - public class PartialViewMacrosTreeController : FileSystemTreeController2 + public class PartialViewMacrosTreeController : FileSystemTreeController { protected override IFileSystem2 FileSystem { diff --git a/src/Umbraco.Web/Trees/PartialViewsTreeController.cs b/src/Umbraco.Web/Trees/PartialViewsTreeController.cs index bf4ec65a2e..67195cf37c 100644 --- a/src/Umbraco.Web/Trees/PartialViewsTreeController.cs +++ b/src/Umbraco.Web/Trees/PartialViewsTreeController.cs @@ -8,7 +8,7 @@ namespace Umbraco.Web.Trees /// Tree for displaying partial views in the settings app /// [Tree(Constants.Applications.Settings, "partialViews", null, sortOrder: 2)] - public class PartialViewsTreeController : FileSystemTreeController2 + public class PartialViewsTreeController : FileSystemTreeController { protected override IFileSystem2 FileSystem { diff --git a/src/Umbraco.Web/Trees/ScriptTreeController.cs b/src/Umbraco.Web/Trees/ScriptTreeController.cs index cb9954019b..2e4c25c656 100644 --- a/src/Umbraco.Web/Trees/ScriptTreeController.cs +++ b/src/Umbraco.Web/Trees/ScriptTreeController.cs @@ -5,7 +5,7 @@ using Umbraco.Web.Models.Trees; namespace Umbraco.Web.Trees { [Tree(Constants.Applications.Settings, "scripts", null, sortOrder: 4)] - public class ScriptTreeController : FileSystemTreeController2 + public class ScriptTreeController : FileSystemTreeController { protected override IFileSystem2 FileSystem { diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 0eb3a2a27c..0048ef01f9 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -435,7 +435,6 @@ - @@ -713,7 +712,7 @@ - +