-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilities.cs
67 lines (66 loc) · 2.69 KB
/
Utilities.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
namespace WickerREST
{
internal static class Utilities
{
internal static async System.Threading.Tasks.Task EnsureFileExists(string filePath, string url, bool isAsync = true, bool isBinary = false)
{
if (!File.Exists(filePath))
{
try
{
using (var httpClient = new HttpClient())
{
if (isBinary)
{
var response = await httpClient.GetAsync(url);
if (response.IsSuccessStatusCode)
{
var contentBytes = await response.Content.ReadAsByteArrayAsync();
if(isAsync)
{
await File.WriteAllBytesAsync(filePath, contentBytes);
}
else {
File.WriteAllBytes(filePath, contentBytes);
}
WickerServer.Instance.LogMessage($"Downloaded binary file to {filePath}", 1);
}
else
{
WickerServer.Instance.LogMessage($"Failed to download binary file. Status code: {response.StatusCode}", 1);
}
}
else
{
var contentString = await httpClient.GetStringAsync(url);
if (isAsync)
{
await File.WriteAllTextAsync(filePath, contentString);
}
else
{
File.WriteAllText(filePath, contentString);
}
WickerServer.Instance.LogMessage($"Downloaded text file to {filePath}", 1);
}
}
}
catch (Exception ex)
{
WickerServer.Instance.LogMessage($"Exception during file download: {ex.Message}", 1);
}
}
}
public static string EnsureUniqueKey(IEnumerable<string> existingKeys, string originalKey)
{
string uniqueKey = originalKey;
int counter = 2;
while (existingKeys.Contains(uniqueKey))
{
uniqueKey = $"{originalKey}_{counter}";
counter++;
}
return uniqueKey;
}
}
}