BaseItemExtensions.cs 3.9 KB

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