The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Utility.cs 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Dynamic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Web;
  9. namespace Teknik
  10. {
  11. public static class Utility
  12. {
  13. public static dynamic Merge(object item1, object item2)
  14. {
  15. if (item1 == null || item2 == null)
  16. return item1 ?? item2 ?? new ExpandoObject();
  17. dynamic expando = new ExpandoObject();
  18. var result = expando as IDictionary<string, object>;
  19. foreach (System.Reflection.PropertyInfo fi in item1.GetType().GetProperties())
  20. {
  21. result[fi.Name] = fi.GetValue(item1, null);
  22. }
  23. foreach (System.Reflection.PropertyInfo fi in item2.GetType().GetProperties())
  24. {
  25. result[fi.Name] = fi.GetValue(item2, null);
  26. }
  27. return result;
  28. }
  29. public static string RandomString(int length, string allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
  30. {
  31. const int byteSize = 0x100;
  32. var allowedCharSet = new HashSet<char>(allowedChars).ToArray();
  33. if (byteSize < allowedCharSet.Length) throw new ArgumentException(String.Format("allowedChars may contain no more than {0} characters.", byteSize));
  34. // Guid.NewGuid and System.Random are not particularly random. By using a
  35. // cryptographically-secure random number generator, the caller is always
  36. // protected, regardless of use.
  37. using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
  38. {
  39. var result = new StringBuilder();
  40. var buf = new byte[128];
  41. while (result.Length < length)
  42. {
  43. rng.GetBytes(buf);
  44. for (var i = 0; i < buf.Length && result.Length < length; ++i)
  45. {
  46. // Divide the byte into allowedCharSet-sized groups. If the
  47. // random value falls into the last group and the last group is
  48. // too small to choose from the entire allowedCharSet, ignore
  49. // the value in order to avoid biasing the result.
  50. var outOfRangeStart = byteSize - (byteSize % allowedCharSet.Length);
  51. if (outOfRangeStart <= buf[i]) continue;
  52. result.Append(allowedCharSet[buf[i] % allowedCharSet.Length]);
  53. }
  54. }
  55. return result.ToString();
  56. }
  57. }
  58. public static string GenerateUniqueFileName(string path, string extension, int length)
  59. {
  60. if (Directory.Exists(path))
  61. {
  62. string filename = RandomString(length);
  63. string subDir = filename[0].ToString();
  64. path = Path.Combine(path, subDir);
  65. if (!Directory.Exists(path))
  66. {
  67. Directory.CreateDirectory(path);
  68. }
  69. while (File.Exists(Path.Combine(path, string.Format("{0}.{1}", filename, extension))))
  70. {
  71. filename = RandomString(length);
  72. subDir = filename[0].ToString();
  73. path = Path.Combine(path, subDir);
  74. if (!Directory.Exists(path))
  75. {
  76. Directory.CreateDirectory(path);
  77. }
  78. }
  79. return Path.Combine(path, string.Format("{0}.{1}", filename, extension));
  80. }
  81. return string.Empty;
  82. }
  83. public static string GetDefaultExtension(string mimeType)
  84. {
  85. string result;
  86. RegistryKey key;
  87. object value;
  88. key = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type\" + mimeType, false);
  89. value = key != null ? key.GetValue("Extension", null) : null;
  90. result = value != null ? value.ToString() : string.Empty;
  91. return result;
  92. }
  93. public static string GetBytesReadable(long i)
  94. {
  95. // Get absolute value
  96. long absolute_i = (i < 0 ? -i : i);
  97. // Determine the suffix and readable value
  98. string suffix;
  99. double readable;
  100. if (absolute_i >= 0x1000000000000000) // Exabyte
  101. {
  102. suffix = "EB";
  103. readable = (i >> 50);
  104. }
  105. else if (absolute_i >= 0x4000000000000) // Petabyte
  106. {
  107. suffix = "PB";
  108. readable = (i >> 40);
  109. }
  110. else if (absolute_i >= 0x10000000000) // Terabyte
  111. {
  112. suffix = "TB";
  113. readable = (i >> 30);
  114. }
  115. else if (absolute_i >= 0x40000000) // Gigabyte
  116. {
  117. suffix = "GB";
  118. readable = (i >> 20);
  119. }
  120. else if (absolute_i >= 0x100000) // Megabyte
  121. {
  122. suffix = "MB";
  123. readable = (i >> 10);
  124. }
  125. else if (absolute_i >= 0x400) // Kilobyte
  126. {
  127. suffix = "KB";
  128. readable = i;
  129. }
  130. else
  131. {
  132. return i.ToString("0 B"); // Byte
  133. }
  134. // Divide by 1024 to get fractional value
  135. readable = (readable / 1024);
  136. // Return formatted number with suffix
  137. return readable.ToString("0.### ") + suffix;
  138. }
  139. }
  140. }