BaseItemExtensions.cs 3.5 KB

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