From b721e75cd28de614e9b044fb9f177f692f884eae Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Wed, 13 Nov 2019 13:22:28 +0100 Subject: [PATCH] Move the config usage out of IOHelper --- .../Configuration/IGlobalSettings.cs | 9 ++++- .../Constants-AppSettings.cs | 20 ++++++++++ .../Configuration/GlobalSettings.cs | 38 +++++++++++++++++++ src/Umbraco.Core/IO/IOHelper.cs | 30 ++++++--------- src/Umbraco.Tests/Views/textpage.cshtml | 4 -- 5 files changed, 77 insertions(+), 24 deletions(-) delete mode 100644 src/Umbraco.Tests/Views/textpage.cshtml 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 4bd4f71447..1e62ae960a 100644 --- a/src/Umbraco.Core/Configuration/GlobalSettings.cs +++ b/src/Umbraco.Core/Configuration/GlobalSettings.cs @@ -378,5 +378,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 fae5de74d1..4b665f9005 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. @@ -289,25 +295,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"); - - //use a tilde character instead of the complete path - string ReturnPath(string settingsKey, string standardPath) - { - //TODO do not use ConfigurationManager directly - var retval = ConfigurationManager.AppSettings[settingsKey]; - - if (string.IsNullOrEmpty(retval)) - retval = standardPath; - - return retval.TrimEnd('/'); - } + public string Umbraco => _globalSettings.UmbracoPath; private string _root; diff --git a/src/Umbraco.Tests/Views/textpage.cshtml b/src/Umbraco.Tests/Views/textpage.cshtml deleted file mode 100644 index d7b4dce307..0000000000 --- a/src/Umbraco.Tests/Views/textpage.cshtml +++ /dev/null @@ -1,4 +0,0 @@ -@inherits Umbraco.Web.Mvc.UmbracoViewPage -@{ - Layout = null; -} \ No newline at end of file