ContainerProfile.cs 2.8 KB

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