diff --git a/src/Umbraco.Abstractions/Configuration/IGlobalSettings.cs b/src/Umbraco.Abstractions/Configuration/IGlobalSettings.cs index b96434c30c..48371bd11b 100644 --- a/src/Umbraco.Abstractions/Configuration/IGlobalSettings.cs +++ b/src/Umbraco.Abstractions/Configuration/IGlobalSettings.cs @@ -1,7 +1,7 @@ namespace Umbraco.Core.Configuration { /// - /// Contains general settings information for the entire Umbraco instance based on information from web.config appsettings + /// Contains general settings information for the entire Umbraco instance based on information from web.config appsettings /// public interface IGlobalSettings { @@ -21,7 +21,7 @@ /// Gets the path to umbraco's root directory (/umbraco by default). /// string Path { get; } - + /// /// Gets or sets the configuration status. This will return the version number of the currently installed umbraco instance. /// @@ -67,5 +67,10 @@ /// Gets the location of temporary files. /// string LocalTempPath { get; } + + string UmbracoPath { get; } + string UmbracoCssPath { get; } + string UmbracoScriptsPath { get; } + string UmbracoMediaPath { get; } } } diff --git a/src/Umbraco.Abstractions/Constants-AppSettings.cs b/src/Umbraco.Abstractions/Constants-AppSettings.cs index 509be46b61..beaffef5b4 100644 --- a/src/Umbraco.Abstractions/Constants-AppSettings.cs +++ b/src/Umbraco.Abstractions/Constants-AppSettings.cs @@ -46,6 +46,26 @@ namespace Umbraco.Core /// public const string ReservedUrls = "Umbraco.Core.ReservedUrls"; + /// + /// The path of backoffice. + /// + public const string UmbracoPath = "umbracoPath"; + + /// + /// The path of the stylesheet folder. + /// + public const string UmbracoCssPath = "umbracoCssPath"; + + /// + /// The path of script folder. + /// + public const string UmbracoScriptsPath = "umbracoScriptsPath"; + + /// + /// The path of media folder. + /// + public const string UmbracoMediaPath = "umbracoMediaPath"; + /// /// The reserved paths from web.config /// diff --git a/src/Umbraco.Core/Configuration/GlobalSettings.cs b/src/Umbraco.Core/Configuration/GlobalSettings.cs index 76d6c2e1c1..00168cf028 100644 --- a/src/Umbraco.Core/Configuration/GlobalSettings.cs +++ b/src/Umbraco.Core/Configuration/GlobalSettings.cs @@ -384,5 +384,43 @@ namespace Umbraco.Core.Configuration } } } + + private string _umbracoMediaPath = null; + public string UmbracoMediaPath => GetterWithDefaultValue(Constants.AppSettings.UmbracoMediaPath, "~/umbraco", ref _umbracoMediaPath); + + private string _umbracoScriptsPath = null; + public string UmbracoScriptsPath => GetterWithDefaultValue(Constants.AppSettings.UmbracoScriptsPath, "~/umbraco", ref _umbracoScriptsPath); + + private string _umbracoCssPath = null; + public string UmbracoCssPath => GetterWithDefaultValue(Constants.AppSettings.UmbracoCssPath, "~/umbraco", ref _umbracoCssPath); + + private string _umbracoPath = null; + public string UmbracoPath => GetterWithDefaultValue(Constants.AppSettings.UmbracoPath, "~/umbraco", ref _umbracoPath); + + private T GetterWithDefaultValue(string appSettingKey, T defaultValue, ref T backingField) + { + if (backingField != null) return backingField; + + if (ConfigurationManager.AppSettings.ContainsKey(appSettingKey)) + { + try + { + var value = ConfigurationManager.AppSettings[appSettingKey]; + + backingField = (T)Convert.ChangeType(value, typeof(T)); + } + catch + { + /* ignore and use default value */ + backingField = defaultValue; + } + } + else + { + backingField = defaultValue; + } + + return backingField; + } } } diff --git a/src/Umbraco.Core/IO/IOHelper.cs b/src/Umbraco.Core/IO/IOHelper.cs index 370f9d1f7a..8ab9c93b31 100644 --- a/src/Umbraco.Core/IO/IOHelper.cs +++ b/src/Umbraco.Core/IO/IOHelper.cs @@ -3,16 +3,22 @@ using System.Collections.Generic; using System.Globalization; using System.Reflection; using System.IO; -using System.Configuration; using System.Linq; using System.Web; using System.Web.Hosting; +using Umbraco.Core.Configuration; namespace Umbraco.Core.IO { public class IOHelper : IIOHelper { - internal static IIOHelper Default { get; } = new IOHelper(); + private readonly IGlobalSettings _globalSettings; + internal static IIOHelper Default { get; } = new IOHelper(new GlobalSettings()); + + public IOHelper(IGlobalSettings globalSettings) + { + _globalSettings = globalSettings; + } /// /// Gets or sets a value forcing Umbraco to consider it is non-hosted. @@ -114,23 +120,6 @@ namespace Umbraco.Core.IO return MapPath(path, true); } - //use a tilde character instead of the complete path - private string ReturnPath(string settingsKey, string standardPath, bool useTilde) - { - var retval = ConfigurationManager.AppSettings[settingsKey]; - - if (string.IsNullOrEmpty(retval)) - retval = standardPath; - - return retval.TrimEnd('/'); - } - - private string ReturnPath(string settingsKey, string standardPath) - { - return ReturnPath(settingsKey, standardPath, false); - - } - /// /// Verifies that the current filepath matches a directory where the user is allowed to edit a file. /// @@ -304,13 +293,13 @@ namespace Umbraco.Core.IO } - public string Media => ReturnPath("umbracoMediaPath", "~/media"); + public string Media => _globalSettings.UmbracoMediaPath; - public string Scripts => ReturnPath("umbracoScriptsPath", "~/scripts"); + public string Scripts => _globalSettings.UmbracoScriptsPath; - public string Css => ReturnPath("umbracoCssPath", "~/css"); + public string Css => _globalSettings.UmbracoCssPath; - public string Umbraco => ReturnPath("umbracoPath", "~/umbraco"); + public string Umbraco => _globalSettings.UmbracoPath; private string _root;