summaryrefslogtreecommitdiff
path: root/Services/ConfigService.cs
blob: 752f9f5dbc198a0f3363d71e216c97e0fa88b61d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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);
    }
}