From c751709b1b4fe6f16fd84647e8e071455e7b78d6 Mon Sep 17 00:00:00 2001 From: Jake Mannens Date: Tue, 17 Mar 2026 03:04:36 +1100 Subject: v0.1a --- Services/ConfigService.cs | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Services/ConfigService.cs (limited to 'Services/ConfigService.cs') diff --git a/Services/ConfigService.cs b/Services/ConfigService.cs new file mode 100644 index 0000000..b42b80c --- /dev/null +++ b/Services/ConfigService.cs @@ -0,0 +1,58 @@ +namespace HyperBooru.Services; + +public interface IConfigService { + public string DataPath { get; } + public string DbConnectionString { get; } + public string MediaBasePath { get; } + public string ThumbnailBasePath { get; } +} + +public class ConfigService : IConfigService { + private IConfiguration config; + + private const string AppName = "HyperBooru"; + + public string DataPath { + get { + #if DEBUG + return "Data"; + #else + string? path = config["DataPath"]; + if(path is not null) + return path; + + switch(Environment.OSVersion.Platform) { + case PlatformID.Win32NT: + return Path.Join( + Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), + AppName); + case PlatformID.Unix: + return $"/var/lib/{AppName.ToLower()}"; + default: + throw new NotImplementedException( + $"Unknown Operating System: {Environment.OSVersion.Platform}"); + } + #endif + } + } + + public string DbConnectionString => + config.GetConnectionString("DefaultConnection") ?? + throw new HBException("Unable to get default connection string"); + + public string MediaBasePath => + Path.Join(DataPath, "media"); + + public string ThumbnailBasePath => + Path.Join(DataPath, "thumb"); + + public ConfigService(IConfiguration config) { + this.config = config; + InitDirectoryStructure(); + } + + private void InitDirectoryStructure() { + Directory.CreateDirectory(DataPath); + Directory.CreateDirectory(MediaBasePath); + } +} \ No newline at end of file -- cgit v1.3