BaseItemExtensions.cs 3.6 KB

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