about summary refs log tree commit diff
path: root/MatrixRoomUtils.Desktop/MRUDesktopConfiguration.cs
diff options
context:
space:
mode:
authorEmma@Rory& <root@rory.gay>2023-07-24 20:57:23 +0200
committerEmma@Rory& <root@rory.gay>2023-07-24 20:57:23 +0200
commitcf6f87bea88df3f90e33dfdc5bb16f47b8716054 (patch)
tree92605891ae683d91f1d71a725d1c94f6675be43b /MatrixRoomUtils.Desktop/MRUDesktopConfiguration.cs
parentDebug validation api (diff)
downloadMatrixUtils-cf6f87bea88df3f90e33dfdc5bb16f47b8716054.tar.xz
Start of MRU Desktop
Diffstat (limited to 'MatrixRoomUtils.Desktop/MRUDesktopConfiguration.cs')
-rw-r--r--MatrixRoomUtils.Desktop/MRUDesktopConfiguration.cs44
1 files changed, 44 insertions, 0 deletions
diff --git a/MatrixRoomUtils.Desktop/MRUDesktopConfiguration.cs b/MatrixRoomUtils.Desktop/MRUDesktopConfiguration.cs
new file mode 100644

index 0000000..d321591 --- /dev/null +++ b/MatrixRoomUtils.Desktop/MRUDesktopConfiguration.cs
@@ -0,0 +1,44 @@ +using System.Collections; +using ArcaneLibs.Extensions; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace MatrixRoomUtils.Desktop; + +public class MRUDesktopConfiguration { + private static ILogger<MRUDesktopConfiguration> _logger; + + public MRUDesktopConfiguration(ILogger<MRUDesktopConfiguration> logger, IConfiguration config, HostBuilderContext host) { + _logger = logger; + logger.LogInformation($"Loading configuration for environment: {host.HostingEnvironment.EnvironmentName}..."); + config.GetSection("MRUDesktop").Bind(this); + DataStoragePath = ExpandPath(DataStoragePath); + CacheStoragePath = ExpandPath(CacheStoragePath); + } + + public string DataStoragePath { get; set; } = ""; + public string CacheStoragePath { get; set; } = ""; + + private static string ExpandPath(string path, bool retry = true) { + _logger.LogInformation($"Expanding path `{path}`"); + + if (path.StartsWith("~")) { + path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), path[1..]); + } + + Environment.GetEnvironmentVariables().Cast<DictionaryEntry>().OrderByDescending(x => x.Key.ToString().Length).ToList().ForEach(x => { + path = path.Replace($"${x.Key}", x.Value.ToString()); + }); + + _logger.LogInformation($"Expanded path to `{path}`"); + int tries = 0; + while(retry && path.ContainsAnyOf("~$".Split())) { + if(tries++ > 100) + throw new Exception($"Path `{path}` contains unrecognised environment variables"); + path = ExpandPath(path, false); + } + + return path; + } +}