From f0043f53eefac457f15c5b7a2d7622d993a2bae5 Mon Sep 17 00:00:00 2001 From: Sean Long Date: Sat, 18 Apr 2020 14:57:31 -0400 Subject: [PATCH] OS independent file paths & Utility.PathCombine added System.IO.Path.Combine provides cross-platform support for system paths, however rooted paths discarding of earlier segments Utilities.PathCombine ensures if any parameters start with root chacters does not discard previous Utilities.PathCombine allows appending of "\\" to translate to the correct cross-platform result --- Oqtane.Client/Services/FileService.cs | 2 +- Oqtane.Client/Services/FolderService.cs | 2 +- Oqtane.Server/Controllers/FileController.cs | 6 +++--- Oqtane.Server/Controllers/FolderController.cs | 4 ++-- .../Controllers/ModuleDefinitionController.cs | 8 ++++---- Oqtane.Server/Controllers/UserController.cs | 4 ++-- Oqtane.Server/Infrastructure/DatabaseManager.cs | 4 ++-- .../Infrastructure/InstallationManager.cs | 3 ++- .../SiteTemplates/DefaultSiteTemplate.cs | 2 +- Oqtane.Server/Repository/SiteRepository.cs | 3 ++- Oqtane.Shared/Shared/Utilities.cs | 17 +++++++++++++++++ 11 files changed, 37 insertions(+), 18 deletions(-) diff --git a/Oqtane.Client/Services/FileService.cs b/Oqtane.Client/Services/FileService.cs index c073a460..871a123f 100644 --- a/Oqtane.Client/Services/FileService.cs +++ b/Oqtane.Client/Services/FileService.cs @@ -44,7 +44,7 @@ namespace Oqtane.Services { if (!(folderPath.EndsWith(System.IO.Path.DirectorySeparatorChar) || folderPath.EndsWith(System.IO.Path.AltDirectorySeparatorChar))) { - folderPath = System.IO.Path.Combine(folderPath, " ").TrimEnd(' '); + folderPath = Utilities.PathCombine(folderPath,"\\"); } var path = WebUtility.UrlEncode(folderPath); diff --git a/Oqtane.Client/Services/FolderService.cs b/Oqtane.Client/Services/FolderService.cs index 6c3fd69d..0446b285 100644 --- a/Oqtane.Client/Services/FolderService.cs +++ b/Oqtane.Client/Services/FolderService.cs @@ -40,7 +40,7 @@ namespace Oqtane.Services { if (!(folderPath.EndsWith(System.IO.Path.DirectorySeparatorChar) || folderPath.EndsWith(System.IO.Path.AltDirectorySeparatorChar))) { - folderPath = System.IO.Path.Combine(folderPath, " ").TrimEnd(' '); + folderPath = Utilities.PathCombine(folderPath, "\\"); } var path = WebUtility.UrlEncode(folderPath); diff --git a/Oqtane.Server/Controllers/FileController.cs b/Oqtane.Server/Controllers/FileController.cs index 5b22b062..d6e94644 100644 --- a/Oqtane.Server/Controllers/FileController.cs +++ b/Oqtane.Server/Controllers/FileController.cs @@ -431,12 +431,12 @@ namespace Oqtane.Controllers private string GetFolderPath(Folder folder) { - return Path.Combine(_environment.ContentRootPath, "Content", "Tenants", _tenants.GetTenant().TenantId.ToString(), "Sites", folder.SiteId.ToString(), folder.Path, " ").TrimEnd(' '); + return Utilities.PathCombine(_environment.ContentRootPath, "Content", "Tenants", _tenants.GetTenant().TenantId.ToString(), "Sites", folder.SiteId.ToString(), folder.Path); } private string GetFolderPath(string folder) { - return Path.Combine(_environment.WebRootPath, folder, " ").TrimEnd(' '); + return Utilities.PathCombine(_environment.WebRootPath, folder); } private void CreateDirectory(string folderpath) @@ -448,7 +448,7 @@ namespace Oqtane.Controllers string[] folders = folderpath.Split(separators, StringSplitOptions.RemoveEmptyEntries); foreach (string folder in folders) { - path = Path.Combine(path, folder); + path = Utilities.PathCombine(path, folder,"\\"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); diff --git a/Oqtane.Server/Controllers/FolderController.cs b/Oqtane.Server/Controllers/FolderController.cs index a1e48fc1..55c19039 100644 --- a/Oqtane.Server/Controllers/FolderController.cs +++ b/Oqtane.Server/Controllers/FolderController.cs @@ -110,7 +110,7 @@ namespace Oqtane.Controllers if (string.IsNullOrEmpty(folder.Path) && folder.ParentId != null) { Folder parent = _folders.GetFolder(folder.ParentId.Value); - folder.Path = Path.Combine(parent.Path, folder.Name); + folder.Path = Utilities.PathCombine(parent.Path, folder.Name,"\\"); } folder = _folders.AddFolder(folder); _logger.Log(LogLevel.Information, this, LogFunction.Create, "Folder Added {Folder}", folder); @@ -135,7 +135,7 @@ namespace Oqtane.Controllers if (string.IsNullOrEmpty(folder.Path) && folder.ParentId != null) { Folder parent = _folders.GetFolder(folder.ParentId.Value); - folder.Path = Path.Combine(parent.Path, folder.Name); + folder.Path = Utilities.PathCombine(parent.Path, folder.Name,"\\"); } folder = _folders.UpdateFolder(folder); _logger.Log(LogLevel.Information, this, LogFunction.Update, "Folder Updated {Folder}", folder); diff --git a/Oqtane.Server/Controllers/ModuleDefinitionController.cs b/Oqtane.Server/Controllers/ModuleDefinitionController.cs index 03e1937f..4535b6f7 100644 --- a/Oqtane.Server/Controllers/ModuleDefinitionController.cs +++ b/Oqtane.Server/Controllers/ModuleDefinitionController.cs @@ -189,17 +189,17 @@ namespace Oqtane.Controllers { string rootPath; DirectoryInfo rootFolder = Directory.GetParent(_environment.ContentRootPath); - string templatePath = Path.Combine(rootFolder.FullName, "Oqtane.Client", "Modules", "Admin", "ModuleCreator", "Templates",moduleDefinition.Template," ").TrimEnd(' '); + string templatePath = Utilities.PathCombine(rootFolder.FullName, "Oqtane.Client", "Modules", "Admin", "ModuleCreator", "Templates",moduleDefinition.Template,"\\"); if (moduleDefinition.Template == "internal") { - rootPath = rootFolder.FullName; + rootPath = Utilities.PathCombine(rootFolder.FullName,"\\"); moduleDefinition.ModuleDefinitionName = moduleDefinition.Owner + "." + moduleDefinition.Name + "s.Modules, Oqtane.Client"; moduleDefinition.ServerManagerType = moduleDefinition.Owner + "." + moduleDefinition.Name + "s.Manager." + moduleDefinition.Name + "Manager, Oqtane.Server"; } else { - rootPath = Path.Combine(rootFolder.Parent.FullName , moduleDefinition.Owner + "." + moduleDefinition.Name + "s.Module"); + rootPath = Utilities.PathCombine(rootFolder.Parent.FullName , moduleDefinition.Owner + "." + moduleDefinition.Name + "s.Module","\\"); moduleDefinition.ModuleDefinitionName = moduleDefinition.Owner + "." + moduleDefinition.Name + "s.Modules, " + moduleDefinition.Owner + "." + moduleDefinition.Name + "s.Module.Client"; moduleDefinition.ServerManagerType = moduleDefinition.Owner + "." + moduleDefinition.Name + "s.Manager." + moduleDefinition.Name + "Manager, " + moduleDefinition.Owner + "." + moduleDefinition.Name + "s.Module.Server"; } @@ -218,7 +218,7 @@ namespace Oqtane.Controllers private void ProcessTemplatesRecursively(DirectoryInfo current, string rootPath, string rootFolder, string templatePath, ModuleDefinition moduleDefinition) { // process folder - string folderPath = Path.Combine(rootPath, current.FullName.Replace(templatePath, "")); + string folderPath = Utilities.PathCombine(rootPath, current.FullName.Replace(templatePath, "")); folderPath = folderPath.Replace("[Owner]", moduleDefinition.Owner); folderPath = folderPath.Replace("[Module]", moduleDefinition.Name); if (!Directory.Exists(folderPath)) diff --git a/Oqtane.Server/Controllers/UserController.cs b/Oqtane.Server/Controllers/UserController.cs index dfcb5b48..bfba1459 100644 --- a/Oqtane.Server/Controllers/UserController.cs +++ b/Oqtane.Server/Controllers/UserController.cs @@ -150,7 +150,7 @@ namespace Oqtane.Controllers } // add folder for user - Folder folder = _folders.GetFolder(user.SiteId, "Users"); + Folder folder = _folders.GetFolder(user.SiteId, Utilities.PathCombine("Users","\\")); if (folder != null) { _folders.AddFolder(new Folder @@ -158,7 +158,7 @@ namespace Oqtane.Controllers SiteId = folder.SiteId, ParentId = folder.FolderId, Name = "My Folder", - Path = System.IO.Path.Combine(folder.Path, newUser.UserId.ToString()), + Path = Utilities.PathCombine(folder.Path, newUser.UserId.ToString(),"\\"), Order = 1, IsSystem = true, Permissions = "[{\"PermissionName\":\"Browse\",\"Permissions\":\"[" + newUser.UserId.ToString() + "]\"},{\"PermissionName\":\"View\",\"Permissions\":\"All Users\"},{\"PermissionName\":\"Edit\",\"Permissions\":\"[" + diff --git a/Oqtane.Server/Infrastructure/DatabaseManager.cs b/Oqtane.Server/Infrastructure/DatabaseManager.cs index 838dd582..5ca2eaa2 100644 --- a/Oqtane.Server/Infrastructure/DatabaseManager.cs +++ b/Oqtane.Server/Infrastructure/DatabaseManager.cs @@ -387,14 +387,14 @@ namespace Oqtane.Infrastructure } // add folder for user - var folder = folderRepository.GetFolder(user.SiteId, "Users"); + var folder = folderRepository.GetFolder(user.SiteId, Utilities.PathCombine("Users","\\")); if (folder != null) folderRepository.AddFolder(new Folder { SiteId = folder.SiteId, ParentId = folder.FolderId, Name = "My Folder", - Path = Path.Combine(folder.Path, newUser.UserId.ToString()), + Path = Utilities.PathCombine(folder.Path, newUser.UserId.ToString(),"\\"), Order = 1, IsSystem = true, Permissions = new List diff --git a/Oqtane.Server/Infrastructure/InstallationManager.cs b/Oqtane.Server/Infrastructure/InstallationManager.cs index f37d719e..4cfbaa6b 100644 --- a/Oqtane.Server/Infrastructure/InstallationManager.cs +++ b/Oqtane.Server/Infrastructure/InstallationManager.cs @@ -103,7 +103,8 @@ namespace Oqtane.Infrastructure case ".svg": case ".js": case ".css": - filename = Path.Combine(sourceFolder, entry.FullName.Replace("wwwroot", name)); + string entryPath = Utilities.PathCombine(entry.FullName.Replace("wwwroot", name).Split('/')); + filename = Path.Combine(sourceFolder, entryPath); if (!Directory.Exists(Path.GetDirectoryName(filename))) { Directory.CreateDirectory(Path.GetDirectoryName(filename)); diff --git a/Oqtane.Server/Infrastructure/SiteTemplates/DefaultSiteTemplate.cs b/Oqtane.Server/Infrastructure/SiteTemplates/DefaultSiteTemplate.cs index 150373a2..91f4bc39 100644 --- a/Oqtane.Server/Infrastructure/SiteTemplates/DefaultSiteTemplate.cs +++ b/Oqtane.Server/Infrastructure/SiteTemplates/DefaultSiteTemplate.cs @@ -134,7 +134,7 @@ namespace Oqtane.SiteTemplates if (System.IO.File.Exists(Path.Combine(_environment.WebRootPath, "images", "logo.png"))) { - string folderpath = Path.Combine(_environment.ContentRootPath, "Content", "Tenants", site.TenantId.ToString(), "Sites", site.SiteId.ToString()); + string folderpath = Utilities.PathCombine(_environment.ContentRootPath, "Content", "Tenants", site.TenantId.ToString(), "Sites", site.SiteId.ToString(),"\\"); System.IO.Directory.CreateDirectory(folderpath); if (!System.IO.File.Exists(Path.Combine(folderpath, "logo.png"))) { diff --git a/Oqtane.Server/Repository/SiteRepository.cs b/Oqtane.Server/Repository/SiteRepository.cs index 2d5db9e7..4405536a 100644 --- a/Oqtane.Server/Repository/SiteRepository.cs +++ b/Oqtane.Server/Repository/SiteRepository.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Reflection; using Microsoft.EntityFrameworkCore; @@ -583,7 +584,7 @@ namespace Oqtane.Repository }); _folderRepository.AddFolder(new Folder { - SiteId = site.SiteId, ParentId = folder.FolderId, Name = "Users", Path = "Users", Order = 1, IsSystem = true, + SiteId = site.SiteId, ParentId = folder.FolderId, Name = "Users", Path = Utilities.PathCombine("Users","\\"), Order = 1, IsSystem = true, Permissions = "[{\"PermissionName\":\"Browse\",\"Permissions\":\"Administrators\"},{\"PermissionName\":\"View\",\"Permissions\":\"Administrators\"},{\"PermissionName\":\"Edit\",\"Permissions\":\"Administrators\"}]" }); diff --git a/Oqtane.Shared/Shared/Utilities.cs b/Oqtane.Shared/Shared/Utilities.cs index 662d524d..0f7d603d 100644 --- a/Oqtane.Shared/Shared/Utilities.cs +++ b/Oqtane.Shared/Shared/Utilities.cs @@ -1,5 +1,6 @@ using System; using System.Globalization; +using System.IO; using System.Text; using System.Text.RegularExpressions; @@ -236,5 +237,21 @@ namespace Oqtane.Shared @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-0-9a-z]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$", RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250)); } + + public static string PathCombine(params string[] segments) + { + var separators = new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }; + + for (int i =1;i < segments.Length; i++){ + if(Path.IsPathRooted(segments[i])){ + segments[i] = segments[i].TrimStart(separators); + if(String.IsNullOrEmpty(segments[i])){ + segments[i]=" "; + } + } + } + + return Path.Combine(segments).TrimEnd(); + } } }