This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtil.cs
140 lines (130 loc) · 3.65 KB
/
Util.cs
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System.Globalization;
using LiteDB;
namespace WebsiteProxy
{
public static class Util
{
#pragma warning disable CS8602 // Dereference of a possibly null reference.
public static string currentDirectory = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.Parent.FullName;
#pragma warning restore CS8602 // Dereference of a possibly null reference.
public static Dictionary<string, string> environment = ReadEnv(Path.Combine(currentDirectory, "tokens.env"));
public static CultureInfo cultureInfo = CultureInfo.GetCultureInfo("sv-SE");
public static TextInfo textInfo = cultureInfo.TextInfo;
public static LiteDatabase database = new LiteDatabase(Path.Combine(currentDirectory, "database.db"));
public static ILiteCollection<User> users = database.GetCollection<User>("users");
public static Dictionary<string, object> navbarButtons
{
get
{
Dictionary<string, object> projects = new Dictionary<string, object>()
{
{ "/", "Projects" },
{ "/ISOBot/", "ISO-Bot" }
};
foreach (DirectoryInfo repository in new DirectoryInfo(Path.Combine(currentDirectory, "repositories")).GetDirectories())
{
if (TryGetConfigValue(repository.FullName, "name", out string name))
{
projects.Add("/" + repository.Name + "/", name);
}
else
{
projects.Add("/" + repository.Name + "/", repository.Name);
}
}
Dictionary<string, object> buttons = new Dictionary<string, object>()
{
{ "/", "Home page" },
{ "/projects", projects },
{ "/ha/youDumbFuck/", "Some crazy third page" }
};
return buttons;
}
}
public static Dictionary<string, string> ReadEnv(string path)
{
Dictionary<string, string> env = new Dictionary<string, string>();
foreach (string line in File.ReadAllLines(path))
{
if (line.Contains('='))
{
string[] parts = line.Split('=', 2);
if (parts.Length >= 2)
{
env.Add(parts[0], parts[1]);
}
}
}
return env;
}
public static bool IsInCurrentDirectory(string path)
{
return true;
}
public static bool TryGetConfig(string repositoryPath, out Dictionary<string, string> config)
{
string configPath = Path.Combine(repositoryPath, "config.env");
if (!File.Exists(configPath))
{
config = new Dictionary<string, string>();
return false;
}
config = ReadEnv(configPath);
return true;
}
public static bool TryGetConfigValue(string repositoryPath, string key, out string value)
{
if (TryGetConfig(repositoryPath, out Dictionary<string, string> config) && config.TryGetValue(key, out string? nullableValue))
{
value = nullableValue;
return true;
}
value = "";
return false;
}
public static string GrammaticalListing(IEnumerable<object> collection, bool quotes = false)
{
int count = collection.Count();
if (count >= 2)
{
if (quotes)
{
return "\"" + string.Join("\", \"", collection.Take(count - 1)) + "\" and \"" + collection.Last() + "\"";
}
else
{
return string.Join(", ", collection.Take(count - 1)) + " and " + collection.Last();
}
}
else if (count == 1)
{
string? firstString = collection.First().ToString();
if (firstString == null)
{
return "";
}
if (quotes)
{
return "\"" + firstString + "\"";
}
else
{
return firstString;
}
}
return "";
}
public static string[] FindMissingKeys(Dictionary<string, object> dictionary, string[] keys)
{
List<string> missing = new List<string>();
foreach(string key in keys)
{
if (!dictionary.ContainsKey(key))
{
missing.Add(key);
}
}
return missing.ToArray();
}
}
}