BaseExtensions.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Security.Cryptography;
  7. namespace MediaBrowser.Common.Extensions
  8. {
  9. public static class BaseExtensions
  10. {
  11. static MD5CryptoServiceProvider md5Provider = new MD5CryptoServiceProvider();
  12. public static Guid GetMD5(this string str)
  13. {
  14. lock (md5Provider)
  15. {
  16. return new Guid(md5Provider.ComputeHash(Encoding.Unicode.GetBytes(str)));
  17. }
  18. }
  19. /// <summary>
  20. /// Examine a list of strings assumed to be file paths to see if it contains a parent of
  21. /// the provided path.
  22. /// </summary>
  23. /// <param name="lst"></param>
  24. /// <param name="path"></param>
  25. /// <returns></returns>
  26. public static bool ContainsParentFolder(this List<string> lst, string path)
  27. {
  28. path = path.TrimEnd('\\');
  29. foreach (var str in lst)
  30. {
  31. //this should be a little quicker than examining each actual parent folder...
  32. var compare = str.TrimEnd('\\');
  33. if (path.Equals(compare,StringComparison.OrdinalIgnoreCase)
  34. || (path.StartsWith(compare, StringComparison.OrdinalIgnoreCase) && path[compare.Length] == '\\')) return true;
  35. }
  36. return false;
  37. }
  38. /// <summary>
  39. /// Helper method for Dictionaries since they throw on not-found keys
  40. /// </summary>
  41. /// <typeparam name="T"></typeparam>
  42. /// <typeparam name="U"></typeparam>
  43. /// <param name="dictionary"></param>
  44. /// <param name="key"></param>
  45. /// <param name="defaultValue"></param>
  46. /// <returns></returns>
  47. public static U GetValueOrDefault<T, U>(this Dictionary<T, U> dictionary, T key, U defaultValue)
  48. {
  49. U val;
  50. if (!dictionary.TryGetValue(key, out val))
  51. {
  52. val = defaultValue;
  53. }
  54. return val;
  55. }
  56. }
  57. }