-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblobController.cs
More file actions
184 lines (162 loc) · 6.73 KB
/
Copy pathblobController.cs
File metadata and controls
184 lines (162 loc) · 6.73 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System.Text.Json;
using System.Net;
namespace synk
{
public static class blobController
{
//the dictionary that holds all magic keys and their associated blob filenames
static Dictionary<string, string> blobKeys = new Dictionary<string, string>();
// save blobKeys to file
static public void SaveBlobKeys()
{
string fn = "SaveBlobKeys";
DBg.d(LogLevel.Trace, fn);
string blobKeyFile = Path.Combine(GlobalConfig.synkStore!, "blobKeys.json");
try
{
File.WriteAllText(blobKeyFile, JsonSerializer.Serialize(blobKeys, BlobKeysJsonContext.Default.DictionaryStringString));
}
catch (Exception ex)
{
DBg.d(LogLevel.Error, $"Failed to save blob keys to file {blobKeyFile}. {ex}");
}
}
// load blobKeys from file
static public void LoadBlobKeys()
{
string fn = "LoadBlobKeys";
DBg.d(LogLevel.Trace, fn);
string blobKeyFile = Path.Combine(GlobalConfig.synkStore!, "blobKeys.json");
try
{
if (File.Exists(blobKeyFile))
{
blobKeys = JsonSerializer.Deserialize(File.ReadAllText(blobKeyFile), BlobKeysJsonContext.Default.DictionaryStringString)
?? new Dictionary<string, string>();
}
}
catch (Exception ex)
{
DBg.d(LogLevel.Error, $"Failed to load blob keys from file {blobKeyFile}. {ex}");
}
}
// verify that for each blobKey, the blob file actually exists; return true/false
static public bool VerifyBlobKeyFiles(bool purgeMissing = false)
{
string fn = "VerifyBlobKeyFiles";
DBg.d(LogLevel.Trace, fn);
foreach (var blobKey in blobKeys)
{
string blobFile = Path.Combine(GlobalConfig.synkStore!, blobKey.Value);
if (!File.Exists(blobFile))
{
if (purgeMissing)
{
DBg.d(LogLevel.Warning, $"Blob file {blobFile} does not exist for key {blobKey.Key}. Purging key.");
blobKeys.Remove(blobKey.Key);
SaveBlobKeys();
}
else
DBg.d(LogLevel.Error, $"Blob file {blobFile} does not exist for key {blobKey.Key}");
return false;
}
}
return true;
}
// data written to the blobFile is sanitized; blobFile name is SHA256 hash of the blobKey
static public void WriteBlobFile(string blobKey, byte[] data)
{
string fn = "WriteBlobFile";
DBg.d(LogLevel.Trace, fn);
try
{
// the blob file name is the SHA256 has of the blobKey
string blobFileName = Path.Combine(GlobalConfig.synkStore!, GlobalStatic.SHA256(blobKey));
// check if the blobKey already exists
if (!blobKeys.ContainsKey(blobKey)) {
// add the blobKey and bloFileName to the dictionary
blobKeys[blobKey] = blobFileName;
SaveBlobKeys();
DBg.d(LogLevel.Debug, $"Blob key {blobKey} and {blobFileName} added.");
}
File.WriteAllBytes(blobFileName, data);
// success; return null exception
DBg.d(LogLevel.Debug, $"Blob file {blobFileName} written for key {blobKey}");
}
catch (Exception ex)
{
DBg.d(LogLevel.Error, $"Failed to write blob file for key {blobKey}. {ex}");
throw new Exception($"Failed to write blob file for key {blobKey}.", ex);
}
}
// read the blob file and return the data
static public byte[]? ReadBlobFile(string blobKey)
{
string fn = "ReadBlobFile";
DBg.d(LogLevel.Trace, fn);
// check if the blobKey exists
if (blobKeys.ContainsKey(blobKey))
{
string blobFile = Path.Combine(GlobalConfig.synkStore!, blobKeys[blobKey]);
if (File.Exists(blobFile))
{
return File.ReadAllBytes(blobFile);
}
else
{
DBg.d(LogLevel.Error, $"Blob file {blobFile} does not exist for key {blobKey}");
return null;
}
}
else
{
DBg.d(LogLevel.Error, $"Blob key {blobKey} does not exist");
return null;
}
}
// delete blob file
static public void DeleteBlobFile(string blobKey)
{
string fn = "DeleteBlobFile";
DBg.d(LogLevel.Trace, fn);
// check if the blobKey exists
if (blobKeys.ContainsKey(blobKey))
{
string blobFile = Path.Combine(GlobalConfig.synkStore!, blobKeys[blobKey]);
if (File.Exists(blobFile))
{
File.Delete(blobFile);
SaveBlobKeys();
DBg.d(LogLevel.Debug, $"Blob file {blobFile} deleted for key {blobKey}");
}
else
{
DBg.d(LogLevel.Error, $"Blob file {blobFile} does not exist for key {blobKey}");
throw new FileNotFoundException($"Blob file {blobFile} does not exist for key {blobKey}");
}
}
else
{
DBg.d(LogLevel.Error, $"Blob key {blobKey} does not exist");
throw new KeyNotFoundException($"Blob key {blobKey} does not exist");
}
}
// method that returns true/false if the provided value is Url encoded
static public bool IsUrlEncoded(string original)
{
string fn = "IsUrlEncoded";
DBg.d(LogLevel.Trace, fn);
string decoded = WebUtility.UrlDecode(original);
string reencoded = WebUtility.UrlEncode(decoded);
bool isUrlEncoded = string.Equals(original, reencoded, StringComparison.Ordinal);
return isUrlEncoded;
}
// method that simply decodes the provided value
static public string UrlDecode(string original)
{
string fn = "UrlDecode";
DBg.d(LogLevel.Trace, fn);
return WebUtility.UrlDecode(original);
}
}
}