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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. while (File.Exists(Path.Combine(path, string.Format("{0}.{1}", filename, extension))))
  64. {
  65. filename = RandomString(length);
  66. }
  67. return Path.Combine(path, string.Format("{0}.{1}", filename, extension));
  68. }
  69. return string.Empty;
  70. }
  71. public static string GetDefaultExtension(string mimeType)
  72. {
  73. string result;
  74. RegistryKey key;
  75. object value;
  76. key = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type\" + mimeType, false);
  77. value = key != null ? key.GetValue("Extension", null) : null;
  78. result = value != null ? value.ToString() : string.Empty;
  79. return result;
  80. }
  81. public static string GetBytesReadable(long i)
  82. {
  83. // Get absolute value
  84. long absolute_i = (i < 0 ? -i : i);
  85. // Determine the suffix and readable value
  86. string suffix;
  87. double readable;
  88. if (absolute_i >= 0x1000000000000000) // Exabyte
  89. {
  90. suffix = "EB";
  91. readable = (i >> 50);
  92. }
  93. else if (absolute_i >= 0x4000000000000) // Petabyte
  94. {
  95. suffix = "PB";
  96. readable = (i >> 40);
  97. }
  98. else if (absolute_i >= 0x10000000000) // Terabyte
  99. {
  100. suffix = "TB";
  101. readable = (i >> 30);
  102. }
  103. else if (absolute_i >= 0x40000000) // Gigabyte
  104. {
  105. suffix = "GB";
  106. readable = (i >> 20);
  107. }
  108. else if (absolute_i >= 0x100000) // Megabyte
  109. {
  110. suffix = "MB";
  111. readable = (i >> 10);
  112. }
  113. else if (absolute_i >= 0x400) // Kilobyte
  114. {
  115. suffix = "KB";
  116. readable = i;
  117. }
  118. else
  119. {
  120. return i.ToString("0 B"); // Byte
  121. }
  122. // Divide by 1024 to get fractional value
  123. readable = (readable / 1024);
  124. // Return formatted number with suffix
  125. return readable.ToString("0.### ") + suffix;
  126. }
  127. }
  128. }