diff --git a/src/Umbraco.Core/Routing/WebPath.cs b/src/Umbraco.Core/Routing/WebPath.cs new file mode 100644 index 0000000000..0c9ab52e44 --- /dev/null +++ b/src/Umbraco.Core/Routing/WebPath.cs @@ -0,0 +1,33 @@ +using System; +using System.Linq; + +namespace Umbraco.Core.Routing +{ + public class WebPath + { + public static string Combine(params string[] paths) + { + const string separator = "/"; + + if (paths == null) throw new ArgumentNullException(nameof(paths)); + if (!paths.Any()) return string.Empty; + + + + var result = paths[0].TrimEnd(separator); + + if(!(result.StartsWith(separator) || result.StartsWith("~" + separator))) + { + result = separator + result; + } + + for (var index = 1; index < paths.Length; index++) + { + + result +=separator + paths[index].Trim(separator); + } + + return result; + } + } +} diff --git a/src/Umbraco.Infrastructure/Composing/CompositionExtensions/Services.cs b/src/Umbraco.Infrastructure/Composing/CompositionExtensions/Services.cs index 5d73b1659c..980dd1c11e 100644 --- a/src/Umbraco.Infrastructure/Composing/CompositionExtensions/Services.cs +++ b/src/Umbraco.Infrastructure/Composing/CompositionExtensions/Services.cs @@ -8,6 +8,7 @@ using Umbraco.Core.Hosting; using Umbraco.Core.IO; using Umbraco.Core.Logging; using Umbraco.Core.Packaging; +using Umbraco.Core.Routing; using Umbraco.Core.Services; using Umbraco.Core.Services.Implement; @@ -100,9 +101,9 @@ namespace Umbraco.Core.Composing.CompositionExtensions { var hostingEnvironment = container.GetInstance(); var globalSettings = container.GetInstance(); - var mainLangFolder = new DirectoryInfo(hostingEnvironment.MapPathContentRoot(Path.Combine(globalSettings.UmbracoPath , "config","lang"))); + var mainLangFolder = new DirectoryInfo(hostingEnvironment.MapPathContentRoot(WebPath.Combine(globalSettings.UmbracoPath , "config","lang"))); var appPlugins = new DirectoryInfo(hostingEnvironment.MapPathContentRoot(Constants.SystemDirectories.AppPlugins)); - var configLangFolder = new DirectoryInfo(hostingEnvironment.MapPathContentRoot(Path.Combine(Constants.SystemDirectories.Config ,"lang"))); + var configLangFolder = new DirectoryInfo(hostingEnvironment.MapPathContentRoot(WebPath.Combine(Constants.SystemDirectories.Config ,"lang"))); var pluginLangFolders = appPlugins.Exists == false ? Enumerable.Empty() diff --git a/src/Umbraco.Infrastructure/Logging/Viewer/LogViewerConfig.cs b/src/Umbraco.Infrastructure/Logging/Viewer/LogViewerConfig.cs index 8ac49e93c5..e13558b59f 100644 --- a/src/Umbraco.Infrastructure/Logging/Viewer/LogViewerConfig.cs +++ b/src/Umbraco.Infrastructure/Logging/Viewer/LogViewerConfig.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; using Newtonsoft.Json; using Umbraco.Core.Hosting; +using Umbraco.Core.Routing; using Formatting = Newtonsoft.Json.Formatting; namespace Umbraco.Core.Logging.Viewer @@ -10,7 +11,7 @@ namespace Umbraco.Core.Logging.Viewer public class LogViewerConfig : ILogViewerConfig { private readonly IHostingEnvironment _hostingEnvironment; - private static readonly string _pathToSearches = Path.Combine(Constants.SystemDirectories.Config, "logviewer.searches.config.js"); + private static readonly string _pathToSearches = WebPath.Combine(Constants.SystemDirectories.Config, "logviewer.searches.config.js"); private readonly FileInfo _searchesConfig; public LogViewerConfig(IHostingEnvironment hostingEnvironment) diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Core/Routing/WebPathTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Core/Routing/WebPathTests.cs new file mode 100644 index 0000000000..6c2e5f3fcb --- /dev/null +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Core/Routing/WebPathTests.cs @@ -0,0 +1,39 @@ +using System; +using NUnit.Framework; +using Umbraco.Core.Routing; + +namespace Umbraco.Tests.UnitTests.Umbraco.Core.Routing +{ + [TestFixture] + public class WebPathTests + { + [Test] + [TestCase("/umbraco", "config", "lang", ExpectedResult = "/umbraco/config/lang")] + [TestCase("/umbraco", "/config", "/lang", ExpectedResult = "/umbraco/config/lang")] + [TestCase("/umbraco/", "/config/", "/lang/", ExpectedResult = "/umbraco/config/lang")] + [TestCase("/umbraco/", "config/", "lang/", ExpectedResult = "/umbraco/config/lang")] + [TestCase("umbraco", "config", "lang", ExpectedResult = "/umbraco/config/lang")] + [TestCase("umbraco", ExpectedResult = "/umbraco")] + [TestCase("~/umbraco", "config", "lang", ExpectedResult = "~/umbraco/config/lang")] + [TestCase("~/umbraco", "/config", "/lang", ExpectedResult = "~/umbraco/config/lang")] + [TestCase("~/umbraco/", "/config/", "/lang/", ExpectedResult = "~/umbraco/config/lang")] + [TestCase("~/umbraco/", "config/", "lang/", ExpectedResult = "~/umbraco/config/lang")] + [TestCase("~/umbraco", ExpectedResult = "~/umbraco")] + public string Combine(params string[] parts) + { + return WebPath.Combine(parts); + } + + [Test] + public void Combine_must_handle_empty_array() + { + Assert.AreEqual(string.Empty,WebPath.Combine(Array.Empty())); + } + + [Test] + public void Combine_must_handle_null() + { + Assert.Throws(() => WebPath.Combine(null)); + } + } +} diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/StylesheetBuilderTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/StylesheetBuilderTests.cs index 65f4a0fa88..a9104b5dd4 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/StylesheetBuilderTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/StylesheetBuilderTests.cs @@ -1,5 +1,6 @@ using System.IO; using NUnit.Framework; +using Umbraco.Core.Routing; using Umbraco.Tests.Common.Builders; namespace Umbraco.Tests.UnitTests.Umbraco.Tests.Common.Builders @@ -11,7 +12,7 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Tests.Common.Builders public void Is_Built_Correctly() { // Arrange - var testPath = Path.Combine("css", "styles.css"); + var testPath = WebPath.Combine("css", "styles.css"); const string testContent = @"body { color:#000; } .bold {font-weight:bold;}"; var builder = new StylesheetBuilder(); @@ -23,7 +24,7 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Tests.Common.Builders .Build(); // Assert - Assert.AreEqual(testPath, stylesheet.Path); + Assert.AreEqual("\\css\\styles.css", stylesheet.Path); Assert.AreEqual(testContent, stylesheet.Content); } }