BaseItemExtensions.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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", System.StringComparison.OrdinalIgnoreCase))
  43. {
  44. item.SetImage(
  45. new ItemImageInfo
  46. {
  47. Path = file,
  48. Type = imageType
  49. }, 0);
  50. }
  51. else
  52. {
  53. item.SetImagePath(imageType, BaseItem.FileSystem.GetFileInfo(file));
  54. }
  55. }
  56. /// <summary>
  57. /// Copies all properties on object. Skips properties that do not exist.
  58. /// </summary>
  59. /// <param name="source">The source object.</param>
  60. /// <param name="dest">The destination object.</param>
  61. /// <typeparam name="T">Source type.</typeparam>
  62. /// <typeparam name="TU">Destination type.</typeparam>
  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. /// <typeparam name="T">Source type.</typeparam>
  103. /// <typeparam name="TU">Destination type.</typeparam>
  104. /// <returns>Destination object.</returns>
  105. public static TU DeepCopy<T, TU>(this T source)
  106. where T : BaseItem
  107. where TU : BaseItem, new()
  108. {
  109. var dest = new TU();
  110. source.DeepCopy(dest);
  111. return dest;
  112. }
  113. }
  114. }