ContainerProfile.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Linq;
  4. using System.Xml.Serialization;
  5. namespace MediaBrowser.Model.Dlna
  6. {
  7. public class ContainerProfile
  8. {
  9. [XmlAttribute("type")]
  10. public DlnaProfileType Type { get; set; }
  11. public ProfileCondition[] Conditions { get; set; }
  12. [XmlAttribute("container")]
  13. public string Container { get; set; }
  14. public ContainerProfile()
  15. {
  16. Conditions = Array.Empty<ProfileCondition>();
  17. }
  18. public string[] GetContainers()
  19. {
  20. return SplitValue(Container);
  21. }
  22. public static string[] SplitValue(string value)
  23. {
  24. if (string.IsNullOrEmpty(value))
  25. {
  26. return Array.Empty<string>();
  27. }
  28. return value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  29. }
  30. public bool ContainsContainer(string container)
  31. {
  32. var containers = GetContainers();
  33. return ContainsContainer(containers, container);
  34. }
  35. public static bool ContainsContainer(string profileContainers, string inputContainer)
  36. {
  37. var isNegativeList = false;
  38. if (profileContainers != null && profileContainers.StartsWith("-", StringComparison.Ordinal))
  39. {
  40. isNegativeList = true;
  41. profileContainers = profileContainers.Substring(1);
  42. }
  43. return ContainsContainer(SplitValue(profileContainers), isNegativeList, inputContainer);
  44. }
  45. public static bool ContainsContainer(string[] profileContainers, string inputContainer)
  46. {
  47. return ContainsContainer(profileContainers, false, inputContainer);
  48. }
  49. public static bool ContainsContainer(string[] profileContainers, bool isNegativeList, string inputContainer)
  50. {
  51. if (profileContainers.Length == 0)
  52. {
  53. return true;
  54. }
  55. if (isNegativeList)
  56. {
  57. var allInputContainers = SplitValue(inputContainer);
  58. foreach (var container in allInputContainers)
  59. {
  60. if (profileContainers.Contains(container, StringComparer.OrdinalIgnoreCase))
  61. {
  62. return false;
  63. }
  64. }
  65. return true;
  66. }
  67. else
  68. {
  69. var allInputContainers = SplitValue(inputContainer);
  70. foreach (var container in allInputContainers)
  71. {
  72. if (profileContainers.Contains(container, StringComparer.OrdinalIgnoreCase))
  73. {
  74. return true;
  75. }
  76. }
  77. return false;
  78. }
  79. }
  80. }
  81. }