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 6.2KB

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