BaseItemExtensions.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. public static void DeepCopy<T, TU>(this T source, TU dest)
  62. where T : BaseItem
  63. where TU : BaseItem
  64. {
  65. if (source == null)
  66. {
  67. throw new ArgumentNullException(nameof(source));
  68. }
  69. if (dest == null)
  70. {
  71. throw new ArgumentNullException(nameof(dest));
  72. }
  73. var destProps = typeof(TU).GetProperties().Where(x => x.CanWrite).ToList();
  74. foreach (var sourceProp in typeof(T).GetProperties())
  75. {
  76. // We should be able to write to the property
  77. // for both the source and destination type
  78. // This is only false when the derived type hides the base member
  79. // (which we shouldn't copy anyway)
  80. if (!sourceProp.CanRead || !sourceProp.CanWrite)
  81. {
  82. continue;
  83. }
  84. var v = sourceProp.GetValue(source);
  85. if (v == null)
  86. {
  87. continue;
  88. }
  89. var p = destProps.Find(x => x.Name == sourceProp.Name);
  90. if (p != null)
  91. {
  92. p.SetValue(dest, v);
  93. }
  94. }
  95. }
  96. /// <summary>
  97. /// Copies all properties on newly created object. Skips properties that do not exist.
  98. /// </summary>
  99. /// <param name="source">The source object.</param>
  100. public static TU DeepCopy<T, TU>(this T source)
  101. where T : BaseItem
  102. where TU : BaseItem, new()
  103. {
  104. var dest = new TU();
  105. source.DeepCopy(dest);
  106. return dest;
  107. }
  108. }
  109. }