summaryrefslogtreecommitdiff
path: root/Server/Services/ConfigService.cs
diff options
context:
space:
mode:
authorJake Mannens <jake@asger.xyz>2026-05-22 00:52:16 +1000
committerJake Mannens <jake@asger.xyz>2026-05-23 22:22:55 +1000
commit12eaa5814ef20b0910e8d64a753378b6f6797989 (patch)
tree062cf477c29054e0f089cb80f0cd79a9f3b7ccd9 /Server/Services/ConfigService.cs
parent6de5d7f5364fe1d54703da6d6b7cb08ea26e939f (diff)
Initial commitwasm-initial
Diffstat (limited to 'Server/Services/ConfigService.cs')
-rw-r--r--Server/Services/ConfigService.cs72
1 files changed, 72 insertions, 0 deletions
diff --git a/Server/Services/ConfigService.cs b/Server/Services/ConfigService.cs
new file mode 100644
index 0000000..ac1f155
--- /dev/null
+++ b/Server/Services/ConfigService.cs
@@ -0,0 +1,72 @@
+using HyperBooru.ApiModels;
+
+namespace HyperBooru.Services;
+
+public interface IConfigService {
+ public string DataPath { get; }
+ public string KeyPath { get; }
+ public string DbConnectionString { get; }
+ public string MediaBasePath { get; }
+ public string ThumbnailBasePath { get; }
+ public string ConvertedMediaBasePath { get; }
+ public bool EnableOcr { 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 KeyPath =>
+ Path.Join(DataPath, "keys");
+
+ 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 string ConvertedMediaBasePath =>
+ Path.Join(DataPath, "converted");
+
+ public bool EnableOcr =>
+ bool.TryParse(config["DisableOcr"], out bool x) ? !x : true;
+
+ public ConfigService(IConfiguration config) {
+ this.config = config;
+ InitDirectoryStructure();
+ }
+
+ private void InitDirectoryStructure() {
+ Directory.CreateDirectory(DataPath);
+ Directory.CreateDirectory(MediaBasePath);
+ }
+} \ No newline at end of file