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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. }
  82. }