BaseExtensions.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. namespace MediaBrowser.Common.Extensions
  6. {
  7. /// <summary>
  8. /// Class BaseExtensions
  9. /// </summary>
  10. public static class BaseExtensions
  11. {
  12. /// <summary>
  13. /// Strips the HTML.
  14. /// </summary>
  15. /// <param name="htmlString">The HTML string.</param>
  16. /// <returns>System.String.</returns>
  17. public static string StripHtml(this string htmlString)
  18. {
  19. // http://stackoverflow.com/questions/1349023/how-can-i-strip-html-from-text-in-net
  20. const string pattern = @"<(.|\n)*?>";
  21. return Regex.Replace(htmlString, pattern, string.Empty).Trim();
  22. }
  23. /// <summary>
  24. /// Gets the M d5.
  25. /// </summary>
  26. /// <param name="str">The STR.</param>
  27. /// <returns>Guid.</returns>
  28. public static Guid GetMD5(this string str)
  29. {
  30. using (var provider = MD5.Create())
  31. {
  32. return new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(str)));
  33. }
  34. }
  35. /// <summary>
  36. /// Gets the MB id.
  37. /// </summary>
  38. /// <param name="str">The STR.</param>
  39. /// <param name="type">The type.</param>
  40. /// <returns>Guid.</returns>
  41. public static Guid GetMBId(this string str, Type type)
  42. {
  43. return str.GetMBId(type, false);
  44. }
  45. /// <summary>
  46. /// Gets the MB id.
  47. /// </summary>
  48. /// <param name="str">The STR.</param>
  49. /// <param name="type">The type.</param>
  50. /// <param name="isInMixedFolder">if set to <c>true</c> [is in mixed folder].</param>
  51. /// <returns>Guid.</returns>
  52. /// <exception cref="System.ArgumentNullException">type</exception>
  53. public static Guid GetMBId(this string str, Type type, bool isInMixedFolder)
  54. {
  55. if (type == null)
  56. {
  57. throw new ArgumentNullException("type");
  58. }
  59. var key = type.FullName + str.ToLower();
  60. if (isInMixedFolder)
  61. {
  62. key += "InMixedFolder";
  63. }
  64. return key.GetMD5();
  65. }
  66. /// <summary>
  67. /// Gets the attribute value.
  68. /// </summary>
  69. /// <param name="str">The STR.</param>
  70. /// <param name="attrib">The attrib.</param>
  71. /// <returns>System.String.</returns>
  72. /// <exception cref="System.ArgumentNullException">attrib</exception>
  73. public static string GetAttributeValue(this string str, string attrib)
  74. {
  75. if (string.IsNullOrEmpty(str))
  76. {
  77. throw new ArgumentNullException("str");
  78. }
  79. if (string.IsNullOrEmpty(attrib))
  80. {
  81. throw new ArgumentNullException("attrib");
  82. }
  83. string srch = "[" + attrib + "=";
  84. int start = str.IndexOf(srch, StringComparison.OrdinalIgnoreCase);
  85. if (start > -1)
  86. {
  87. start += srch.Length;
  88. int end = str.IndexOf(']', start);
  89. return str.Substring(start, end - start);
  90. }
  91. return null;
  92. }
  93. }
  94. }