-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathCommonUtils.cs
More file actions
502 lines (423 loc) · 15.4 KB
/
CommonUtils.cs
File metadata and controls
502 lines (423 loc) · 15.4 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.IO.Compression;
using System.Security.Cryptography;
using System.Text;
using ZipFile = System.IO.Compression.ZipFile;
namespace Obj2Tiles.Common
{
public static class CommonUtils
{
public static string RandomString(int length)
{
const string valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
var res = new StringBuilder();
using var rng = RandomNumberGenerator.Create();
var uintBuffer = new byte[sizeof(uint)];
while (length-- > 0)
{
rng.GetBytes(uintBuffer);
var num = BitConverter.ToUInt32(uintBuffer, 0);
res.Append(valid[(int)(num % (uint)valid.Length)]);
}
return res.ToString();
}
private const int BytesToRead = sizeof(long);
public static bool FilesAreEqual(string first, string second)
{
return FilesAreEqual(new FileInfo(first), new FileInfo(second));
}
public static bool FilesAreEqual(FileInfo first, FileInfo second)
{
if (first.Length != second.Length)
return false;
if (string.Equals(first.FullName, second.FullName, StringComparison.OrdinalIgnoreCase))
return true;
var iterations = (int)Math.Ceiling((double)first.Length / BytesToRead);
using var fs1 = first.OpenRead();
using var fs2 = second.OpenRead();
var one = new byte[BytesToRead];
var two = new byte[BytesToRead];
for (var i = 0; i < iterations; i++)
{
fs1.ReadExactly(one, 0, BytesToRead);
fs2.ReadExactly(two, 0, BytesToRead);
if (BitConverter.ToInt64(one, 0) != BitConverter.ToInt64(two, 0))
return false;
}
return true;
}
public static TValue? SafeGetValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
{
return !dictionary.TryGetValue(key, out var value) ? default : value;
}
public static TValueOut? SafeGetValue<TKey, TValue, TValueOut>(this IDictionary<TKey, TValue> dictionary,
TKey key, Func<TValue, TValueOut> selector) where TValueOut : struct
{
return !dictionary.TryGetValue(key, out var value) ? null : selector(value);
}
/// <summary>
/// Ensures that the sqlite database folder exists
/// </summary>
/// <param name="connstr"></param>
public static void EnsureFolderCreated(string connstr)
{
var segments = connstr.Split(';', StringSplitOptions.RemoveEmptyEntries);
foreach (var segment in segments)
{
var fields = segment.Split('=');
if (string.Equals(fields[0], "Data Source", StringComparison.OrdinalIgnoreCase))
{
var dbPath = fields[1];
var folder = Path.GetDirectoryName(dbPath);
if (folder != null)
Directory.CreateDirectory(folder);
}
}
}
/// <summary>
/// Creates the parent folder if it does not exist
/// </summary>
/// <param name="path"></param>
public static void EnsureSafePath(string path)
{
var folder = Path.GetDirectoryName(path);
if (folder != null)
Directory.CreateDirectory(folder);
}
public static string ComputeSha256Hash(string str)
{
return ComputeSha256Hash(Encoding.UTF8.GetBytes(str));
}
public static string ComputeFileHash(string filePath)
{
using var fileStream = File.OpenRead(filePath);
using var sha256Hash = SHA256.Create();
var bytes = sha256Hash.ComputeHash(fileStream);
return ConvertBytesToString(bytes);
}
private static string ConvertBytesToString(byte[] bytes)
{
// Convert byte array to a string
var builder = new StringBuilder();
foreach (var t in bytes)
builder.Append(t.ToString("x2"));
return builder.ToString();
}
public static string ComputeSha256Hash(byte[] rawData)
{
using var sha256Hash = SHA256.Create();
var bytes = sha256Hash.ComputeHash(rawData);
return ConvertBytesToString(bytes);
}
private const string SmartFileCacheFolder = "SmartFileCache";
/// <summary>
/// Downloads a file using a rudimentary cache in temp folder
/// </summary>
/// <param name="url"></param>
/// <param name="path"></param>
public static void SmartDownloadFile(string url, string path)
{
var uri = new Uri(url);
var fileName = uri.Segments.Last();
var smartFileCacheFolder = Path.Combine(Path.GetTempPath(), SmartFileCacheFolder);
if (!Directory.Exists(smartFileCacheFolder))
Directory.CreateDirectory(smartFileCacheFolder);
var cachedFilePath = Path.Combine(smartFileCacheFolder, fileName);
if (!File.Exists(cachedFilePath))
HttpHelper.DownloadFileAsync(url, cachedFilePath).Wait();
File.Copy(cachedFilePath, path, true);
}
/// <summary>
/// Downloads a file using a rudimentary cache in temp folder
/// </summary>
/// <param name="url"></param>
public static byte[] SmartDownloadData(string url)
{
var tmp = Path.GetTempFileName();
SmartDownloadFile(url, tmp);
var data = File.ReadAllBytes(tmp);
File.Delete(tmp);
return data;
}
// Credit: https://stackoverflow.com/questions/12166404/how-do-i-get-folder-size-in-c
public static long GetDirectorySize(string folderPath)
{
DirectoryInfo di = new DirectoryInfo(folderPath);
return di.EnumerateFiles("*", SearchOption.AllDirectories).Sum(fi => fi.Length);
}
public static bool SafeDelete(string path)
{
try
{
File.Delete(path);
return true;
}
catch
{
return false;
}
}
public static bool SafeCopy(string source, string dest, bool overwrite = true)
{
try
{
File.Copy(source, dest, overwrite);
return true;
}
catch
{
return false;
}
}
public static bool SafeDeleteFolder(string path)
{
try
{
Directory.Delete(path, true);
return true;
}
catch
{
return false;
}
}
public static void RemoveEmptyFolders(string folder, bool removeSelf = false)
{
try
{
if (!Directory.Exists(folder)) return;
// Recursive call
Directory.EnumerateDirectories(folder).ToList().ForEach(f => RemoveEmptyFolders(f, true));
// If not empty we don't have to delete it
if (Directory.EnumerateFileSystemEntries(folder).Any()) return;
if (!removeSelf) return;
try
{
Directory.Delete(folder);
}
catch (UnauthorizedAccessException)
{
//
}
catch (DirectoryNotFoundException)
{
//
}
}
catch (UnauthorizedAccessException)
{
//
}
catch (DirectoryNotFoundException)
{
//
}
}
/// <summary>
/// Combines an array of strings into a path using the forward slash as folder separator
/// </summary>
/// <param name="paths">An array of parts of the path.</param>
/// <returns></returns>
public static string SafeCombine(params string[] paths)
{
return Path.Combine(paths.Where(item => item != null).ToArray()).Replace('\\', '/');
}
public static (string, Stream) GetTempStream(int bufferSize = 104857600)
{
var file = Path.Combine(Path.GetTempPath(), "temp-files", RandomString(16));
return (file, new BufferedStream(File.Open(file, FileMode.CreateNew, FileAccess.ReadWrite), bufferSize));
}
public static string[] SafeTreeDelete(string baseTempFolder, int rounds = 3, int delay = 500)
{
var entries = new List<string>(
Directory.EnumerateFileSystemEntries(baseTempFolder, "*", SearchOption.AllDirectories));
for (var n = 0; n < rounds; n++)
{
foreach (var entry in entries.ToArray())
{
try
{
if (Directory.Exists(entry))
{
Directory.Delete(entry, true);
entries.Remove(entry);
continue;
}
if (File.Exists(entry))
{
File.Delete(entry);
entries.Remove(entry);
continue;
}
entries.Remove(entry);
}
catch (Exception ex)
{
Debug.WriteLine("Exception: " + ex.Message);
}
}
if (!entries.Any())
return [];
Thread.Sleep(delay);
}
return entries.ToArray();
}
private static readonly HashSet<string> _compressibleMimeTypes =
[
"text/html",
"text/css",
"text/plain",
"text/xml",
"text/csv",
"text/x-component",
"text/javascript",
"application/pdf",
"application/rtf",
"application/x-sh",
"application/x-tar",
"application/x-javascript",
"application/javascript",
"application/json",
"application/manifest+json",
"application/vnd.api+json",
"application/xml",
"application/xhtml+xml",
"application/rss+xml",
"application/atom+xml",
"application/vnd.ms-fontobject",
"application/x-font-ttf",
"application/x-font-opentype",
"application/x-font-truetype",
"image/svg+xml",
"image/x-icon",
"image/vnd.microsoft.icon",
"font/ttf",
"font/eot",
"font/otf",
"font/opentype"
];
// Credit https://stackoverflow.com/a/11124118
/// <summary>
/// Returns the human-readable file size for an arbitrary, 64-bit file size
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public static string GetBytesReadable(long i)
{
// Get absolute value
var abs = (i < 0 ? -i : i);
// Determine the suffix and readable value
string suffix;
double readable;
switch (abs)
{
// Exabyte
case >= 0x1000000000000000:
suffix = "EB";
readable = (i >> 50);
break;
// Petabyte
case >= 0x4000000000000:
suffix = "PB";
readable = (i >> 40);
break;
// Terabyte
case >= 0x10000000000:
suffix = "TB";
readable = (i >> 30);
break;
// Gigabyte
case >= 0x40000000:
suffix = "GB";
readable = (i >> 20);
break;
// Megabyte
case >= 0x100000:
suffix = "MB";
readable = (i >> 10);
break;
// Kilobyte
case >= 0x400:
suffix = "KB";
readable = i;
break;
default:
return i.ToString("0 B"); // Byte
}
// Divide by 1024 to get fractional value
readable /= 1024;
// Return formatted number with suffix
return readable.ToString("0.### ") + suffix;
}
public static bool Validate<T>(T obj, out ICollection<ValidationResult> results)
{
results = new List<ValidationResult>();
return Validator.TryValidateObject(obj!, new ValidationContext(obj!), results, true);
}
public static string ToErrorString(this IEnumerable<ValidationResult> results)
{
var builder = new StringBuilder();
foreach (var res in results)
{
builder.Append(res.ErrorMessage);
if (res.MemberNames.Any())
{
builder.Append(" (");
builder.Append(string.Join(", ", res.MemberNames));
builder.Append(')');
}
builder.Append("; ");
}
return builder.ToString();
}
/*
public static async Task<FileStream> WaitForFile(string fullPath, FileMode mode, FileAccess access, FileShare share,
int hops = 15, int baseDelay = 10, int incrementDelay = 2)
{
int delay = baseDelay;
for (var hop = 0; hop < hops; hops++)
{
FileStream fs = null;
try
{
fs = new FileStream(fullPath, mode, access, share);
return fs;
}
catch (IOException)
{
if (fs != null)
{
await fs.DisposeAsync();
}
delay *= 2;
await Task.Delay(delay);
}
}
return null;
}*/
public static async Task<FileStream?> WaitForFile(string fullPath, FileMode mode, FileAccess access,
FileShare share,
int delay = 50, int retries = 1200)
{
for (var numTries = 0; numTries < retries; numTries++)
{
FileStream? fs = null;
try
{
fs = new FileStream(fullPath, mode, access, share);
return fs;
}
catch (IOException)
{
if (fs != null)
{
await fs.DisposeAsync();
}
await Task.Delay(delay); // Thread.Sleep (50);
}
}
return null;
}
}
}