BaseItemExtensions.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #nullable disable
  2. #nullable enable
  3. #pragma warning disable CS1591
  4. using System;
  5. using System.Linq;
  6. using MediaBrowser.Model.Entities;
  7. using MediaBrowser.Model.IO;
  8. namespace MediaBrowser.Controller.Entities
  9. {
  10. public static class BaseItemExtensions
  11. {
  12. /// <summary>
  13. /// Gets the image path.
  14. /// </summary>
  15. /// <param name="item">The item.</param>
  16. /// <param name="imageType">Type of the image.</param>
  17. /// <returns>System.String.</returns>
  18. public static string GetImagePath(this BaseItem item, ImageType imageType)
  19. {
  20. return item.GetImagePath(imageType, 0);
  21. }
  22. public static bool HasImage(this BaseItem item, ImageType imageType)
  23. {
  24. return item.HasImage(imageType, 0);
  25. }
  26. /// <summary>
  27. /// Sets the image path.
  28. /// </summary>
  29. /// <param name="item">The item.</param>
  30. /// <param name="imageType">Type of the image.</param>
  31. /// <param name="file">The file.</param>
  32. public static void SetImagePath(this BaseItem item, ImageType imageType, FileSystemMetadata file)
  33. {
  34. item.SetImagePath(imageType, 0, file);
  35. }
  36. /// <summary>
  37. /// Sets the image path.
  38. /// </summary>
  39. /// <param name="item">The item.</param>
  40. /// <param name="imageType">Type of the image.</param>
  41. /// <param name="file">The file.</param>
  42. public static void SetImagePath(this BaseItem item, ImageType imageType, string file)
  43. {
  44. if (file.StartsWith("http", System.StringComparison.OrdinalIgnoreCase))
  45. {
  46. item.SetImage(
  47. new ItemImageInfo
  48. {
  49. Path = file,
  50. Type = imageType
  51. }, 0);
  52. }
  53. else
  54. {
  55. item.SetImagePath(imageType, BaseItem.FileSystem.GetFileInfo(file));
  56. }
  57. }
  58. /// <summary>
  59. /// Copies all properties on object. Skips properties that do not exist.
  60. /// </summary>
  61. /// <param name="source">The source object.</param>
  62. /// <param name="dest">The destination object.</param>
  63. public static void DeepCopy<T, TU>(this T source, TU dest)
  64. where T : BaseItem
  65. where TU : BaseItem
  66. {
  67. if (source == null)
  68. {
  69. throw new ArgumentNullException(nameof(source));
  70. }
  71. if (dest == null)
  72. {
  73. throw new ArgumentNullException(nameof(dest));
  74. }
  75. var destProps = typeof(TU).GetProperties().Where(x => x.CanWrite).ToList();
  76. foreach (var sourceProp in typeof(T).GetProperties())
  77. {
  78. // We should be able to write to the property
  79. // for both the source and destination type
  80. // This is only false when the derived type hides the base member
  81. // (which we shouldn't copy anyway)
  82. if (!sourceProp.CanRead || !sourceProp.CanWrite)
  83. {
  84. continue;
  85. }
  86. var v = sourceProp.GetValue(source);
  87. if (v == null)
  88. {
  89. continue;
  90. }
  91. var p = destProps.Find(x => x.Name == sourceProp.Name);
  92. if (p != null)
  93. {
  94. p.SetValue(dest, v);
  95. }
  96. }
  97. }
  98. /// <summary>
  99. /// Copies all properties on newly created object. Skips properties that do not exist.
  100. /// </summary>
  101. /// <param name="source">The source object.</param>
  102. public static TU DeepCopy<T, TU>(this T source)
  103. where T : BaseItem
  104. where TU : BaseItem, new()
  105. {
  106. var dest = new TU();
  107. source.DeepCopy(dest);
  108. return dest;
  109. }
  110. }
  111. }