BaseItemExtensions.cs 4.2 KB

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